目的:通过一个简单的例子(鼠标点击,使立方体旋转和变色)熟悉Unity中C#脚本的编写。
软件环境:Unity 2017.3.0f3 、 VS2013。
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函数中的动画函数。
void Update () {
if(bCube2) //当Cube为真时
{
this.gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 200); //Cube转动
}
}
因为Cube转动是持续性的,所以把旋转脚本写在Update函数里面实现Cube转动。
// 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;
}
}
}
使用UGUI组件必须在C#脚本中添加UI的命名空间,这样我们才能引用。当bCube2的值为真时,Text组件显示“Cube正在旋转中...”,所以在Update函数的if语句里面应添加以下脚本
GameObject.Find("Text").GetComponent<Text>().text = "Cube正在旋转...";
鼠标点击前:
鼠标点击后:
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和及时总结。