我很困惑:
代码 (CS01.cs)
该脚本附加到一个简单的 Cube 对象。
using UnityEngine;
using System.Collections;
public class CS01 : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log (this); //--> Cube(CS01)
Debug.Log (this.GetType()); //--> CS01
Debug.Log (this.GetType() == typeof(UnityEngine.GameObject)); //--> False
Debug.Log (this == gameObject); //--> False
Debug.Log (this.name); //--> Cube
Debug.Log (gameObject.name); //--> Cube
this.name = "Hello"; // --> Will get the gameObject's name changed to 'Hello'
Debug.Log (gameObject.name); //--> Hello
}
// Update is called once per frame
void Update () {
}
}
1> 会CS01
是一个脚本对象吧?
DOC:Unity 中的脚本包括将称为行为的自定义脚本对象附加到游戏对象。
2> 在Component
object 中,transform、renderer、rigidbody 等变量都引用到gameObject
thisComponent
附加到的组件。所以在脚本中,this.renderer.material.color = Color.red;
等价于this.gameObject.renderer.material.color = Color.red
. name
但是,文档中对变量的描述name: The name of the object.
只告诉它是对象的名称。
3> 那么,如何理解上面的代码呢?该变量是否name
还返回name
此脚本附加到的游戏对象的?
4>this
指的是脚本对象,而不是脚本附加的游戏对象,对吧?