protected override void PopulateContextMenu(GenericMenu menu, OverrideState overrideState, IExposedPropertyTable exposedPropertyTable, SerializedProperty exposedName, SerializedProperty defaultValue) { var propertyName = new PropertyName(exposedName.stringValue); OverrideState currentOverrideState; UnityEngine.Object currentValue = Resolve(new PropertyName(exposedName.stringValue), exposedPropertyTable, defaultValue.objectReferenceValue, out currentOverrideState); if (overrideState == OverrideState.DefaultValue) { menu.AddItem(new GUIContent(ExposePropertyContent.text), false, (userData) => { var guid = UnityEditor.GUID.Generate(); exposedName.stringValue = guid.ToString(); exposedName.serializedObject.ApplyModifiedProperties(); var newPropertyName = new PropertyName(exposedName.stringValue); Undo.RecordObject(exposedPropertyTable as Object, "Set Exposed Property"); exposedPropertyTable.SetReferenceValue(newPropertyName, currentValue); }, null); } else { menu.AddItem(UnexposePropertyContent, false, (userData) => { exposedName.stringValue = ""; exposedName.serializedObject.ApplyModifiedProperties(); Undo.RecordObject(exposedPropertyTable as Object, "Clear Exposed Property"); exposedPropertyTable.ClearReferenceValue(propertyName); }, null); } }
public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) { SerializedProperty serializedProperty = prop.FindPropertyRelative("defaultValue"); SerializedProperty serializedProperty2 = prop.FindPropertyRelative("exposedName"); string stringValue = serializedProperty2.stringValue; ExposedPropertyMode exposedPropertyMode = GetExposedPropertyMode(stringValue); Rect rect = position; rect.xMax -= kDriveWidgetWidth; Rect position2 = position; position2.x = rect.xMax; position2.width = kDriveWidgetWidth; IExposedPropertyTable exposedPropertyTable = GetExposedPropertyTable(prop); bool flag = exposedPropertyTable != null; PropertyName propertyName = new PropertyName(stringValue); OverrideState currentOverrideState = OverrideState.DefaultValue; Object currentReferenceValue = Resolve(propertyName, exposedPropertyTable, serializedProperty.objectReferenceValue, out currentOverrideState); Color color = GUI.color; //bool boldDefaultFont = false;//EditorGUIUtility.GetBoldDefaultFont(); Rect position3 = DrawLabel(flag, currentOverrideState, label, position, exposedPropertyTable, stringValue, serializedProperty2, serializedProperty); if (exposedPropertyMode == ExposedPropertyMode.DefaultValue || exposedPropertyMode == ExposedPropertyMode.NamedGUID) { OnRenderProperty(position3, propertyName, currentReferenceValue, serializedProperty, serializedProperty2, exposedPropertyMode, exposedPropertyTable); } else { position3.width /= 2f; EditorGUI.BeginChangeCheck(); stringValue = EditorGUI.TextField(position3, stringValue); if (EditorGUI.EndChangeCheck()) { serializedProperty2.stringValue = stringValue; } position3.x += position3.width; OnRenderProperty(position3, new PropertyName(stringValue), currentReferenceValue, serializedProperty, serializedProperty2, exposedPropertyMode, exposedPropertyTable); } GUI.color = color; //EditorGUIUtility.SetBoldDefaultFont(boldDefaultFont); if (flag && GUI.Button(position2, GUIContent.none, kDropDownStyle)) { GenericMenu genericMenu = new GenericMenu(); PopulateContextMenu(genericMenu, currentOverrideState, exposedPropertyTable, serializedProperty2, serializedProperty); genericMenu.ShowAsContext(); Event.current.Use(); } }
private Rect DrawLabel(bool showContextMenu, OverrideState currentOverrideState, GUIContent label, Rect position, IExposedPropertyTable exposedPropertyTable, string exposedNameStr, SerializedProperty exposedName, SerializedProperty defaultValue) { if (showContextMenu) { position.xMax -= kDriveWidgetWidth; } //EditorGUIUtility.SetBoldDefaultFont(currentOverrideState != OverrideState.DefaultValue); m_ModifiedLabel.text = label.text; m_ModifiedLabel.tooltip = label.tooltip; m_ModifiedLabel.image = label.image; if (!string.IsNullOrEmpty(m_ModifiedLabel.tooltip)) { m_ModifiedLabel.tooltip += "\n"; } if (currentOverrideState == OverrideState.MissingOverride) { GUI.color = kMissingOverrideColor; GUIContent modifiedLabel = m_ModifiedLabel; modifiedLabel.tooltip = modifiedLabel.tooltip + label.text + " " + NotFoundOn.text + " " + exposedPropertyTable + "."; } else if (currentOverrideState == OverrideState.Overridden && exposedPropertyTable != null) { GUIContent modifiedLabel = m_ModifiedLabel; modifiedLabel.tooltip = modifiedLabel.tooltip + OverridenByContent.text + exposedPropertyTable + "."; } Rect result = EditorGUI.PrefixLabel(position, m_ModifiedLabel, EditorStyles.boldLabel); if (exposedPropertyTable != null && Event.current.type == EventType.ContextClick && position.Contains(Event.current.mousePosition)) { GenericMenu genericMenu = new GenericMenu(); PopulateContextMenu(genericMenu, (!string.IsNullOrEmpty(exposedNameStr)) ? OverrideState.Overridden : OverrideState.DefaultValue, exposedPropertyTable, exposedName, defaultValue); genericMenu.ShowAsContext(); } return(result); }
Rect DrawLabel(bool showContextMenu, OverrideState currentOverrideState, GUIContent label, Rect position, IExposedPropertyTable exposedPropertyTable, string exposedNameStr, SerializedProperty exposedName, SerializedProperty defaultValue) { if (showContextMenu) { position.xMax = position.xMax - ExposedReferencePropertyDrawer.kDriveWidgetWidth; } EditorGUIUtility.SetBoldDefaultFont(currentOverrideState != OverrideState.DefaultValue); m_ModifiedLabel.text = label.text; m_ModifiedLabel.tooltip = label.tooltip; m_ModifiedLabel.image = label.image; if (!string.IsNullOrEmpty(m_ModifiedLabel.tooltip)) { m_ModifiedLabel.tooltip += "\n"; } if (currentOverrideState == OverrideState.MissingOverride) { GUI.color = kMissingOverrideColor; m_ModifiedLabel.tooltip += label.text + " " + NotFoundOn.text + " " + exposedPropertyTable + "."; } else if (currentOverrideState == OverrideState.Overridden && exposedPropertyTable != null) { m_ModifiedLabel.tooltip += OverridenByContent.text + exposedPropertyTable + "."; } var prefixRect = EditorGUI.PrefixLabel(position, m_ModifiedLabel); // Show contextual menu if (exposedPropertyTable != null && Event.current.type == EventType.ContextClick) { if (position.Contains(Event.current.mousePosition)) { GenericMenu menu = new GenericMenu(); PopulateContextMenu(menu, string.IsNullOrEmpty(exposedNameStr) ? OverrideState.DefaultValue : OverrideState.Overridden, exposedPropertyTable, exposedName, defaultValue); menu.ShowAsContext(); } } return(prefixRect); }
protected Object Resolve(PropertyName exposedPropertyName, IExposedPropertyTable exposedPropertyTable, Object defaultValue, out OverrideState currentOverrideState) { Object @object = null; bool idValid = false; bool flag = !PropertyName.IsNullOrEmpty(exposedPropertyName); currentOverrideState = OverrideState.DefaultValue; if (exposedPropertyTable != null) { @object = exposedPropertyTable.GetReferenceValue(exposedPropertyName, out idValid); if (idValid) { currentOverrideState = OverrideState.Overridden; } else if (flag) { currentOverrideState = OverrideState.MissingOverride; } } return((currentOverrideState == OverrideState.Overridden) ? @object : defaultValue); }
public override void OnGUI(Rect position, UnityEditor.SerializedProperty prop, GUIContent label) { SerializedProperty defaultValue = prop.FindPropertyRelative("defaultValue"); SerializedProperty exposedName = prop.FindPropertyRelative("exposedName"); var exposedNameStr = exposedName.stringValue; // 是默认值,GUID,还是名称 var propertyMode = GetExposedPropertyMode(exposedNameStr); Rect propertyFieldPosition = position; propertyFieldPosition.xMax = propertyFieldPosition.xMax - ExposedReferencePropertyDrawer.kDriveWidgetWidth; Rect driveFieldPosition = position; driveFieldPosition.x = propertyFieldPosition.xMax; driveFieldPosition.width = ExposedReferencePropertyDrawer.kDriveWidgetWidth; var exposedPropertyTable = GetExposedPropertyTable(prop); bool showContextMenu = exposedPropertyTable != null; var propertyName = new PropertyName(exposedNameStr); OverrideState currentOverrideState = OverrideState.DefaultValue; var currentReferenceValue = Resolve(propertyName, exposedPropertyTable, defaultValue.objectReferenceValue, out currentOverrideState); var previousColor = GUI.color; var wasBoldDefaultFont = EditorGUIUtility.GetBoldDefaultFont(); var valuePosition = DrawLabel(showContextMenu, currentOverrideState, label, position, exposedPropertyTable, exposedNameStr, exposedName, defaultValue); if (propertyMode == ExposedPropertyMode.DefaultValue || propertyMode == ExposedPropertyMode.NamedGUID) { OnRenderProperty(valuePosition, propertyName, currentReferenceValue, defaultValue, exposedName, propertyMode, exposedPropertyTable); } else { valuePosition.width /= 2; EditorGUI.BeginChangeCheck(); exposedNameStr = EditorGUI.TextField(valuePosition, exposedNameStr); if (EditorGUI.EndChangeCheck()) { exposedName.stringValue = exposedNameStr; } valuePosition.x += valuePosition.width; OnRenderProperty(valuePosition, new PropertyName(exposedNameStr), currentReferenceValue, defaultValue, exposedName, propertyMode, exposedPropertyTable); } GUI.color = previousColor; EditorGUIUtility.SetBoldDefaultFont(wasBoldDefaultFont); if (showContextMenu && GUI.Button(driveFieldPosition, GUIContent.none, kDropDownStyle)) { GenericMenu menu = new GenericMenu(); PopulateContextMenu(menu, currentOverrideState, exposedPropertyTable, exposedName, defaultValue); menu.ShowAsContext(); Event.current.Use(); } }
protected abstract void PopulateContextMenu(GenericMenu menu, OverrideState overrideState, IExposedPropertyTable exposedPropertyTable, SerializedProperty exposedName, SerializedProperty defaultValue);
protected Object Resolve(PropertyName exposedPropertyName, IExposedPropertyTable exposedPropertyTable, Object defaultValue, out OverrideState currentOverrideState) { Object objReference = null; bool propertyFoundInTable = false; var propertyIsNamed = !PropertyName.IsNullOrEmpty(exposedPropertyName); currentOverrideState = OverrideState.DefaultValue; if (exposedPropertyTable != null) { objReference = exposedPropertyTable.GetReferenceValue(exposedPropertyName, out propertyFoundInTable); if (propertyFoundInTable) { currentOverrideState = OverrideState.Overridden; } else if (propertyIsNamed) { currentOverrideState = OverrideState.MissingOverride; } } return(currentOverrideState == OverrideState.Overridden ? objReference : defaultValue); }
protected virtual void SetOverrideValue(int stateIndex, ref OverrideState[] overrideState, OverrideState stateValue) { overrideState[stateIndex] = stateValue; }