前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过C#脚本实现旋转的立方体

通过C#脚本实现旋转的立方体

作者头像
Zoctopus
发布2018-06-04 10:24:29
1.6K0
发布2018-06-04 10:24:29
举报

一、介绍

目的:通过一个简单的例子(鼠标点击,使立方体旋转和变色)熟悉Unity中C#脚本的编写。

软件环境:Unity 2017.3.0f3 、 VS2013。

二、C#脚本实现

1,启动Unity,创建游戏场景。【关于Unity基本操作请点击 Unity入门教程(上)进行了解】

2,在Assets目录下创建文件夹,用于存放游戏的各种资源。

3,创建一个名为CubeRotate的C#脚本并拖放到场景的方块上,调整好相机位置。

4,双击打开脚本,在脚本中加入鼠标相关函数

5,设定一个功能:当鼠标光标移动到物体上时,物体材质色彩变为黄色。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeRotate : MonoBehaviour {

    private bool bCube1 = false;
    private bool bCube2 = false;
    private Color OldColor;

    // Use this for initialization
    void Start () {
        OldColor = this.gameObject.GetComponent<Renderer>().material.color; //获取物体的原始颜色
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    void OnMouseOver()
    {
        this.gameObject.GetComponent<Renderer>().material.color = Color.yellow; //立方体变为黄色
        bCube1 = true;
    }
    void OnMouseExit()
    {
        this.gameObject.GetComponent<Renderer>().material.color = OldColor; //立方体变为原始颜色
        bCube1 = false;
    }
    void OnMouseDown()
    {
        if(bCube1)
        {
            bCube2 = true;
        }
    }

}

代码解读:当鼠标光标移动到物体上时,物体变为黄色,同时将一个初始值为false的bCube1的值变为true;当鼠标光标离开后,物体材质色彩还原,bCube1为false;当按下鼠标左键,且bCube1的值为true,bCube2的值为真。

注:OnMouse函数都是执行一次的函数,因此不能将与动画有关的控制函数放于其内执行,所以通常会用布尔值开关来控制Update函数中的动画函数。

6,在Update函数里实现Cube转动

代码语言:javascript
复制
    void Update () {
        if(bCube2) //当Cube为真时
        {
            this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200);  //Cube转动
        }
    }

因为Cube转动是持续性的,所以把旋转脚本写在Update函数里面实现Cube转动。

7,更改Spotlight的强度

代码语言:javascript
复制
    // Use this for initialization
    void Start () {
        OldColor = this.gameObject.GetComponent<Renderer>().material.color; //获取物体的原始颜色
        GameObject.Find("Spotlight").GetComponent<Light>().intensity = 1.0F;  //获取Spotlight的强度属性
    }
    
    // Update is called once per frame
    void Update () {
        if(bCube2) //当Cube为真时
        {
            this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200);  //Cube转动
            if(GameObject.Find("Spotlight").GetComponent<Light>().intensity < 8.0F)
            {
                GameObject.Find("Spotlight").GetComponent<Light>().intensity += 0.05F;
            }
        }
    }

8,UGUI的使用->添加Text组件

9,添加控制Text显示的脚本

使用UGUI组件必须在C#脚本中添加UI的命名空间,这样我们才能引用。当bCube2的值为真时,Text组件显示“Cube正在旋转中...”,所以在Update函数的if语句里面应添加以下脚本

代码语言:javascript
复制
            GameObject.Find("Text").GetComponent<Text>().text = "Cube正在旋转...";

10,点击“Play”按钮,运行游戏

鼠标点击前:

鼠标点击后:

11,完整脚本代码

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;  //添加UI的命名空间

public class CubeRotate : MonoBehaviour {

    private bool bCube1 = false;
    private bool bCube2 = false;
    private Color OldColor;

    // Use this for initialization
    void Start () {
        OldColor = this.gameObject.GetComponent<Renderer>().material.color; //获取物体的原始颜色
        GameObject.Find("Spotlight").GetComponent<Light>().intensity = 1.0F;  //获取Spotlight的强度属性
    }
    
    // Update is called once per frame
    void Update () {
        if(bCube2) //当Cube为真时
        {
            this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200);  //Cube转动
            GameObject.Find("Text").GetComponent<Text>().text = "Cube正在旋转...";

            if(GameObject.Find("Spotlight").GetComponent<Light>().intensity < 8.0F)
            {
                GameObject.Find("Spotlight").GetComponent<Light>().intensity += 0.05F;
            }
        }
    }

    void OnMouseOver()
    {
        this.gameObject.GetComponent<Renderer>().material.color = Color.yellow; //立方体变为黄色
        bCube1 = true;
    }
    void OnMouseExit()
    {
        this.gameObject.GetComponent<Renderer>().material.color = OldColor; //立方体变为原始颜色
        bCube1 = false;
    }
    void OnMouseDown()
    {
        if(bCube1)
        {
            bCube2 = true;
        }
    }

}

三、总结

通过学习我们了解了C#脚本对于游戏对象的作用,中间还学习了UGUI的使用。

Unity脚本语言的综合应用并不是通过一个实例就能够达到熟练的程度,还需要自己不断地练习和探索,不断的尝试bug和及时总结。 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、介绍
  • 二、C#脚本实现
    • 1,启动Unity,创建游戏场景。【关于Unity基本操作请点击 Unity入门教程(上)进行了解】
      • 2,在Assets目录下创建文件夹,用于存放游戏的各种资源。
        • 3,创建一个名为CubeRotate的C#脚本并拖放到场景的方块上,调整好相机位置。
          • 4,双击打开脚本,在脚本中加入鼠标相关函数
            • 5,设定一个功能:当鼠标光标移动到物体上时,物体材质色彩变为黄色。
              • 6,在Update函数里实现Cube转动
                • 7,更改Spotlight的强度
                  • 8,UGUI的使用->添加Text组件
                    • 9,添加控制Text显示的脚本
                      • 10,点击“Play”按钮,运行游戏
                        • 11,完整脚本代码
                        • 三、总结
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档