Exemple #1
0
        private static object DrawSingleField(GUIContent _content, FieldInfo _fieldInfo, Type _fieldType, object _value)
        {
            if (Utility_Attribute.TryGetFieldInfoAttribute(_fieldInfo, out FieldAttribute att) ||
                Utility_Attribute.TryGetTypeAttribute(_fieldType, out att))
            {
                FieldDrawer objectDrawer;
                if ((objectDrawer = ObjectDrawerUtility.GetObjectDrawer(att)) != null)
                {
                    objectDrawer.Value     = _value;
                    objectDrawer.FieldInfo = _fieldInfo;
                    objectDrawer.OnGUI(_content);
                    return(_value);
                }
            }

            if (_fieldType.Equals(typeof(int)))
            {
                return(EditorGUILayout.IntField(_content, _value == null ? 0 : (int)_value, EditorStylesExtension.NumberFieldStyle));
            }
            if (_fieldType.Equals(typeof(float)))
            {
                return(EditorGUILayout.FloatField(_content, _value == null ? 0 : (float)_value, EditorStylesExtension.NumberFieldStyle));
            }
            if (_fieldType.Equals(typeof(double)))
            {
                return(EditorGUILayout.DoubleField(_content, Convert.ToSingle(_value == null ? 0 : (double)_value), EditorStylesExtension.NumberFieldStyle));
            }
            if (_fieldType.Equals(typeof(long)))
            {
                return((long)EditorGUILayout.IntField(_content, Convert.ToInt32(_value == null ? 0 : (long)_value), EditorStylesExtension.NumberFieldStyle));
            }
            if (_fieldType.Equals(typeof(bool)))
            {
                return(EditorGUILayout.Toggle(_content, _value == null ? false : (bool)_value));
            }
            if (_fieldType.Equals(typeof(string)))
            {
                return(EditorGUILayout.TextField(_content, _value == null ? "" : (string)_value, EditorStylesExtension.TextFieldStyle));
            }
            if (_fieldType.Equals(typeof(byte)))
            {
                return(Convert.ToByte(EditorGUILayout.IntField(_content, Convert.ToInt32(_value == null ? 0 : (byte)_value))));
            }
            if (_fieldType.Equals(typeof(Vector2)))
            {
                return(EditorGUILayout.Vector2Field(_content, _value == null ? Vector2.zero : (Vector2)_value));
            }
            if (_fieldType.Equals(typeof(Vector2Int)))
            {
                return(EditorGUILayout.Vector2IntField(_content, _value == null ? Vector2Int.zero : (Vector2Int)_value));
            }
            if (_fieldType.Equals(typeof(Vector3)))
            {
                return(EditorGUILayout.Vector3Field(_content, _value == null ? Vector3.zero : (Vector3)_value));
            }
            if (_fieldType.Equals(typeof(Vector3Int)))
            {
                return(EditorGUILayout.Vector3IntField(_content, _value == null ? Vector3Int.zero : (Vector3Int)_value));
            }
            if (_fieldType.Equals(typeof(Vector4)))
            {
                return(EditorGUILayout.Vector4Field(_content.text, _value == null ? Vector4.zero : (Vector4)_value));
            }
            if (_fieldType.Equals(typeof(Quaternion)))
            {
                Quaternion quaternion = _value == null ? Quaternion.identity : (Quaternion)_value;
                Vector4    vector     = Vector4.zero;
                vector.Set(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
                vector = EditorGUILayout.Vector4Field(_content.text, vector);
                quaternion.Set(vector.x, vector.y, vector.z, vector.w);
                return(quaternion);
            }
            if (_fieldType.Equals(typeof(Color)))
            {
                return(EditorGUILayout.ColorField(_content, _value == null ? Color.black : (Color)_value));
            }
            if (_fieldType.Equals(typeof(Rect)))
            {
                return(EditorGUILayout.RectField(_content, _value == null ? Rect.zero : (Rect)_value));
            }
            if (_fieldType.Equals(typeof(Gradient)))
            {
                return(EditorGUILayout.GradientField(_content, _value == null ? new Gradient() : (Gradient)_value));
            }
            if (_fieldType.Equals(typeof(Matrix4x4)))
            {
                GUILayout.BeginVertical(new GUILayoutOption[0]);
                if (EditorGUILayoutExtension.DrawFoldout(_content.text.GetHashCode(), _content))
                {
                    EditorGUI.indentLevel++;
                    Matrix4x4 matrix4x = _value == null ? Matrix4x4.identity : (Matrix4x4)_value;
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            EditorGUI.BeginChangeCheck();
                            matrix4x[i, j] = EditorGUILayout.FloatField("E" + i.ToString() + j.ToString(), matrix4x[i, j]);
                            if (EditorGUI.EndChangeCheck())
                            {
                                GUI.changed = true;
                            }
                        }
                    }
                    _value = matrix4x;
                    EditorGUI.indentLevel--;
                }
                GUILayout.EndVertical();
                return(_value);
            }
            if (_fieldType.Equals(typeof(AnimationCurve)))
            {
                if (_value == null)
                {
                    _value      = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
                    GUI.changed = true;
                }
                return(EditorGUILayout.CurveField(_content, (AnimationCurve)_value));
            }
            if (_fieldType.Equals(typeof(LayerMask)))
            {
                return(EditorGUILayoutExtension.DrawLayerMask(_content, (LayerMask)_value));
            }
            if (typeof(UnityObject).IsAssignableFrom(_fieldType))
            {
                return(EditorGUILayout.ObjectField(_content, (UnityObject)_value, _fieldType, true));
            }
            if (_fieldType.IsEnum)
            {
                return(EditorGUILayout.EnumPopup(_content, (Enum)_value));
            }
            if (_fieldType.IsClass || (_fieldType.IsValueType && !_fieldType.IsPrimitive))
            {
                if (typeof(Delegate).IsAssignableFrom(_fieldType))
                {
                    return(null);
                }
                if (typeof(object).IsAssignableFrom(_fieldType) && _value == null)
                {
                    return(null);
                }
                int hashCode = _value.GetHashCode();
                if (EditorGUILayoutExtension.drawnObjects.Contains(hashCode))
                {
                    return(null);
                }
                try
                {
                    EditorGUILayoutExtension.drawnObjects.Add(hashCode);
                    GUILayout.BeginVertical();
                    if (_value == null)
                    {
                        if (_fieldType.IsGenericType && _fieldType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            _fieldType = Nullable.GetUnderlyingType(_fieldType);
                        }
                        _value = Activator.CreateInstance(_fieldType, true);
                    }
                    if (EditorGUILayoutExtension.DrawFoldout(hashCode, _content))
                    {
                        EditorGUI.indentLevel++;
                        _value = EditorGUILayoutExtension.DrawFields(_value);
                        EditorGUI.indentLevel--;
                    }
                    EditorGUILayoutExtension.drawnObjects.Remove(hashCode);
                    GUILayout.EndVertical();
                    return(_value);
                }
                catch (Exception)
                {
                    GUILayout.EndVertical();
                    EditorGUILayoutExtension.drawnObjects.Remove(hashCode);
                    return(null);
                }
            }
            EditorGUILayout.LabelField("Unsupported Type: " + _fieldType);
            return(null);
        }
        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);
        }
