unity源码解析GameObject

本文详细介绍了 Unity 中 GameObject 类的功能,包括如何获取不同类型的组件、添加组件、查找带有特定标签的游戏对象等。此外还提供了 GameObject 的构造方法及一些实用方法的使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine.Internal;
using UnityEngineInternal;

namespace UnityEngine
{
    public sealed class GameObject : Object
    {
        public extern bool isStatic
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        internal extern bool isStaticBatchable
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Transform 组件的接口
        public extern Transform transform
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Rigidbody 组件的接口
        public extern Rigidbody rigidbody
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Rigidbody2D 组件的接口
        public extern Rigidbody2D rigidbody2D
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Camera 组件的接口
        public extern Camera camera
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Light 组件的接口
        public extern Light light
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Animation 组件的接口
        public extern Animation animation
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得ConstantForce 组件的接口
        public extern ConstantForce constantForce
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Renderer 组件的接口
        public extern Renderer renderer
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得AudioSource 组件的接口
        public extern AudioSource audio
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得GUIText 组件的接口
        public extern GUIText guiText
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得NetworkView 组件的接口
        public extern NetworkView networkView
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得GUIElement 组件的接口
        [Obsolete("Please use guiTexture instead")]
        public extern GUIElement guiElement
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得GUITexture组件的接口
        public extern GUITexture guiTexture
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Collider 组件的接口
        public extern Collider collider
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得Collider2D 组件的接口
        public extern Collider2D collider2D
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;

        }


        public extern HingeJoint hingeJoint
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得ParticleEmitter 组件的接口
        public extern ParticleEmitter particleEmitter
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
//提供了获得ParticleSystem 组件的接口
        public extern ParticleSystem particleSystem
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }

        public extern int layer
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        [Obsolete("GameObject.active is obsolete. Use GameObject.SetActive(), GameObject.activeSelf or GameObject.activeInHierarchy.")]
        public extern bool active
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        public extern bool activeSelf
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }

        public extern bool activeInHierarchy
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }

        public extern string tag
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        public GameObject gameObject
        {
            get
            {
                return this;
            }
        }
//构造函数
        public GameObject(string name)
        {
            GameObject.Internal_CreateGameObject(this, name);
        }
//构造函数
        public GameObject()
        {
            GameObject.Internal_CreateGameObject(this, null);
        }
