/// <summary> /// Draws the object. /// </summary> public static void DrawObject(object obj, bool drawHeader, bool friendlyNamespacePrefix, UnityEngine.Object target, bool drawNoFieldsNotice, Action changeCallback) { if (obj == null) { return; } if (drawHeader) { EditorGUILayout.LabelField(DisplayTypeName(obj.GetType(), friendlyNamespacePrefix), EditorStyles.boldLabel); } EditorGUI.BeginChangeCheck(); var inspectorDrawer = InspectorDrawerUtility.InspectorDrawerForType(obj.GetType()); if (inspectorDrawer != null) { inspectorDrawer.OnInspectorGUI(obj, target); } else { ObjectInspector.DrawFields(obj, drawNoFieldsNotice); } if (EditorGUI.EndChangeCheck()) { InspectorUtility.RecordUndoDirtyObject(target, "Change Value"); if (changeCallback != null) { changeCallback(); } } }
/// <summary> /// The object has been enabled again. /// </summary> public static void OnEnable() { // Start with a fresh cache. s_ObjectTypes.Clear(); s_CanAddMultipleTypes.Clear(); InspectorDrawerUtility.OnEnable(); }
/// <summary> /// Draws the specified object. /// </summary> /// <param name="guiContent">The GUIContent to draw with the associated object.</param> /// <param name="type">The type of object to draw.</param> /// <param name="value">The value of the object.</param> /// <param name="name">The name of the object being drawn.</param> /// <param name="hashPrefix">The prefix of the hash from the parent class. This value will prevent collisions with similarly named objects.</param> /// <param name="valuePositionMap">A map between the value hash and the position within the positions array.</param> /// <param name="serialization">The serialized data.</param> /// <param name="fieldProperty">A reference to the field or property that is being drawn.</param> /// <param name="drawFields">Should the fields be drawn? If false the properties will be drawn.</param> /// <returns>The drawn object.</returns> private static object DrawSingleObject(GUIContent guiContent, Type type, object value, string name, int hashPrefix, Dictionary <int, int> valuePositionMap, Serialization serialization, object fieldProperty, bool drawFields) { if (type == typeof(int)) { return(EditorGUILayout.IntField(guiContent, (int)value)); } if (type == typeof(float)) { // The range attribute may be used instead. if (drawFields) { var field = (System.Reflection.FieldInfo)fieldProperty; var rangeAttribute = field.GetCustomAttributes(typeof(RangeAttribute), false) as RangeAttribute[]; if (rangeAttribute != null && rangeAttribute.Length > 0) { return(EditorGUILayout.Slider(guiContent, (float)value, rangeAttribute[0].min, rangeAttribute[0].max)); } } return(EditorGUILayout.FloatField(guiContent, (float)value)); } if (type == typeof(double)) { return(EditorGUILayout.FloatField(guiContent, Convert.ToSingle((double)value))); } if (type == typeof(long)) { return((long)EditorGUILayout.IntField(guiContent, Convert.ToInt32((long)value))); } if (type == typeof(bool)) { return(EditorGUILayout.Toggle(guiContent, (bool)value)); } if (type == typeof(string)) { return(EditorGUILayout.TextField(guiContent, (string)value)); } if (type == typeof(byte)) { return(Convert.ToByte(EditorGUILayout.IntField(guiContent, Convert.ToInt32(value)))); } if (type == typeof(Vector2)) { return(EditorGUILayout.Vector2Field(guiContent, (Vector2)value)); } if (type == typeof(Vector3)) { return(EditorGUILayout.Vector3Field(guiContent, (Vector3)value)); } if (type == typeof(Vector4)) { return(EditorGUILayout.Vector4Field(guiContent, (Vector4)value)); } if (type == typeof(Quaternion)) { var quaternion = (Quaternion)value; var vectorValue = Vector4.zero; vectorValue.Set(quaternion.x, quaternion.y, quaternion.z, quaternion.w); vectorValue = EditorGUILayout.Vector4Field(name, vectorValue); quaternion.Set(vectorValue.x, vectorValue.y, vectorValue.z, vectorValue.w); return(quaternion); } if (type == typeof(Color)) { return(EditorGUILayout.ColorField(guiContent, (Color)value)); } if (type == typeof(Rect)) { return(EditorGUILayout.RectField(guiContent, (Rect)value)); } if (type == typeof(Matrix4x4)) { var matrix = (Matrix4x4)value; if (InspectorUtility.Foldout(value, guiContent)) { EditorGUI.indentLevel++; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { matrix[i, j] = EditorGUILayout.FloatField("E" + i.ToString() + j.ToString(), matrix[i, j]); } } EditorGUI.indentLevel--; } return(matrix); } if (type == typeof(AnimationCurve)) { if (value == null) { value = AnimationCurve.EaseInOut(0, 0, 1, 1); GUI.changed = true; } return(EditorGUILayout.CurveField(guiContent, (AnimationCurve)value)); } if (type == typeof(LayerMask)) { return(DrawLayerMask(guiContent, (LayerMask)value)); } if (type.IsEnum) { if (type.IsDefined(typeof(FlagsAttribute), true)) { return(EditorGUILayout.MaskField(guiContent, (int)value, Enum.GetNames(type))); } return(EditorGUILayout.EnumPopup(guiContent, (Enum)Enum.ToObject(type, value))); } if (typeof(UnityEngine.Object).IsAssignableFrom(type)) { return(EditorGUILayout.ObjectField(guiContent, (UnityEngine.Object)value, type, true)); } if (type.IsClass || (type.IsValueType && !type.IsPrimitive)) // Classes and structs. { if (typeof(Delegate).IsAssignableFrom(type)) // Delegates are not supported. { return(null); } if (s_DrawnObjects == null) { s_DrawnObjects = new HashSet <int>(); } // Do not endlessly loop on objects that have already been drawn. var hash = name.GetHashCode(); if (s_DrawnObjects.Contains(hash)) { return(null); } else { try { // Unity may throw an exception so catch it to clean up. s_DrawnObjects.Add(hash); GUILayout.BeginVertical(); if (value == null) { value = Activator.CreateInstance(type); GUI.changed = true; } if (InspectorUtility.Foldout(value, guiContent)) { EditorGUI.indentLevel++; var inspectorDrawer = InspectorDrawerUtility.InspectorDrawerForType(type); if (inspectorDrawer != null) { inspectorDrawer.OnInspectorGUI(value, null); } else if (drawFields) { value = DrawFields(value, true, hashPrefix + Serialization.StringHash(type.FullName) + Serialization.StringHash(name)); } else { value = DrawProperties(type, value, hashPrefix + Serialization.StringHash(type.FullName) + Serialization.StringHash(name), valuePositionMap, serialization, MemberVisibility.Public, null, null); } EditorGUI.indentLevel--; } GUILayout.EndVertical(); s_DrawnObjects.Remove(hash); return(value); } catch (Exception /*e*/) { // Clean up any exceptions. EditorGUI.indentLevel--; GUILayout.EndVertical(); s_DrawnObjects.Remove(hash); return(null); } } } Debug.LogWarning("Warning: unsupported value type: " + type); return(null); }