Exemple #3
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);
        }
 private static object DrawSingleField(GUIContent _content, Type _fieldType, object _value)
 {
     if (_fieldType.Equals(typeof(int)))
     {
         return(EditorGUILayout.IntField(_content, (int)_value));
     }
     if (_fieldType.Equals(typeof(float)))
     {
         return(EditorGUILayout.FloatField(_content, (float)_value));
     }
     if (_fieldType.Equals(typeof(double)))
     {
         return(EditorGUILayout.FloatField(_content, Convert.ToSingle((double)_value)));
     }
     if (_fieldType.Equals(typeof(long)))
     {
         return((long)EditorGUILayout.IntField(_content, Convert.ToInt32((long)_value)));
     }
     if (_fieldType.Equals(typeof(bool)))
     {
         return(EditorGUILayout.Toggle(_content, (bool)_value));
     }
     if (_fieldType.Equals(typeof(string)))
     {
         return(EditorGUILayout.TextField(_content, (string)_value));
     }
     if (_fieldType.Equals(typeof(byte)))
     {
         return(Convert.ToByte(EditorGUILayout.IntField(_content, Convert.ToInt32(_value))));
     }
     if (_fieldType.Equals(typeof(Vector2)))
     {
         return(EditorGUILayout.Vector2Field(_content, (Vector2)_value));
     }
     if (_fieldType.Equals(typeof(Vector2Int)))
     {
         return(EditorGUILayout.Vector2IntField(_content, (Vector2Int)_value));
     }
     if (_fieldType.Equals(typeof(Vector3)))
     {
         return(EditorGUILayout.Vector3Field(_content, (Vector3)_value));
     }
     if (_fieldType.Equals(typeof(Vector3Int)))
     {
         return(EditorGUILayout.Vector3IntField(_content, (Vector3Int)_value));
     }
     if (_fieldType.Equals(typeof(Vector3)))
     {
         return(EditorGUILayout.Vector3Field(_content, (Vector3)_value));
     }
     if (_fieldType.Equals(typeof(Vector4)))
     {
         return(EditorGUILayout.Vector4Field(_content.text, (Vector4)_value));
     }
     if (_fieldType.Equals(typeof(Quaternion)))
     {
         Quaternion quaternion = (Quaternion)_value;
         Vector4    vector     = Vector4.zero;
         vector.Set(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
         vector = EditorGUILayout.Vector4Field(_content.text, vector);
         quaternion.Set(vector.x, vector.y, vector.z, vector.w);
         return(quaternion);
     }
     if (_fieldType.Equals(typeof(Color)))
     {
         return(EditorGUILayout.ColorField(_content, (Color)_value));
     }
     if (_fieldType.Equals(typeof(Rect)))
     {
         return(EditorGUILayout.RectField(_content, (Rect)_value));
     }
     if (_fieldType.Equals(typeof(Matrix4x4)))
     {
         GUILayout.BeginVertical(new GUILayoutOption[0]);
         if (EditorGUILayoutExtension.DrawFoldout(_content.text.GetHashCode(), _content))
         {
             EditorGUI.indentLevel++;
             Matrix4x4 matrix4x = (Matrix4x4)_value;
             for (int i = 0; i < 4; i++)
             {
                 for (int j = 0; j < 4; j++)
                 {
                     EditorGUI.BeginChangeCheck();
                     matrix4x[i, j] = EditorGUILayout.FloatField("E" + i.ToString() + j.ToString(), matrix4x[i, j]);
                     if (EditorGUI.EndChangeCheck())
                     {
                         GUI.changed = true;
                     }
                 }
             }
             _value = matrix4x;
             EditorGUI.indentLevel--;
         }
         GUILayout.EndVertical();
         return(_value);
     }
     if (_fieldType.Equals(typeof(AnimationCurve)))
     {
         if (_value == null)
         {
             _value      = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
             GUI.changed = true;
         }
         return(EditorGUILayout.CurveField(_content, (AnimationCurve)_value));
     }
     if (_fieldType.Equals(typeof(LayerMask)))
     {
         return(EditorGUILayoutExtension.DrawLayerMask(_content, (LayerMask)_value));
     }
     if (typeof(Object).IsAssignableFrom(_fieldType))
     {
         return(EditorGUILayout.ObjectField(_content, (Object)_value, _fieldType, true));
     }
     if (_fieldType.IsEnum)
     {
         return(EditorGUILayout.EnumPopup(_content, (Enum)_value));
     }
     if (_fieldType.IsClass || (_fieldType.IsValueType && !_fieldType.IsPrimitive))
     {
         if (typeof(Delegate).IsAssignableFrom(_fieldType))
         {
             return(null);
         }
         int hashCode = _content.text.GetHashCode();
         if (EditorGUILayoutExtension.drawnObjects.Contains(hashCode))
         {
             return(null);
         }
         try
         {
             EditorGUILayoutExtension.drawnObjects.Add(hashCode);
             GUILayout.BeginVertical(new GUILayoutOption[0]);
             if (_value == null)
             {
                 if (_fieldType.IsGenericType && _fieldType.GetGenericTypeDefinition() == typeof(Nullable <>))
                 {
                     _fieldType = Nullable.GetUnderlyingType(_fieldType);
                 }
                 _value = Activator.CreateInstance(_fieldType, true);
             }
             if (EditorGUILayoutExtension.DrawFoldout(hashCode, _content))
             {
                 EditorGUI.indentLevel++;
                 _value = EditorGUILayoutExtension.DrawFields(_value);
                 EditorGUI.indentLevel--;
             }
             EditorGUILayoutExtension.drawnObjects.Remove(hashCode);
             GUILayout.EndVertical();
             return(_value);
         }
         catch (Exception)
         {
             GUILayout.EndVertical();
             EditorGUILayoutExtension.drawnObjects.Remove(hashCode);
             return(null);
         }
     }
     EditorGUILayout.LabelField("Unsupported Type: " + _fieldType);
     return(null);
 }