当前位置: 首页 > news >正文

Day03-MonoBehavior中的常用内容

🏆 个人愚见,没事写写笔记

🏆《博客内容》:Unity3D开发内容

🏆🎉欢迎 👍点赞✍评论⭐收藏

🔎目标:MonoBehavior中的常用内容

☀️实现:获取当前GameObject,Transfrom信息的属性,以及多种获取脚本信息的方法。

注:脚本挂在到GameObject物体上!!!

public class Learn3 : MonoBehaviour
{public Learn3Test learn3Test;private void Start(){#region 成员// 1.获取当前的GameObjectGameObject gameObject = this.gameObject;print(gameObject.name);// 2.获取当前的TransfromTransform transform = this.transform;//同理:Transform transform =this.gameObject.transform;print(transform.position);//位置print(transform.eulerAngles);//旋转print(transform.lossyScale);//缩放// 3.控制脚本是否激活this.enabled = false;// 4.获取其他的脚本对象的GameObjectGameObject otherGameObject1 = learn3Test.gameObject;print(otherGameObject1.name);#endregion#region 方法//获取对象的其它脚本//获取自身单个脚本//1.根据脚本名字获取,如果获取失败返回NULLLearn3Test learn3Test1 = this.GetComponent("Learn3Test") as Learn3Test;if (learn3Test1 != null) learn3Test1.TestFun(nameof(learn3Test1));//2.根据类型获取Learn3Test learn3Test2 = this.GetComponent(typeof(Learn3Test)) as Learn3Test;if (learn3Test2 != null) learn3Test2.TestFun(nameof(learn3Test2));//3.根据泛型获取(常用)Learn3Test learn3Test3 = this.GetComponent<Learn3Test>();if (learn3Test3 != null) learn3Test3.TestFun(nameof(learn3Test3));//只要得到到场景中别的对象或者这个对象身上的脚本,就能获取到这个对象的所有信息//获取自身多个脚本Learn3Test[] learn3TestArray = this.GetComponents<Learn3Test>();print(learn3TestArray.Length);List<Learn3Test> learn3TestList = new List<Learn3Test>();this.GetComponents<Learn3Test>(learn3TestList);print(learn3TestList.Count);//获取子物体身上的脚本(所有的子物体,无论多层级)//GetComponentsInChildren默认也会找自身上的脚本,默认是false,失活不找这个脚本Learn3Test[] Learn3Test4Array = this.GetComponentsInChildren<Learn3Test>(true);print(Learn3Test4Array.Length);//获取父物体身上的脚本(所有的父物体,无论多层级)//GetComponentInParent默认也会找自身上的脚本,默认是false,失活不找这个脚本Learn3Test learn3Test5 = this.GetComponentInParent<Learn3Test>(true);if (learn3Test5 != null) learn3Test5.TestFun(nameof(learn3Test5));//尝试获取脚本Learn3Test learn3Test6;bool isGet = this.TryGetComponent<Learn3Test>(out learn3Test6);print(isGet);#endregion}

🚀感谢:🎉欢迎 👍点赞✍评论⭐收藏


http://www.mrgr.cn/news/5954.html

相关文章:

  • 金九银十,软件测试面试八股文【含答案+文档】
  • 解决 vmware nat 模式无法上网
  • C++入门——14树型关联容器
  • Go 语言类型转换 19
  • X-AnyLabeling系列:多类别单通道掩码转多边形坐标点
  • XML 实例:深入解析与实际应用
  • 网上花店设计+vue
  • 数据库如何插入优化分别用oracle和mysql举例
  • C ++初阶:C++入门级知识点
  • CSS的:nth-last-child()伪类:逆序子元素选择的艺术
  • commvault学习(9):备份mysql(linux平台)
  • Ubuntu启用root用户
  • 【django进阶知识点】
  • Frida实战:枚举模块、导入表、导出表与符号表
  • 结构型模式之组合模式
  • YOLOv10:实时端到端目标检测
  • IDM下载器怎么提速 IDM下载速度慢解决办法 idm下载器设置怎么下载最快
  • git打包某个分支的特定提交
  • 【SpringBoot】开发常见注解
  • RabbitMQ中如何通过死信交换机实现延时队列