unity源码解析Object

有个工具获取unity源码,就是ILSPY,随便解压一个unity的游戏包,可以找到下列文件

unity源码解析Object_第1张图片

这些文件就是unity的编译的结果。把他们拖入ILSPY之后就可以看到这些里面到底干了些什么。

unity源码解析Object_第2张图片

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);
        }
    }
}

你可能感兴趣的:(unity源码解析Object)