//构造函数,并且添加一个组件
        public GameObject(string name, params Type[] components)
        {
            GameObject.Internal_CreateGameObject(this, name);
            for (int i = 0; i < components.Length; i++)
            {
                Type componentType = components[i];
                this.AddComponent(componentType);
            }
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SampleAnimation(AnimationClip animation, float time);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern GameObject CreatePrimitive(PrimitiveType type);

        [WrapperlessIcall, TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Component GetComponent(Type type);
//获得组件
        public T GetComponent<T>() where T : Component
        {
            return this.GetComponent(typeof(T)) as T;
        }

        public Component GetComponent(string type)
        {
            return this.GetComponentByName(type);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern Component GetComponentByName(string type);

        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInChildren(Type type) //这个需要注意,是一个递归方法,从子节点查找组件,找到立即返回,如果最后都没有找到则返回空
        {
            if (this.activeInHierarchy)
            {
                Component component = this.GetComponent(type);
                if (component != null)
                {
                    return component;
                }
            }
            Transform transform = this.transform;
            if (transform != null)
            {
                foreach (Transform transform2 in transform)
                {
                    Component componentInChildren = transform2.gameObject.GetComponentInChildren(type);
                    if (componentInChildren != null)
                    {
                        return componentInChildren;
                    }
                }
            }
            return null;
        }
//调用了上面的方法
        public T GetComponentInChildren<T>() where T : Component
        {
            return this.GetComponentInChildren(typeof(T)) as T;
        }

        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]

//这个方法,优先从自己身上找到该组件,如果找不到则从父节点出发,将递归所有的父节点,如果所有父节点都没有,则返回空

      public Component GetComponentInParent(Type type)

        {
            if (this.activeInHierarchy)
            {
                Component component = this.GetComponent(type);
                if (component != null)
                {
                    return component;
                }
            }
            Transform parent = this.transform.parent;
            if (parent != null)
            {
                while (parent != null)
                {
                    if (parent.gameObject.activeInHierarchy)
                    {
                        Component component2 = parent.gameObject.GetComponent(type);
                        if (component2 != null)
                        {
                            return component2;
                        }
                    }
                    parent = parent.parent;
                }
            }
            return null;
        }
//同上
        public T GetComponentInParent<T>() where T : Component
        {
            return this.GetComponentInParent(typeof(T)) as T;
        }
//调用了外部方法,因为没有开源,只能根据名字来猜测,获得父节点以及以上节点中的指定组件,includeInactive是否包括非active成员,将这些组件放到result列表里
        public void GetComponentsInParent<T>(bool includeInactive, List<T> results) where T : Component
        {
            this.GetComponentsForListInternal(typeof(T), typeof(T), true, includeInactive, true, results);
        }

        [CanConvertToFlash]

        public Component[] GetComponents(Type type)

        {

//GetComponentsInternal(Type type, bool isGenericTypeArray, bool recursive, bool includeInactive, bool reverse);

            return this.GetComponentsInternal(type, false, false, true, false);
        }

        public T[] GetComponents<T>() where T : Component
        {
            return (T[])this.GetComponentsInternal(typeof(T), true, false, true, false);
        }

        public void GetComponents(Type type, List<Component> results)
        {
            this.GetComponentsForListInternal(type, typeof(Component), false, true, false, results);
        }

        public void GetComponents<T>(List<T> results) where T : Component
        {
            this.GetComponentsForListInternal(typeof(T), typeof(T), false, true, false, results);
        }

        [ExcludeFromDocs]
        public Component[] GetComponentsInChildren(Type type)
        {
            bool includeInactive = false;
            return this.GetComponentsInChildren(type, includeInactive);
        }

        public Component[] GetComponentsInChildren(Type type, [DefaultValue("false")] bool includeInactive)
        {
            return this.GetComponentsInternal(type, false, true, includeInactive, false);
        }

        public T[] GetComponentsInChildren<T>(bool includeInactive) where T : Component
        {
            return (T[])this.GetComponentsInternal(typeof(T), true, true, includeInactive, false);
        }

        public void GetComponentsInChildren<T>(bool includeInactive, List<T> results) where T : Component
        {
            this.GetComponentsForListInternal(typeof(T), typeof(T), true, includeInactive, false, results);
        }

        public T[] GetComponentsInChildren<T>() where T : Component
        {
            return this.GetComponentsInChildren<T>(false);
        }

        public void GetComponentsInChildren<T>(List<T> results) where T : Component
        {
            this.GetComponentsInChildren<T>(false, results);
        }

        [ExcludeFromDocs]
        public Component[] GetComponentsInParent(Type type)
        {
            bool includeInactive = false;
            return this.GetComponentsInParent(type, includeInactive);
        }

        public Component[] GetComponentsInParent(Type type, [DefaultValue("false")] bool includeInactive)
        {
            return this.GetComponentsInternal(type, false, true, includeInactive, true);
        }

        public T[] GetComponentsInParent<T>(bool includeInactive) where T : Component
        {
            return (T[])this.GetComponentsInternal(typeof(T), true, true, includeInactive, true);
        }

        public T[] GetComponentsInParent<T>() where T : Component
        {
            return this.GetComponentsInParent<T>(false);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void GetComponentsForListInternal(Type searchType, Type listElementType, bool recursive, bool includeInactive, bool reverse, object resultList);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern Component[] GetComponentsInternal(Type type, bool isGenericTypeArray, bool recursive, bool includeInactive, bool reverse);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetActive(bool value);

        [Obsolete("gameObject.SetActiveRecursively() is obsolete. Use GameObject.SetActive(), which is now inherited by children."), WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetActiveRecursively(bool state);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern bool CompareTag(string tag);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern GameObject FindGameObjectWithTag(string tag);
//静态方法,全局查找tag对应的gameobject
        public static GameObject FindWithTag(string tag)
        {
            return GameObject.FindGameObjectWithTag(tag);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern GameObject[] FindGameObjectsWithTag(string tag);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SendMessageUpwards(string methodName, [DefaultValue("null")] object value, [DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);

        [ExcludeFromDocs]//触发该物体以及父物体的methodName方法
        public void SendMessageUpwards(string methodName, object value)
        {
            SendMessageOptions options = SendMessageOptions.RequireReceiver;
            this.SendMessageUpwards(methodName, value, options);
        }

        [ExcludeFromDocs]//触发该物体以及父物体的methodName方法
        public void SendMessageUpwards(string methodName)
        {
            SendMessageOptions options = SendMessageOptions.RequireReceiver;
            object value = null;
            this.SendMessageUpwards(methodName, value, options);
        }

        public void SendMessageUpwards(string methodName, SendMessageOptions options)
        {
            this.SendMessageUpwards(methodName, null, options);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SendMessage(string methodName, [DefaultValue("null")] object value, [DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);

        [ExcludeFromDocs]
        public void SendMessage(string methodName, object value)
        {
            SendMessageOptions options = SendMessageOptions.RequireReceiver;
            this.SendMessage(methodName, value, options);
        }

        [ExcludeFromDocs]
        public void SendMessage(string methodName)
        {
            SendMessageOptions options = SendMessageOptions.RequireReceiver;
            object value = null;
            this.SendMessage(methodName, value, options);
        }

        public void SendMessage(string methodName, SendMessageOptions options)
        {
            this.SendMessage(methodName, null, options);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void BroadcastMessage(string methodName, [DefaultValue("null")] object parameter, [DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);

        [ExcludeFromDocs]//跟SendMessageUpwards相反,是触发该物体以及子物体methodName方法
        public void BroadcastMessage(string methodName, object parameter)
        {
            SendMessageOptions options = SendMessageOptions.RequireReceiver;
            this.BroadcastMessage(methodName, parameter, options);
        }

        [ExcludeFromDocs]
        public void BroadcastMessage(string methodName)
        {
            SendMessageOptions options = SendMessageOptions.RequireReceiver;
            object parameter = null;
            this.BroadcastMessage(methodName, parameter, options);
        }

        public void BroadcastMessage(string methodName, SendMessageOptions options)
        {
            this.BroadcastMessage(methodName, null, options);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Component AddComponent(string className);
//添加组件
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component AddComponent(Type componentType)
        {
            return this.Internal_AddComponentWithType(componentType);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern Component Internal_AddComponentWithType(Type componentType);

        public T AddComponent<T>() where T : Component
        {
            return this.AddComponent(typeof(T)) as T;
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void Internal_CreateGameObject([Writable] GameObject mono, string name);

        [Obsolete("gameObject.PlayAnimation is not supported anymore. Use animation.Play"), WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void PlayAnimation(AnimationClip animation);

        [Obsolete("gameObject.StopAnimation is not supported anymore. Use animation.Stop"), WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void StopAnimation();

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern GameObject Find(string name);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁potato

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值