zoukankan      html  css  js  c++  java
  • 通过C#脚本实现旋转的立方体

    一、介绍

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

    软件环境:Unity 2017.3.0f3 、 VS2013。

    二、C#脚本实现

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

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

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

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

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

    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转动

        void Update () {
            if(bCube2) //当Cube为真时
            {
                this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200);  //Cube转动
            }
        }

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

    7,更改Spotlight的强度

        // 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语句里面应添加以下脚本

                GameObject.Find("Text").GetComponent<Text>().text = "Cube正在旋转...";

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

    鼠标点击前:

    鼠标点击后:

    11,完整脚本代码

    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和及时总结。 

  • 相关阅读:
    简析时序数据库 InfluxDB
    tensorflow_1.x(四):线性回归问题初步(准备数据、构建模型、训练模型、进行预测)
    (二) 差分隐私直观理解
    (一) 差分隐私
    (四)PyTorch 的 torch.backends.cudnn.benchmark
    (三)PyTorch 的 Autograd
    (二)PyTorch 中的 tensor 及使用
    (一)PyTorch 中的 ModuleList 和 Sequential
    文本分类(六):不平衡文本分类,Focal Loss理论及PyTorch实现
    tensorflow_1.x(三):Tensorflow2入门(基础、张量、常量与变量、变量的赋值、计算模型、图执行模式、兼容1.0、会话、变量、占位符、feed提交数据)
  • 原文地址:https://www.cnblogs.com/OctoptusLian/p/8746797.html
Copyright © 2011-2022 走看看