unity源码解析GameObject

来源:互联网 发布:易语言ip攻击器源码 编辑:程序博客网 时间:2024/05/29 14:14
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 transform2in 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 ComponentGetComponentInParent(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(boolvalue);

        [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);
    }
}
0 0
原创粉丝点击