Пример #1
0
 public virtual void OnInspectorGUI()
 {
     //EditorGUILayoutExtension.DrawFields(ObjectInspector.Instance.targetObject);
     EditorGUI.BeginDisabledGroup(true);
     EditorGUILayout.ObjectField("Script", Script, typeof(MonoScript), false);
     EditorGUI.EndDisabledGroup();
     foreach (var field in Fields)
     {
         EditorGUI.BeginChangeCheck();
         object value = EditorGUILayoutExtension.DrawField(field, field.GetValue(Target));
         if (EditorGUI.EndChangeCheck())
         {
             field.SetValue(Target, value);
             GUI.changed = true;
         }
     }
 }
        public static object DrawFields(object _obj, GUIContent guiContent)
        {
            if (_obj == null)
            {
                return(null);
            }

            List <FieldInfo> fields = Utility.GetFieldInfos(_obj.GetType());

            for (int j = 0; j < fields.Count; j++)
            {
                if (CanDraw(fields[j]))
                {
                    if (guiContent == null)
                    {
                        string name = fields[j].Name;
                        if (Utility.TryGetFieldInfoAttribute(fields[j], out TooltipAttribute tooltipAtt))
                        {
                            guiContent = new GUIContent(ObjectNames.NicifyVariableName(name), tooltipAtt.tooltip);
                        }
                        else
                        {
                            guiContent = new GUIContent(ObjectNames.NicifyVariableName(name));
                        }
                    }
                    EditorGUI.BeginChangeCheck();

                    object value = EditorGUILayoutExtension.DrawField(guiContent, fields[j], fields[j].GetValue(_obj));
                    if (EditorGUI.EndChangeCheck())
                    {
                        fields[j].SetValue(_obj, value);
                        GUI.changed = true;
                    }
                    guiContent = null;
                }
            }
            return(_obj);
        }
