【发布时间】:2013-07-23 14:05:15
【问题描述】:
我很困惑:
代码(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 对象中,transform、renderer、rigidbody 等变量被引用到gameObject 这个Component 所附加的组件。所以在脚本中,this.renderer.material.color = Color.red; 等价于this.gameObject.renderer.material.color = Color.red。但是,name: The name of the object. 文档中对变量 name 的描述仅告诉它是对象的名称。
3> 那么,如何理解上面的代码呢?变量name 是否也返回此脚本附加到的游戏对象的name?
4> this 指的是脚本对象,而不是脚本附加的游戏对象,对吧?
【问题讨论】:
标签: unity3d