public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var n = property.FindPropertyRelative("presetName"); // Draw Name var lines = position.SplitByHeights((int)EditorGUIUtility.singleLineHeight); var nameBounds = lines[0].SplitByWidthsRatio(0.5f, 0.5f); var keysProp = property.FindPropertyRelative("keys"); List <string> presetKeys = new List <string>(); for (int i = 0; i < keysProp.arraySize; ++i) { presetKeys.Add(keysProp.GetArrayElementAtIndex(i).stringValue); } using (new ColorScope(Color.green, SwitcherInspector.IsPreset(presetKeys))) { using (new ColorScope(Color.red, n.stringValue.IsEmpty())) { if (GUI.Button(nameBounds[0], new GUIContent(n.stringValue))) { var script = property.serializedObject.targetObject as Switcher; script.SetPreset(n.stringValue); SwitcherInspector.SetActive(presetKeys.ToArray()); } EditorGUI.PropertyField(nameBounds[1], n, new GUIContent("")); } // indentation lines[1].x += 30; lines[1].width -= 30; var drawer = GetKeysDrawer(property); drawer.Draw(lines[1]); } }
private PropertyReorder <bool> GetVisibilityDrawer(SerializedProperty p) { var v = vPool.Get(p.propertyPath); if (v == null) { var visibility = p.FindPropertyRelative("visibility"); v = new PropertyReorder <bool>(visibility); v.drawItem = OnDrawVisibility; v.onAdd = i => OnAddObject(visibility, i); v.onRemove = i => OnRemoveObject(visibility, i); v.onReorder = (i1, i2) => OnReorderObject(visibility, i1, i2); v.canAdd = () => { var objs = p.serializedObject.FindProperty("objs"); return(!IsDuplicate(objs, Selection.activeObject)); }; vPool[p.propertyPath] = v; } return(v); void OnDrawVisibility(SerializedProperty item, Rect rect, int index, bool isActive, bool isFocused) { var union = SwitcherInspector.GetVisibilityUnion(item.serializedObject.targetObject as Switcher); using (new ContentColorScope(Color.gray, !union[index])) { var bounds = rect.SplitByWidths(25, 25); // draw index EditorGUI.PrefixLabel(bounds[0], new GUIContent(index.ToString())); // draw bool EditorGUI.PropertyField(bounds[1], item, new GUIContent("")); var objs = item.serializedObject.FindProperty("objs"); var obj = objs.GetArrayElementAtIndex(index); var oldObj = obj.objectReferenceValue; EditorGUI.PropertyField(bounds[2], obj, new GUIContent("")); if (obj.objectReferenceValue == null || IsDuplicate(objs, obj.objectReferenceValue)) { obj.objectReferenceValue = oldObj; } } } }
protected override void OnGUI(SerializedProperty p, Rect bound) { var uiSwitch = p.serializedObject.targetObject as Switcher; // Draw Name var bounds = bound.SplitByHeights(lineHeight); var n = p.FindPropertyRelative("name"); var active = SwitcherInspector.IsActive(n.stringValue); var nameBounds = bounds[0].SplitByWidthsRatio(0.5f, 0.25f, 0.25f); var boundsLeft = bounds[1]; // indentation boundsLeft.x += 30; boundsLeft.width -= 30; Color c = GUI.color; if (active) { c = UpdatePos(p) ? ChangedSelectedColor : SelectedColor; } // Draw Title using (new EnableScope(!n.stringValue.IsEmpty())) { using (new ColorScope(c)) { if (GUI.Button(nameBounds[0], new GUIContent(n.stringValue))) { Undo.RecordObjects(uiSwitch.objs.ToArray(), "switch"); bool hasPreset = !uiSwitch.preset.IsEmpty(); if (!hasPreset) { SwitcherInspector.SetActive(n.stringValue, n.stringValue); } else { SwitcherInspector.Activate(n.stringValue, !SwitcherInspector.IsActive(n.stringValue)); } var script = p.serializedObject.targetObject as Switcher; script.SetKey(n.stringValue); } } } EditorGUI.PropertyField(nameBounds[1], n, new GUIContent("")); if (GUI.Button(nameBounds[2], "Collect")) { uiSwitch.Collect(n.stringValue); EditorUtil.SetDirty(uiSwitch); } // Draw Visibility var visibility = GetVisibilityDrawer(p); var objBounds = boundsLeft.SplitByHeights((int)visibility.GetHeight()); boundsLeft = objBounds[1]; visibility.Draw(objBounds[0]); // Draw Transform if (uiSwitch.showTrans) { var trans = GetTransDrawer(p); var tBound = boundsLeft.SplitByHeights(transHeight); boundsLeft = tBound[1]; using (new ColorScope(c)) { trans.Draw(tBound[0]); } } // Draw Actions if (uiSwitch.showAction) { var actionBounds = boundsLeft.SplitByHeights(actionHeight); boundsLeft = actionBounds[1]; var actionProperty = p.FindPropertyRelative("action"); EditorGUI.PropertyField(actionBounds[0], actionProperty); } // Draw ICompData var dataBounds = boundsLeft.SplitByHeights(dataHeight); boundsLeft = dataBounds[1]; #if UNITY_2019_1_OR_NEWER var dataProperty = p.FindPropertyRelative("data"); EditorGUI.PropertyField(dataBounds[0], dataProperty, true); #endif }