Пример #3
0
        /// <summary> 绘制内部所有字段 </summary>
        public static object DrawFields(object _object)
        {
            if (_object == null)
            {
                return(null);
            }

            List <FieldInfo> fields = Utility_Reflection.GetFieldInfos(_object.GetType());

            for (int j = 0; j < fields.Count; j++)
            {
                if (CanDraw(fields[j]))
                {
                    EditorGUI.BeginChangeCheck();
                    object value = EditorGUILayoutExtension.DrawField(fields[j], fields[j].GetValue(_object));
                    if (EditorGUI.EndChangeCheck())
                    {
                        fields[j].SetValue(_object, value);
                        GUI.changed = true;
                    }
                }
            }
            return(_object);
        }
        private static object DrawArrayField(GUIContent _content, FieldInfo _fieldInfo, Type _fieldType, object _value)
        {
            Type elementType;

            if (_fieldType.IsArray)
            {
                elementType = _fieldType.GetElementType();
            }
            else
            {
                Type type2 = _fieldType;
                while (!type2.IsGenericType)
                {
                    type2 = type2.BaseType;
                }
                elementType = type2.GetGenericArguments()[0];
            }
            IList list;

            if (_value == null)
            {
                if (_fieldType.IsGenericType || _fieldType.IsArray)
                {
                    list = (Activator.CreateInstance(typeof(List <>).MakeGenericType(new Type[]
                    {
                        elementType
                    }), true) as IList);
                }
                else
                {
                    list = (Activator.CreateInstance(_fieldType, true) as IList);
                }
                if (_fieldType.IsArray)
                {
                    Array array = Array.CreateInstance(elementType, list.Count);
                    list.CopyTo(array, 0);
                    list = array;
                }
                GUI.changed = true;
            }
            else
            {
                list = (IList)_value;
            }
            if (EditorGUILayoutExtension.DrawFoldout(list.GetHashCode(), _content))
            {
                EditorGUI.indentLevel++;
                bool flag = list.GetHashCode() == EditorGUILayoutExtension.editingFieldHash;
                int  num  = (!flag) ? list.Count : EditorGUILayoutExtension.savedArraySize;
                int  num2 = EditorGUILayout.IntField("Size", num);
                if (flag && EditorGUILayoutExtension.editingArray && (GUIUtility.keyboardControl != EditorGUILayoutExtension.currentKeyboardControl || Event.current.keyCode == (KeyCode)13))
                {
                    if (num2 != list.Count)
                    {
                        Array array2 = Array.CreateInstance(elementType, num2);
                        int   num3   = -1;
                        for (int i = 0; i < num2; i++)
                        {
                            if (i < list.Count)
                            {
                                num3 = i;
                            }
                            if (num3 == -1)
                            {
                                break;
                            }
                            object value2 = list[num3];
                            if (i >= list.Count && !typeof(Object).IsAssignableFrom(elementType) && !typeof(string).IsAssignableFrom(elementType))
                            {
                                value2 = Activator.CreateInstance(list[num3].GetType(), true);
                            }
                            array2.SetValue(value2, i);
                        }
                        if (_fieldType.IsArray)
                        {
                            list = array2;
                        }
                        else
                        {
                            if (_fieldType.IsGenericType)
                            {
                                list = (Activator.CreateInstance(typeof(List <>).MakeGenericType(new Type[]
                                {
                                    elementType
                                }), true) as IList);
                            }
                            else
                            {
                                list = (Activator.CreateInstance(_fieldType, true) as IList);
                            }
                            for (int j = 0; j < array2.Length; j++)
                            {
                                list.Add(array2.GetValue(j));
                            }
                        }
                    }
                    EditorGUILayoutExtension.editingArray     = false;
                    EditorGUILayoutExtension.savedArraySize   = -1;
                    EditorGUILayoutExtension.editingFieldHash = -1;
                    GUI.changed = true;
                }
                else if (num2 != num)
                {
                    if (!EditorGUILayoutExtension.editingArray)
                    {
                        EditorGUILayoutExtension.currentKeyboardControl = GUIUtility.keyboardControl;
                        EditorGUILayoutExtension.editingArray           = true;
                        EditorGUILayoutExtension.editingFieldHash       = list.GetHashCode();
                    }
                    EditorGUILayoutExtension.savedArraySize = num2;
                }

                if (_fieldInfo != null &&
                    (Utility.TryGetFieldInfoAttribute(_fieldInfo, out ObjectDrawerAttribute att) ||
                     Utility.TryGetTypeAttribute(elementType, out att)))
                {
                    for (int k = 0; k < list.Count; k++)
                    {
                        FieldDrawer objectDrawer;
                        if ((objectDrawer = ObjectDrawerUtility.GetObjectDrawer(att)) != null)
                        {
                            _content.text      = "Element " + k;
                            objectDrawer.Value = list[k];
                            objectDrawer.OnGUI(_content);
                            if (objectDrawer.Value != list[k])
                            {
                                list[k]     = objectDrawer.Value;
                                GUI.changed = true;
                            }
                        }
                    }
                }
                else
                {
                    for (int k = 0; k < list.Count; k++)
                    {
                        _content.text = "Element " + k;
                        list[k]       = EditorGUILayoutExtension.DrawField(_content, elementType, list[k]);
                    }
                }
                EditorGUI.indentLevel--;
            }
            return(list);
        }
