Transform 、GameObject 区别【新手向】

来源:互联网 发布:ajax传数组到php 编辑:程序博客网 时间:2024/06/16 08:16

以下叙述的区别,只针对新手向。对于两者之间傻傻分不清楚的原因做下总结叙述。

    新手常常对这俩个类的用法表示很迷茫。    为什么有时候用Transform, 有时候用Gameobject。    而且不是说Transform 是控制旋转、缩放等。     怎么还看到有教学视频里面用来 GetComponents<T> 获取组件,亦或者FindChild("") 等等。

GameObject

1.GameObject 是一个类,其具体的对象实现就是 gameObject。比如Gameobject gameObject = new GameObject();只不过这个部分,不用我们来操心。  2.gameObject.GetComponent<T>() 就是我们经常用来获取组件的方法。3.并且它还可以获取到Transform。 gameObject.transform。

Transform

1.Transform 是一个类 具体描述如GameObject 【1】 描述相同。2.Transform 单独来说的确就是控制旋转、缩放、位置等。但是跟进去看它的类, 它是继承于 Component 的。
namespace UnityEngine{    public class Transform : Component, IEnumerable    {

区别

    我们接下来在看下这个 Component 中有什么 , 下面的部分新手看起来可能会头痛,不过不用看明白。 大部分的代码 和 GameObject 中的一样 都是能用来  使用GetComponent 获取组件的。     这也就是为什么我们会看到 transform.GetComponent 的原因。因为他的父类提供了这些。 不仅仅是这样。下面代码片段中的最后,你会发现他还提供了 获取GameObject 。  ………………  是不是有点乱。。 1. GameObject 可以获取 Transform 2. Transform 可以通过继承 Component 来获取   GameObject 所以结果就是如果你想 你可以写成。。 gameObject. transform. gameObject. transform
namespace UnityEngine{    [RequiredByNativeCode]    public class Component : Object    {        //        // Properties        //        [Obsolete ("Property animation has been deprecated. Use GetComponent<Animation>() instead. (UnityUpgradable)", true)]        public Component animation {            get;        }        [Obsolete ("Property audio has been deprecated. Use GetComponent<AudioSource>() instead. (UnityUpgradable)", true)]        public Component audio {            get;        }        [Obsolete ("Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)", true)]        public Component camera {            get;        }        [Obsolete ("Property collider has been deprecated. Use GetComponent<Collider>() instead. (UnityUpgradable)", true)]        public Component collider {            get;        }        [Obsolete ("Property collider2D has been deprecated. Use GetComponent<Collider2D>() instead. (UnityUpgradable)", true)]        public Component collider2D {            get;        }        [Obsolete ("Property constantForce has been deprecated. Use GetComponent<ConstantForce>() instead. (UnityUpgradable)", true)]        public Component constantForce {            get;        }        public extern GameObject gameObject {            [WrapperlessIcall]            [MethodImpl (MethodImplOptions.InternalCall)]            get;        }

总结

    总结下来就是 Transform 是 GameObject 的 一个组件,的确是用来控制位置、缩放等。但是因为他的父类原因。    它才可以获取GameObject类的 对象 gameObject。并且其父类自身也提供了类似 GetComponent<T>() 这样的方法来获取其他组件。希望这篇新手博客,可以帮助到他人!

以上如果有说的不对的地方,希望可以评论提出,我也好及时矫正错误的知识认知

    ——————————刚刚开始自学Unity3D 的菜鸟
0 0
原创粉丝点击