unity源碼解析Object

有個工具獲取unity源碼,就是ILSPY,隨便解壓一個unity的遊戲包,可以找到下列文件


這些文件就是unity的編譯的結果。把他們拖入ILSPY之後就可以看到這些裏面到底幹了些什麼。


Object這個類是uinty的核心,之後開發者用到的東西幾乎都是繼承自它,爲了方便起見,我直接在代碼龐添加註釋。嗯,開始吧:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine.Internal;
using UnityEngineInternal;

namespace UnityEngine
{
    [StructLayout(LayoutKind.Sequential)]
    public class Object
    {
        private ReferenceData m_UnityRuntimeReferenceData;

        public extern string name  //就是該對象的名字
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        public extern HideFlags hideFlags //設置隱藏
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        public override bool Equals(object o)//比較兩個對象是否相同,最終是跳轉到CompareBaseObjectsInternal
        {
            return Object.CompareBaseObjects(this, oas Object);
        }

        public override int GetHashCode()
        {
            return this.GetInstanceID();
        }

        private static bool CompareBaseObjects(Object lhs, Object rhs)
        {
            return Object.CompareBaseObjectsInternal(lhs, rhs);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern bool CompareBaseObjectsInternal([Writable] Object lhs, [Writable] Object rhs);

        [NotRenamed]
        public int GetInstanceID()//獲取實例化id
        {
            return this.m_UnityRuntimeReferenceData.instanceID;
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Object Internal_CloneSingle(Object data);//深拷貝,實例化時候使用

        private static Object Internal_InstantiateSingle(Object data, Vector3 pos, Quaternion rot)//同上
        {
            return Object.INTERNAL_CALL_Internal_InstantiateSingle(data,ref pos, ref rot);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Object INTERNAL_CALL_Internal_InstantiateSingle(Object data, ref Vector3 pos, ref Quaternion rot);

        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original, Vector3 position, Quaternion rotation)//實例化
        {
            Object.CheckNullArgument(original,"The prefab you want to instantiate is null.");
            return Object.Internal_InstantiateSingle(original, position, rotation);
        }

        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original)//實例化
        {
            Object.CheckNullArgument(original,"The thing you want to instantiate is null.");
            return Object.Internal_CloneSingle(original);
        }

        private static void CheckNullArgument(object arg,string message) //檢查傳入的對象是否爲空
        {
            if (arg == null)
            {
                throw new ArgumentException(message);
            }
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern void Destroy(Object obj, [DefaultValue("0.0F")]float t);

        [ExcludeFromDocs]
        public static void Destroy(Object obj) //根據提供的t來決定延遲後銷燬對象,0爲立即
        {
            float t = 0f;
            Object.Destroy(obj, t);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern void DestroyImmediate(Object obj, [DefaultValue("false")]bool allowDestroyingAssets);

        [ExcludeFromDocs]
        public static void DestroyImmediate(Object obj)//銷燬對象,區別是一般用在編輯模式下
        {
            bool allowDestroyingAssets =false;
            Object.DestroyImmediate(obj, allowDestroyingAssets);
        }

        [WrapperlessIcall, TypeInferenceRule(TypeInferenceRules.ArrayOfTypeReferencedByFirstArgument)]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern Object[] FindObjectsOfType(Type type);

        public static T[] FindObjectsOfType<T>() where T : Object
        {
            return Resources.ConvertObjects<T>(Object.FindObjectsOfType(typeof(T)));
        }

        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public static Object FindObjectOfType(Type type)
        {
            Object[] array = Object.FindObjectsOfType(type);
            if (array.Length >0)
            {
                return array[0];
            }
            return null;
        }

        public static T FindObjectOfType<T>() where T : Object
        {
            return (T)((object)Object.FindObjectOfType(typeof(T)));
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern void DontDestroyOnLoad(Object target);//當加載場景時不銷燬

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern void DestroyObject(Object obj, [DefaultValue("0.0F")]float t);

        [ExcludeFromDocs]
        public static void DestroyObject(Object obj)
        {
            float t = 0f;
            Object.DestroyObject(obj, t);
        }

        [Obsolete("use Object.FindObjectsOfType instead."), WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern Object[] FindSceneObjectsOfType(Type type);

        [Obsolete("use Resources.FindObjectsOfTypeAll instead."), WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern Object[] FindObjectsOfTypeIncludingAssets(Type type);

        [Obsolete("Please use Resources.FindObjectsOfTypeAll instead")]
        public static Object[] FindObjectsOfTypeAll(Type type)
        {
            return Resources.FindObjectsOfTypeAll(type);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public override extern string ToString();

        public static implicit operator bool(Object exists)//判斷是否存在
        {
            return !Object.CompareBaseObjects(exists,null);
        }

        public static bool operator ==(Object x, Object y)
        {
            return Object.CompareBaseObjects(x, y);
        }

        public static bool operator !=(Object x, Object y)
        {
            return !Object.CompareBaseObjects(x, y);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章