欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Day03-MonoBehavior中的常用内容

Day03-MonoBehavior中的常用内容

2024/10/25 12:16:32 来源:https://blog.csdn.net/qq_40299598/article/details/141436865  浏览:    关键词: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}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com