Exemplo n.º 1
0
        void OnGUI()
        {
            EditorGUILayoutExtension.DrawFields(data);

            if (GUILayout.Button("绘制一个普通对象到Inspector"))
            {
                EditorGUILayoutExtension.DrawFieldsInInspector("Test", data);
            }
        }
        private static object DrawSingleField(GUIContent _content, FieldInfo _fieldInfo, Type _fieldType, object _value)
        {
            if ((Utility.TryGetFieldInfoAttribute(_fieldInfo, out ObjectDrawerAttribute att) ||
                 Utility.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, (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 = _value.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);
        }
Exemplo n.º 3
0
 public virtual void OnInspectorGUI()
 {
     EditorGUILayoutExtension.DrawFields(ObjectInspector.Instance.targetObject);
 }