Unity-Mono中的重要内容

一.重要成员

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))
{   
    //逻辑处理
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值