本文仅用于记录作者的学习过程,有些细节可能会自动在脑中忽略掉,专业及完整的描述可以点击超链接进入Unity官网查看相应API文件。
开始时我们需要明确:GameObject是Unity 场景中所有实体的基类。
注意:GameObject 类中的许多变量已被移除。要访问 例如 csharp 中的 GameObject.renderer,则使用 GetComponent<Renderer>()。在 JS 脚本中,使用 GetComponent.<Renderer>()。
activeInHierarchy | 定义 GameObject 在 Scene 中是否处于活动状态。 |
activeSelf | 此 GameObject 的本地活动状态。(只读) |
isStatic | 仅限 Editor 的 API,指定游戏对象是否为静态。 |
layer | 该游戏对象所在的层。 |
scene | 该 GameObject 所属的场景。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
创建对象
首先创建一个新的空对象,用于测试脚本
编写脚本测试GameObject中各类成员变量:
名字,激活状态,是否静态,层级,标签,位置信息
其中位置信息是通过父类MonoBehaviour去获取信息
脚本代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//名字
print(this.gameObject.name);
//改名!!!
this.gameObject.name = "yefly";
print(this.gameObject.name);
//是否激活
print(this.gameObject.activeSelf);
//是否是静态
print(this.gameObject.isStatic);
//层级!!!
print(this.gameObject.layer);
//标签
print(this.gameObject.tag);
/*transform 通过Mono去得到的依附对象的GameObject的位置信息
他们得到的信息是一样的 都是依附的GameObject的位置信息!!!*/
print(this.gameObject.transform.position);
}
// Update is called once per frame
void Update()
{
}
}
启动项目
保存脚本并把脚本挂载到空对象上后,运行项目
控制台输出的内容与unity中的信息一致,并且改名成功