问题描述
限时送ChatGPT账号..我很困惑:
脚本附加到一个简单的 Cube 对象.
The script is attached to a simple Cube object.
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
会是一个脚本对象吗?
1> Will CS01
be an script object right?
DOC:Unity 中的脚本包括附加自定义脚本对象被称为游戏对象的行为.
DOC: Scripting inside Unity consists of attaching custom script objects called behaviours to game objects.
2> 在Component
对象中,transform、renderer、rigidbody 等变量被引用到这个Component
附属的gameObject
的组件.所以在脚本中,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
指的是脚本对象而不是脚本所附加的游戏对象,对吧?
2> In Component
object, variables like transform, renderer, rigidbody are referenced to the components of gameObject
this Component
attached to. So in the script, this.renderer.material.color = Color.red;
is equivalent to this.gameObject.renderer.material.color = Color.red
. However, the description of variable name
in the document name: The name of the object.
only tells it's the name of the object.
3> So, how to understand the code above? Does the variable name
also return the name
of the gameObject this script attached to?
4> this
means the script object but not the gameObject the script attached to, right?
推荐答案
如果你将 CS01
附加到场景中的一个游戏对象并且这个游戏对象将被加载,你将有一个实例.在Awake
和OnEnable
被调用之前的CS01
构建过程中,gameObject
、tranform<等所有属性/code>、
name
等被初始化.如果它们引用一个对象,它们中的大多数将获得对父属性的引用.
If you attach CS01
to a game object in the scene and this game object will be loaded, you will have one instance of each. During the construction of CS01
before Awake
and OnEnable
are called, all properties like gameObject
, tranform
, name
, etc. are initialised. Most of them will get a reference to the parent's properties if they are refering to an object.
string
类在 C# 或 java 中有些特殊,其行为类似于 struct
- 它会被复制.详细说明:CS01.name
(继承自Unity.Object
)将获得游戏对象的名称.只要它们具有相同的名称,则两个变量都指向内存中的相同位置.当您更改 CS01 的名称时,将在内部创建一个新的 string
实例,并使用您分配给它的文本进行初始化.因此游戏对象的原始即名称属性将保持不变.
string
class is somewhat special in C# or java and behaves rather like a struct
- it will be kind of copied. In detail: CS01.name
(which is inherited from Unity.Object
) will get the game object's name. As long as they have both the same name, both variables point to the same place in memory. When you change CS01's name, a new string
instance will be created internally and be initialised with the text you assign to it. Thus the original i.e. name property of game object will remain unchanged.
GameObject
属性的属性,一种快捷方式和2一样.name
定义在基类Object
中.GameObject
和 MonoBehaviour
都源自 Unity.Object
.没错,这就是您在代码中编写它的类
Yes
Most components have properties that refer to the GameObject
properties, kind of shortcut
Like in 2. name
is defined in base class Object
. GameObject
and MonoBehaviour
both are derived from Unity.Object
.
Exactly, this is the class where you writing it in the code
这篇关于Unity3D 脚本(c# 脚本、JavaScript)中的“this"关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论