unity源碼解析GameObject

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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章