Пример #5
0
        private static object DrawArrayField(GUIContent _content, FieldInfo _fieldInfo, Type _fieldType, object _value)
        {
            Type elementType;

            if (_fieldType.IsArray)
            {
                elementType = _fieldType.GetElementType();
            }
            else
            {
                Type type2 = _fieldType;
                while (!type2.IsGenericType)
                {
                    type2 = type2.BaseType;
                }
                elementType = type2.GetGenericArguments()[0];
            }
            IList list;

            if (_value == null)
            {
                if (_fieldType.IsGenericType || _fieldType.IsArray)
                {
                    _value = list = (Activator.CreateInstance(typeof(List <>).MakeGenericType(new Type[]
                    {
                        elementType
                    }), true) as IList);
                }
                else
                {
                    _value = list = (Activator.CreateInstance(_fieldType, true) as IList);
                }
                if (_fieldType.IsArray)
                {
                    Array array = Array.CreateInstance(elementType, list.Count);
                    list.CopyTo(array, 0);
                    _value = list = array;
                }
                GUI.changed = true;
            }
            else
            {
                list = (IList)_value;
            }
            if (EditorGUILayoutExtension.DrawFoldout(_value.GetHashCode(), _content))
            {
                EditorGUI.indentLevel++;
                bool flag     = _value.GetHashCode() == EditorGUILayoutExtension.editingFieldHash;
                int  count    = (!flag) ? list.Count : EditorGUILayoutExtension.savedArraySize;
                int  newCount = EditorGUILayout.IntField(EditorGUIExtension.GetGUIContent("Size"), count);
                if (flag && EditorGUILayoutExtension.editingArray && (GUIUtility.keyboardControl != EditorGUILayoutExtension.currentKeyboardControl ||
                                                                      (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return)))
                {
                    if (newCount != list.Count)
                    {
                        int currentCount = list.Count;
                        if (newCount > currentCount)
                        {
                            if (_fieldType.IsArray)
                            {
                                Array array2 = Array.CreateInstance(elementType, newCount);
                                int   num3   = -1;
                                for (int i = 0; i < newCount; i++)
                                {
                                    if (i < list.Count)
                                    {
                                        num3 = i;
                                    }
                                    if (num3 == -1)
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                Type type = list.Count > 0 ? list[list.Count - 1].GetType() : elementType;
                                if (!typeof(UnityObject).IsAssignableFrom(type))
                                {
                                    for (int i = currentCount; i < newCount; i++)
                                    {
                                        list.Add(Activator.CreateInstance(type, true));
                                    }
                                }
                                else
                                {
                                    for (int i = currentCount; i < newCount; i++)
                                    {
                                        list.Add(null);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (!_fieldType.IsArray)
                            {
                                while (list.Count > newCount)
                                {
                                    list.RemoveAt(list.Count - 1);
                                }
                            }
                        }
                    }
                    EditorGUILayoutExtension.editingArray     = false;
                    EditorGUILayoutExtension.savedArraySize   = -1;
                    EditorGUILayoutExtension.editingFieldHash = -1;
                    GUI.changed = true;
                }
                else if (newCount != count)
                {
                    if (!EditorGUILayoutExtension.editingArray)
                    {
                        EditorGUILayoutExtension.currentKeyboardControl = GUIUtility.keyboardControl;
                        EditorGUILayoutExtension.editingArray           = true;
                        EditorGUILayoutExtension.editingFieldHash       = _value.GetHashCode();
                    }
                    EditorGUILayoutExtension.savedArraySize = newCount;
                }

                if (_fieldInfo != null &&
                    (Utility_Attribute.TryGetFieldInfoAttribute(_fieldInfo, out FieldAttribute att) ||
                     Utility_Attribute.TryGetTypeAttribute(elementType, out att)))
                {
                    for (int k = 0; k < list.Count; k++)
                    {
                        FieldDrawer objectDrawer;
                        if ((objectDrawer = ObjectDrawerUtility.GetObjectDrawer(att)) != null)
                        {
                            _content.text      = "Element " + k;
                            objectDrawer.Value = list[k];
                            objectDrawer.OnGUI(_content);
                            if (objectDrawer.Value != list[k])
                            {
                                list[k]     = objectDrawer.Value;
                                GUI.changed = true;
                            }
                        }
                    }
                }
                else
                {
                    for (int k = 0; k < list.Count; k++)
                    {
                        _content.text = "Element " + k;
                        list[k]       = EditorGUILayoutExtension.DrawField(_content, elementType, list[k]);
                    }
                }
                EditorGUI.indentLevel--;
            }
            return(list);
        }