最小单位GameObject

GameObject中的成员变量
名字:print(this.gameObject.name);
是否激活:print(this.gameObject.activeSelf);
是否是静态:print(this.gameObject.isStatic);
层级:print(this.gameObject.layer);
标签:print(this.gameObject.tag);
GameObject中的静态方法
创建自带几何体:
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
只要得到了一个GameObject对象 我就可以得到它身上挂在的任何脚本信息,通过obj.GetComponent来得。
通过对象名查找对象:GameObject obj2 = GameObject.Find("Name");
通过tag来查找对象:GameObject obj3 = GameObject.FindGameObjectWithTag("Name");
两种找单个对象的共同点:
无法找到失活的对象的,只能找到 激活的对象。
如果场景中存在多个满足条件的对象,我们无法准确确定找到的是谁。
得到某一个单个对象:
是public从外部面板拖 进行关联
通过API去找
查找多个对象:GameObject[] objs = GameObject.FindGameObjectsWithTag("Name");
实例化对象(克隆对象)的方法:
实例化(克隆)对象 它的作用 是根据一个GameObject对象 创建出一个和它一模一样的对象
GameObject obj5 = GameObject.Instantiate(myObj);
删除对象的方法:
GameObject.Destroy(myObj2);
第二个参数 代表延迟几秒钟删除
GameObject.Destroy(obj5, 5);
立即把对象从内存中移除:
GameObject.DestroyImmediate(myObj);
过场景不移除 :
DontDestroyOnLoad(this.gameObject);
GameObject中的成员方法:
创建空物体:GameObject obj6 = new GameObject();
GameObject obj8 = new GameObject("顺便加脚本的空物体", typeof(Lesson2),typeof(Lesson1));
为对象添加脚本:
Le2 les2 = obj6.AddComponent<Le2>();
标签比较:
this.gameObject.CompareTag("Player");
设置激活失活:
obj6.SetActive(false);