예제 #1
0
        public static object Create(Type type)
        {
            object instance = null;

            if (TypeTool.IsCreateInstance(type) == false)
            {
                return(instance);
            }

            instance = CreateCustomType(type);

            if (instance != null)
            {
                return(instance);
            }

            if (type.IsValueType == true)//如果是值类型 结构体
            {
                instance = Activator.CreateInstance(type, true);

                return(instance);
            }

            if (IsNoArgumentsConstructor(type) == true)
            {
                instance = Activator.CreateInstance(type, true);

                return(instance);
            }

            ConstructorInfo constructorInfo = GetMinConstructor(type);

            ParameterInfo[] parameterInfos = constructorInfo.GetParameters();

            object[] parameters = new object[parameterInfos.Length];

            for (int i = 0; i < parameterInfos.Length; i++)
            {
                parameters[i] = Create(parameterInfos[i].ParameterType);
            }

            instance = constructorInfo.Invoke(parameters);

            if (instance == null)
            {
                Debug.LogError("创建实例失败 :" + type.ToString());
            }

            return(instance);
        }
예제 #2
0
        public static void Input(Type type, string describe, ref object value, ref bool isChange)
        {
            if (type == typeof(int))
            {
                InputInt(describe, ref value, ref isChange);
            }
            else if (type == typeof(string))
            {
                InputString(describe, ref value, ref isChange);
            }
            else if (type == typeof(float))
            {
                InputFloat(describe, ref value, ref isChange);
            }
            else if (type == typeof(sbyte))
            {
                InputSbyte(describe, ref value, ref isChange);
            }
            else if (type == typeof(byte))
            {
                InputByte(describe, ref value, ref isChange);
            }
            else if (type == typeof(short))
            {
                InputShort(describe, ref value, ref isChange);
            }
            else if (type == typeof(ushort))
            {
                InputUshort(describe, ref value, ref isChange);
            }
            else if (type == typeof(long))
            {
                InputLong(describe, ref value, ref isChange);
            }
            else if (type == typeof(ulong))
            {
                InputUlong(describe, ref value, ref isChange);
            }
            else if (type == typeof(double))
            {
                InputDouble(describe, ref value, ref isChange);
            }
            else if (type == typeof(decimal))
            {
                InputDecimal(describe, ref value, ref isChange);
            }
            else if (type == typeof(uint))
            {
                InputUint(describe, ref value, ref isChange);
            }
            else if (type == typeof(bool))
            {
                InputBool(describe, ref value, ref isChange);
            }
            else if (type == typeof(char))
            {
                InputChar(describe, ref value, ref isChange);
            }
            else if (TypeTool.IsComponent(type) || type == typeof(GameObject))
            {
                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.LabelField(describe);

                UnityEngine.Object newValue = EditorGUILayout.ObjectField(value as UnityEngine.Object, type, true);
                if (newValue != value as UnityEngine.Object)
                {
                    isChange = true;
                    value    = newValue;
                }

                EditorGUILayout.EndHorizontal();
            }
            else if (type == typeof(Vector2))
            {
                Vector2 newValue = EditorGUILayout.Vector2Field(describe, (Vector2)value);
                if (newValue != (Vector2)value)
                {
                    isChange = true;
                    value    = newValue;
                }
            }
            else if (type == typeof(Vector3))
            {
                Vector3 newValue = EditorGUILayout.Vector3Field(describe, (Vector3)value);
                if (newValue != (Vector3)value)
                {
                    isChange = true;
                    value    = newValue;
                }
            }
            else if (type == typeof(Vector4))
            {
                Vector4 newValue = EditorGUILayout.Vector4Field(describe, (Vector4)value);
                if (newValue != (Vector4)value)
                {
                    isChange = true;
                    value    = newValue;
                }
            }
            else if (type == typeof(Color))
            {
                Color newValue = EditorGUILayout.ColorField(describe, (Color)value);
                if (newValue != (Color)value)
                {
                    isChange = true;
                    value    = newValue;
                }
            }
            else if (type.IsEnum)
            {
                Enum a = value as Enum;

                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.LabelField(describe);

                Enum newValue = EditorGUILayout.EnumPopup(a);
                if (newValue != a)
                {
                    isChange = true;
                    value    = newValue;
                }

                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.LabelField(describe + " 尚未解析该类型 :" + type.Name);

                EditorGUILayout.EndHorizontal();
            }
        }
예제 #3
0
        public ObjectData(object value, Type type, string name, GetValueHandle getValueHandler, SetValueHandle setValueHandler, bool isInit, int index)
        {
            this.value           = value;
            this.type            = type;
            this.name            = name;
            this.getValueHandler = getValueHandler;
            this.setValueHandler = setValueHandler;
            level = index;

            originalValue = value;

            if (isInit)
            {
                Init();
            }

            //支持类型
            if (TypeTool.IsSupported(type))
            {
                return;
            }

            //定制化类型
            if (type.IsArray && type.GetArrayRank() == 1)
            {
                arrayData = new ArrayData(this.value, this.type, this.name, this.getValueHandler, this.setValueHandler);
                return;
            }
            if (TypeTool.IsList(type))
            {
                listData = new ListData(this.value, this.type, this.name, this.getValueHandler, this.setValueHandler);
                return;
            }

            //不进一步解析Unity自带类型
            if (type.Namespace != null && type.Namespace.Contains("UnityEngine"))
            {
                return;
            }

            if (type.IsArray)
            {
                //不解析非一维数组
            }
            else if (TypeTool.IsDictionary(type))
            {
                //不解析字典
                dictionaryData = new DictionaryData(this.value, this.type, this.name, this.getValueHandler, this.setValueHandler);
            }
            else if (type == typeof(object))
            {
                //不解析object
            }
            else
            {
                //最多迭代10层
                if (level >= 10)
                {
                    return;
                }

                //Debug.LogError(name);
                //Debug.LogError(type.Name);
                classData = new ClassData(this.value, this.type, level + 1);
            }
        }