Example #1
0
    void Awake()
    {
        bool b = gameObject.activeInHierarchy;

        if (b)
        {
            gameObject.SetActive(false);
        }

#if (UNITY_EDITOR && !DISABLE_ILRUNTIME) || (!UNITY_EDITOR && !UNITY_STANDALONE_WIN) || FOCE_ENABLE_ILRUNTIME
        IType type = ILRuntimeManager.GetScriptType(script);
        if (type != null)
        {
            ILTypeInstance instance = new ILTypeInstance(type as ILType, false);;
            if (instance != null)
            {
                for (int i = 0; i < fields.Count; ++i)
                {
                    int index;
                    var field = instance.Type.GetField(fields[i].name, out index);
                    if (field != null)
                    {
                        var fieldType = field.TypeForCLR;
                        if (fieldType.IsPrimitive)
                        {
                            if (fieldType == typeof(sbyte))
                            {
                                sbyte val;
                                if (!sbyte.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(short))
                            {
                                short val;
                                if (!short.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(int))
                            {
                                int val;
                                if (!int.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(long))
                            {
                                long val;
                                if (!long.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(byte))
                            {
                                byte val;
                                if (!byte.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(ushort))
                            {
                                ushort val;
                                if (!ushort.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(uint))
                            {
                                uint val;
                                if (!uint.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(ulong))
                            {
                                ulong val;
                                if (!ulong.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(float))
                            {
                                float val;
                                if (!float.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(double))
                            {
                                double val;
                                if (!double.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(bool))
                            {
                                bool val;
                                if (!bool.TryParse(fields[i].value, out val))
                                {
                                    val = false;
                                }
                                instance[index] = val;
                            }
                            else if (fieldType == typeof(char))
                            {
                                char val;
                                if (!char.TryParse(fields[i].value, out val))
                                {
                                    val = ' ';
                                }
                                instance[index] = val;
                            }
                            //else
                            //throw new System.NotImplementedException();
                        }
                        else
                        {
                            if (!typeof(UnityEngine.Object).IsAssignableFrom(fieldType))
                            {
                                if (fieldType == typeof(string))
                                {
                                    instance[index] = fields[i].value;
                                }
                                else if (fieldType == typeof(UnityEngine.Vector2))
                                {
                                    instance[index] = UnityHelper.ParseVector2(fields[i].value);
                                }
                                else if (fieldType == typeof(UnityEngine.Vector3))
                                {
                                    instance[index] = UnityHelper.ParseVector3(fields[i].value);
                                }
                                else if (fieldType == typeof(UnityEngine.Vector4))
                                {
                                    instance[index] = UnityHelper.ParseVector4(fields[i].value);
                                }
                            }
                        }
                    }
                }


                for (int i = 0; i < objFields.Count; ++i)
                {
                    int index;
                    var field = instance.Type.GetField(objFields[i].name, out index);
                    if (field != null)
                    {
                        var fieldType = field.TypeForCLR;
                        if (!fieldType.IsPrimitive)
                        {
                            if (typeof(UnityEngine.Object).IsAssignableFrom(fieldType))
                            {
                                instance[index] = objFields[i];
                            }
                        }
                    }
                }

                var adptor = gameObject.AddComponent <MonoBehaviourAdapter.Adaptor>();
                adptor.enabled    = enabled;
                adptor.AppDomain  = ILRuntimeManager.app;
                adptor.ILInstance = instance;
            }
        }
#else
        Type      type     = ILRuntimeManager.assembly.GetType(script);
        Component instance = gameObject.AddComponent(type);

        if (instance != null)
        {
            for (int i = 0; i < fields.Count; ++i)
            {
                var field = type.GetField(fields[i].name);
                if (field != null)
                {
                    var fieldType = field.FieldType;
                    if (fieldType.IsPrimitive)
                    {
                        if (fieldType == typeof(sbyte))
                        {
                            sbyte val;
                            if (!sbyte.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(short))
                        {
                            short val;
                            if (!short.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(int))
                        {
                            int val;
                            if (!int.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(long))
                        {
                            long val;
                            if (!long.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(byte))
                        {
                            byte val;
                            if (!byte.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(ushort))
                        {
                            ushort val;
                            if (!ushort.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(uint))
                        {
                            uint val;
                            if (!uint.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(ulong))
                        {
                            ulong val;
                            if (!ulong.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(float))
                        {
                            float val;
                            if (!float.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(double))
                        {
                            double val;
                            if (!double.TryParse(fields[i].value, out val))
                            {
                                val = 0;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(bool))
                        {
                            bool val;
                            if (!bool.TryParse(fields[i].value, out val))
                            {
                                val = false;
                            }
                            field.SetValue(instance, val);
                        }
                        else if (fieldType == typeof(char))
                        {
                            char val;
                            if (!char.TryParse(fields[i].value, out val))
                            {
                                val = ' ';
                            }
                            field.SetValue(instance, val);
                        }
                        //else
                        //throw new System.NotImplementedException();
                    }
                    else
                    {
                        if (!typeof(UnityEngine.Object).IsAssignableFrom(fieldType))
                        {
                            if (fieldType == typeof(string))
                            {
                                field.SetValue(instance, fields[i].value);
                            }
                            else if (fieldType == typeof(UnityEngine.Vector2))
                            {
                                field.SetValue(instance, UnityHelper.ParseVector2(fields[i].value));
                            }
                            else if (fieldType == typeof(UnityEngine.Vector3))
                            {
                                field.SetValue(instance, UnityHelper.ParseVector2(fields[i].value));
                            }
                            else if (fieldType == typeof(UnityEngine.Vector4))
                            {
                                field.SetValue(instance, UnityHelper.ParseVector2(fields[i].value));
                            }
                        }
                    }
                }
            }


            for (int i = 0; i < objFields.Count; ++i)
            {
                var field = type.GetField(objFields[i].name);
                if (field != null)
                {
                    var fieldType = field.FieldType;
                    if (!fieldType.IsPrimitive)
                    {
                        if (typeof(UnityEngine.Object).IsAssignableFrom(fieldType))
                        {
                            field.SetValue(instance, objFields[i]);
                        }
                    }
                }
            }

            type.GetProperty("enabled").SetValue(instance, enabled, null);
        }
#endif
        if (b)
        {
            gameObject.SetActive(true);
        }

        TimerManager.Instance.AddFarmeTimer(1, delayDestory);
    }
    void Awake()
    {
        IType itype = ILRuntimeManager.GetScriptType(script);

        if (itype != null)
        {
            ILTypeInstance instance = new ILTypeInstance(itype as ILType, false);;
            if (instance != null)
            {
                for (int i = 0; i < fields.Count; ++i)
                {
                    int index;
                    var type = instance.Type.GetField(fields[i].name, out index);
                    if (type != null)
                    {
                        var cType = type.TypeForCLR;
                        if (cType.IsPrimitive)
                        {
                            if (cType == typeof(sbyte))
                            {
                                sbyte val;
                                if (!sbyte.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(short))
                            {
                                short val;
                                if (!short.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(int))
                            {
                                int val;
                                if (!int.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(long))
                            {
                                long val;
                                if (!long.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(byte))
                            {
                                byte val;
                                if (!byte.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(ushort))
                            {
                                ushort val;
                                if (!ushort.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(uint))
                            {
                                uint val;
                                if (!uint.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(ulong))
                            {
                                ulong val;
                                if (!ulong.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(float))
                            {
                                float val;
                                if (!float.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(double))
                            {
                                double val;
                                if (!double.TryParse(fields[i].value, out val))
                                {
                                    val = 0;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(bool))
                            {
                                bool val;
                                if (!bool.TryParse(fields[i].value, out val))
                                {
                                    val = false;
                                }
                                instance[index] = val;
                            }
                            else if (cType == typeof(char))
                            {
                                char val;
                                if (!char.TryParse(fields[i].value, out val))
                                {
                                    val = ' ';
                                }
                                instance[index] = val;
                            }
                            //else
                            //throw new System.NotImplementedException();
                        }
                        else
                        {
                            if (!typeof(UnityEngine.Object).IsAssignableFrom(cType))
                            {
                                if (cType == typeof(string))
                                {
                                    instance[index] = fields[i].value;
                                }
                                else if (cType == typeof(UnityEngine.Vector2))
                                {
                                    instance[index] = UnityHelper.ParseVector2(fields[i].value);
                                }
                                else if (cType == typeof(UnityEngine.Vector3))
                                {
                                    instance[index] = UnityHelper.ParseVector3(fields[i].value);
                                }
                                else if (cType == typeof(UnityEngine.Vector4))
                                {
                                    instance[index] = UnityHelper.ParseVector4(fields[i].value);
                                }
                            }
                        }
                    }
                }


                for (int i = 0; i < objFields.Count; ++i)
                {
                    int index;
                    var type = instance.Type.GetField(objFields[i].name, out index);
                    if (type != null)
                    {
                        var cType = type.TypeForCLR;
                        if (!cType.IsPrimitive)
                        {
                            if (typeof(UnityEngine.Object).IsAssignableFrom(cType))
                            {
                                instance[index] = objFields[i];
                            }
                        }
                    }
                }

                var adptor = gameObject.AddComponent <MonoBehaviourAdapter.Adaptor>();
                adptor.enabled    = enabled;
                adptor.AppDomain  = ILRuntimeManager.app;
                adptor.ILInstance = instance;
                if (gameObject.activeInHierarchy)
                {
                    adptor.Awake();
                    if (adptor.enabled)
                    {
                        adptor.OnEnable();
                    }
                }
            }
        }

        TimerManager.Instance.AddFarmeTimer(1, delayDestory);
    }