一.重要成员
1.获取依附的GameObject
void Start()
{
//1.获取依附的GameObject
print(this.gameObject.name);
//2.获取依附的GameObject的位置信息
//得到位置信息
print(this.transform.positon);
//得到欧拉角度信息
print(this.transform.eulerAngles);
//得到缩放大小
print(this.transform.lossyScale);
//这种写法和上面是一样的效果,都是得到依附对象信息
//this.gameObject.transform;
//3.获取脚本是否激活
//true:激活 false:失活
this.enabled = true;
}
2.重要方法
先创建两个脚本
//得到依附对象上挂载的其它脚本
//1.得到自己挂载的单个脚本
//根据脚本名获取
//this.GetComponent("获得的脚本名字");
//获取脚本的方法,如果获取失败,就是没有对应的脚本,会默认返回空
Lesson3_Test t = this.GetComponent("Lesson3_Test") as Lesson3_Test;
print(t);
//根据Type获取
t = this.GetCompnent(typeof(Lesson3_Test)) as Lesson3_Test;
print(t);
//根据泛型获取
t = this.GetComponent<Lesson3_Test>();
print(t);
//只要你能得到场景中别的对象或者对象依附的脚本
//那么你就可以获取到它的所有信息
//2.得到自己挂载的多个脚本
Lesson3[] array = this.GetComponents<Lesson3>();
print(array.Length);
List<Lesson3> list = new List<Lesson3>();
this.getComponents<Lesson3>(list);
print(list.Count);
//3.得到子对象挂载的脚本(它默认也会找自己身上是否挂载该脚本)
//函数是有一个参数的默认不传 是false 意思是 如果子对象失活 是不会去找这个对象上是否有某个脚本的
//如果传true 即使失活也会找
//得子对象 挂载脚本单个
t = this.getComponentInChildren<Lesson3_Test>(true);
print(t);
//得子对象 挂载脚本多个
Lesson3_Test[] lts = this.getComponentsInChildren<Lesson3_Test>(true);
print(lts.Lenght);
List<Lesson3_Test> lts2 = new List<Lesson3_Test>();
this.GetComponentsInChildren<Lesson3_Test>(true,list2);
print(list2.Count);
//4.得到父对象挂载的脚本
t = this.GetComponentInParent<Lesson3_Test>();
//5.尝试获取脚本
Lesson3_Test l3t;
//提供了一个更加安全的获取单个脚本的方法,如果得到了,会返回true
//然后在来进行逻辑处理即可
if(this.TryGetComponent<Lesson3_Test>(out l3t))
{
//逻辑处理
}