Transform 、GameObject 区别

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

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

GameObject

1.GameObject 是一个类,其具体的对象实现就是 gameObject。
比如Gameobject gameObject = new GameObject();
只不过这个部分,不用我们来操心。  

2.gameObject.GetComponent<T>() 就是我们经常用来获
取组件的方法。

3.并且它还可以获取到Transform。 gameObject.transform。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Transform

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

区别

    我们接下来在看下这个 Component 中有什么 , 下面的部分新手看起来可能会头痛,不过不用看明白。 大部分的代码 和 GameObject 中的一样 都是能用来  使用GetComponent 获取组件的。 
    这也就是为什么我们会看到 transform.GetComponent 的原因。因为他的父类提供了这些。 不仅仅是这样。下面代码片段中的最后,你会发现他还提供了 获取GameObject 。 

 ………………  是不是有点乱。。
 1. GameObject 可以获取 Transform
 2. Transform 可以通过继承 Component 来获取   GameObject

 所以结果就是如果你想 你可以写成。。
 gameObject. transform. gameObject. transform
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

总结

    总结下来就是 Transform 是 GameObject 的 一个组件,的确是用来控制位置、缩放等。但是因为他的父类原因。
    它才可以获取GameObject类的 对象 gameObject。并且其父类自身也提供了类似 GetComponent<T>() 这样的方法来获取其他组件。
希望这篇新手博客,可以帮助到他人!
  • 1
  • 2
  • 3
  • 4
以上如果有说的不对的地方,希望可以评论提出,我也好及时矫正错误的知识认知
    ——————————刚刚开始自学Unity3D 的菜鸟
  • 1
  • 2
 https://blog.csdn.net/qq_31912661/article/details/51765062
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值