public void AddProperty(SerializedProperty property) { // Check if this property actually belongs to the same direct child if (!property.GetRootPath().Equals(Parent)) { return; } if (reorderableListDict.ContainsKey(property.propertyPath)) { return; } var propList = new ReorderableList( property.serializedObject, property, draggable: true, displayHeader: false, displayAddButton: true, displayRemoveButton: true) { headerHeight = 5 }; propList.drawElementBackgroundCallback = (Rect position, int index, bool active, bool focused) => { if (DrawBackgroundCallback != null) { Rect backgroundRect = new Rect(position); if (index <= 0) { backgroundRect.yMin -= 8; } if (index >= propList.count - 1) { backgroundRect.yMax += 3; } EditorGUI.DrawRect(backgroundRect, DrawBackgroundCallback(active, focused)); } else { propList.drawElementBackgroundCallback = null; } }; propList.drawElementCallback = (Rect position, int index, bool active, bool focused) => { var iterProp = property.GetArrayElementAtIndex(index); var elementName = iterProp.displayName; if (ElementNameCallback != null) { elementName = ElementNameCallback(index); } position.xMin += 5; position.height = EditorGUIUtility.singleLineHeight; EasyGUI.PropertyField(position, iterProp, new GUIContent(elementName), ElementAttributes); }; propList.elementHeightCallback = index => ElementHeightCallback(property, index); reorderableListDict.Add(property.propertyPath, propList); }
private string DoHeader(InspectableProperty property, Rect position, string displayName) { displayName = string.IsNullOrEmpty(displayName) ? property.DisplayName : displayName; string headerName = string.Format(HeaderStr, displayName, property.ArraySize); EasyGUI.PropertyField(position, property, new GUIContent(headerName), false); return(headerName); }
private void DrawPropertySortableArray(Rect position, InspectableProperty property, GUIContent label) { // Try to get the sortable list this property belongs to ReorderableListData listData = null; if (listIndex.Count > 0) { listData = listIndex.Find(data => property.PropertyPath.StartsWith(data.Parent)); } UnityEditor.Editor scriptableEditor; bool isScriptableEditor = editableIndex.TryGetValue(property.PropertyPath, out scriptableEditor); // Has ReorderableList and Try to show the list if (listData != null && listData.DoProperty(position, property)) { } // Else try to draw ScriptableObject editor else if (isScriptableEditor) { bool hasHeader = property.HasAttribute <HeaderAttribute>(); bool hasSpace = property.HasAttribute <SpaceAttribute>(); hasSpace |= hasHeader; // No data in property, draw property field with create button if (scriptableEditor == null) { var fieldPosition = new Rect(position); var buttonPosition = new Rect(position); fieldPosition.width -= hasSpace ? 60 : 50; buttonPosition.xMin = fieldPosition.xMax; EasyGUI.PropertyField(fieldPosition, property); bool doCreate = GUI.Button(buttonPosition, Create, EditorStyles.miniButton); if (doCreate) { var propType = property.GetTypeReflection(); ScriptableObjectUtility.CreateAssetWithSavePrompt(propType, postCreated: createdAsset => { property.ObjectReference = createdAsset; property.IsExpanded = true; return(false); }); } } } else { EasyGUI.PropertyField(position, property, label, property.IsExpanded); } }
protected virtual void EmitPropertyField(Rect position, InspectableProperty targetInspectableProperty, GUIContent label) { var multiline = GetMultilineAttribute(); if (multiline == null) { var range = GetRangeAttribute(); if (range == null) { EasyGUI.PropertyField(position, targetInspectableProperty, label, includeChildren: true); } else { if (targetInspectableProperty.PropertyType == InspectablePropertyType.Float) { EasyGUI.Slider(position, targetInspectableProperty, range.Min, range.Max, label); } else if (targetInspectableProperty.PropertyType == InspectablePropertyType.Integer) { EasyGUI.IntSlider(position, targetInspectableProperty, (int)range.Min, (int)range.Max, label); } else { EditorGUI.LabelField(position, label.text, "Use Range with float or int."); } } } else { var property = targetInspectableProperty; label = EasyGUI.BeginProperty(position, label, property); var method = typeof(EditorGUI).GetMethod("MultiFieldPrefixLabel", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic); position = (Rect)method.Invoke(null, new object[] { position, 0, label, 1 }); EditorGUI.BeginChangeCheck(); int indentLevel = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; var stringValue = EditorGUI.TextArea(position, property.StringValue); EditorGUI.indentLevel = indentLevel; if (EditorGUI.EndChangeCheck()) { property.StringValue = stringValue; } EasyGUI.EndProperty(); } }
protected void DrawProperty(Rect position, SerializedProperty property) { // Try to get the sortable list this property belongs to var listData = GetReorderableListData(property); UnityEditor.Editor scriptableEditor; var isScriptableEditor = editableDict.TryGetValue(property.propertyPath, out scriptableEditor); // Has ReorderableList if (listData != null) { // Try to show the list if (!listData.DoProperty(position, property)) { var headerPosition = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight); EditorGUI.PropertyField(headerPosition, property, false); position.y += EditorGUIUtility.singleLineHeight; if (property.isExpanded) { EditorGUI.indentLevel++; IterateDrawProperty(position, property.Copy()); EditorGUI.indentLevel--; } } } // Else try to draw ScriptableObject editor else if (isScriptableEditor) { bool hasHeader = property.HasAttribute <HeaderAttribute>(); bool hasSpace = property.HasAttribute <SpaceAttribute>(); hasSpace |= hasHeader; // No data in property, draw property field with create button if (scriptableEditor == null) { var objPosition = new Rect(position); var btnPosition = new Rect(position); objPosition.xMax -= hasSpace ? 66 : 56; btnPosition.xMin = btnPosition.xMax - 56; EditorGUI.PropertyField(objPosition, property, false); var doCreate = GUI.Button(btnPosition, Create, EditorStyles.miniButton); if (doCreate) { var propType = property.GetTypeReflection(); ScriptableObjectUtility.CreateAssetWithSavePrompt(propType, postCreated: createdAsset => { property.objectReferenceValue = createdAsset; property.isExpanded = true; return(false); }); } } // Has data in property, draw foldout and editor else { EasyGUI.PropertyField(position, property, new GUIContent(property.displayName), true, null); } } else { var isStartProp = property.propertyPath.StartsWith(M_ScriptStr); if (isStartProp && IgnoreHeader) { } else { using (new EditorGUI.DisabledScope(isStartProp)) { EditorGUI.PropertyField(position, property, property.isExpanded); } } } }
public bool DoProperty(Rect position, InspectableProperty property) { if (!extendListIndex.ContainsKey(property.PropertyPath)) { return(false); } headerPosition = new Rect(position); headerPosition.height = EasyGUI.GetPropertyHeight(property, GUIContent.none, false); // Draw the background if (DrawBackgroundCallback != null) { Rect backgroundPosition = new Rect(headerPosition); if (property.IsExpanded) { backgroundPosition.yMax += 19; } EditorGUI.DrawRect(backgroundPosition, DrawBackgroundCallback(false, false)); } // Draw header if (HeaderCallback != null) { HeaderCallback(headerPosition); } else { string headerName = string.Format(Header, property.DisplayName, property.ArraySize); EasyGUI.PropertyField(headerPosition, property, new GUIContent(headerName), false); } // Draw the reorderable list for the property if (property.IsExpanded) { EditorGUI.BeginDisabledGroup(!Editable); if (!property.Editable) { EditorGUI.indentLevel++; } EditorGUI.BeginChangeCheck(); var sizePosition = new Rect(position); sizePosition.yMin = headerPosition.yMax; sizePosition.yMax = headerPosition.yMax + EditorGUIUtility.singleLineHeight; var newArraySize = Mathf.Clamp(EditorGUI.IntField(sizePosition, Size, property.ArraySize), 0, int.MaxValue); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(property.InspectableObject.SerializedObject.targetObject, SetArraySize); property.ArraySize = newArraySize; EditorUtility.SetDirty(property.InspectableObject.SerializedObject.targetObject); } var listPosition = new Rect(position); listPosition.xMin += 15; listPosition.yMin = sizePosition.yMax; extendListIndex[property.PropertyPath].DoList(listPosition); if (!property.Editable) { EditorGUI.indentLevel--; } EditorGUI.EndDisabledGroup(); } return(true); }
private static bool TryDrawObjectReference(Rect position, Object o, ReorderableListDrawer drawer = null) { var result = false; if (o != null) { var objectData = o.GetSerializedObjectData(); var headerPosition = new Rect(position); headerPosition.yMin = headerPosition.yMax - EditorGUIUtility.singleLineHeight; if (drawer != null) { drawer.IgnoreHeader = true; if (objectData.Foldout) { drawer.serializedObject.Update(); var listPosition = new Rect(headerPosition); listPosition.xMin += 15; listPosition.y += EditorGUIUtility.singleLineHeight; drawer.DrawPropertiesAll(listPosition); drawer.serializedObject.ApplyModifiedProperties(); } result = true; } else { var iterProp = objectData.Object.GetIterator(); position.yMin = headerPosition.yMax; position.height = 0f; EditorGUI.BeginChangeCheck(); if (iterProp.NextVisible(true)) { EditorGUI.indentLevel++; int depth = iterProp.depth; do { if (depth != iterProp.depth) { break; } if (iterProp.name.Equals(M_ScriptStr)) { continue; } if (objectData.Foldout) { var displayName = new GUIContent(iterProp.displayName); position.yMin += position.height; position.height = EasyGUI.GetPropertyHeight(iterProp, null, displayName, iterProp.isExpanded); EasyGUI.PropertyField(position, iterProp, displayName, iterProp.isExpanded, null); } result = true; } while (iterProp.NextVisible(false)); EditorGUI.indentLevel--; } if (EditorGUI.EndChangeCheck()) { objectData.Object.ApplyModifiedProperties(); EditorSceneManager.MarkAllScenesDirty(); } } if (result) { var indentLevel = EditorGUI.indentLevel; headerPosition.xMin += EasyGUI.Indent; headerPosition.width = 5; EditorGUI.indentLevel = 0; objectData.Foldout = EditorGUI.Foldout(headerPosition, objectData.Foldout, GUIContent.none, false); EditorGUI.indentLevel = indentLevel; } } return(result); }