Created
August 30, 2014 16:24
-
-
Save empika/6da96d03df0fe40f3886 to your computer and use it in GitHub Desktop.
Editor extension for OnConfirmCancel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEditor; | |
[CustomEditor (typeof(OnConfirmCancel), true)] | |
public class OnConfirmCancelEditor : Editor | |
{ | |
// | |
// Fields | |
// | |
private GUIContent[] m_EventTypes; | |
private GUIContent m_AddButonContent; | |
private GUIContent m_EventIDName; | |
private SerializedProperty m_DelegatesProperty; | |
private GUIContent m_IconToolbarMinus; | |
// | |
// Methods | |
// | |
private void OnAddNewSelected (object index) | |
{ | |
int enumValueIndex = (int)index; | |
this.m_DelegatesProperty.arraySize++; | |
SerializedProperty arrayElementAtIndex = this.m_DelegatesProperty.GetArrayElementAtIndex (this.m_DelegatesProperty.arraySize - 1); | |
SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative ("eventID"); | |
serializedProperty.enumValueIndex = enumValueIndex; | |
base.serializedObject.ApplyModifiedProperties (); | |
} | |
protected virtual void OnEnable () | |
{ | |
this.m_DelegatesProperty = base.serializedObject.FindProperty ("delegates"); | |
this.m_AddButonContent = new GUIContent ("Add New"); | |
this.m_EventIDName = new GUIContent (string.Empty); | |
this.m_IconToolbarMinus = new GUIContent ("-"); | |
this.m_IconToolbarMinus.image = null; | |
this.m_IconToolbarMinus.tooltip = "Remove selection from list"; | |
string[] names = Enum.GetNames (typeof(ConfirmCancelTriggerType)); | |
this.m_EventTypes = new GUIContent[names.Length]; | |
for (int i = 0; i < names.Length; i++) | |
{ | |
this.m_EventTypes [i] = new GUIContent (names [i]); | |
} | |
} | |
public override void OnInspectorGUI () | |
{ | |
base.serializedObject.Update (); | |
int num = -1; | |
EditorGUILayout.Space (); | |
Vector2 vector = EditorStyles.miniButton.CalcSize (this.m_IconToolbarMinus); | |
for (int i = 0; i < this.m_DelegatesProperty.arraySize; i++) | |
{ | |
SerializedProperty arrayElementAtIndex = this.m_DelegatesProperty.GetArrayElementAtIndex (i); | |
SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative ("eventID"); | |
SerializedProperty property = arrayElementAtIndex.FindPropertyRelative ("callback"); | |
this.m_EventIDName.text = serializedProperty.enumNames [serializedProperty.enumValueIndex]; | |
EditorGUILayout.PropertyField (property, this.m_EventIDName, new GUILayoutOption[0]); | |
Rect lastRect = GUILayoutUtility.GetLastRect (); | |
Rect position = new Rect (lastRect.xMax - vector.x - 2f, lastRect.y + 1f, vector.x, vector.y); | |
if (GUI.Button (position, this.m_IconToolbarMinus, EditorStyles.miniButton)) | |
{ | |
num = i; | |
} | |
EditorGUILayout.Space (); | |
} | |
if (num > -1) | |
{ | |
this.RemoveEntry (num); | |
} | |
Rect rect = GUILayoutUtility.GetRect (this.m_AddButonContent, GUI.skin.button); | |
rect.x += (rect.width - 200f) / 2f; | |
rect.width = 200f; | |
if (GUI.Button (rect, this.m_AddButonContent)) | |
{ | |
this.ShowAddTriggermenu (); | |
} | |
base.serializedObject.ApplyModifiedProperties (); | |
} | |
private void RemoveEntry (int toBeRemovedEntry) | |
{ | |
this.m_DelegatesProperty.DeleteArrayElementAtIndex (toBeRemovedEntry); | |
} | |
private void ShowAddTriggermenu () | |
{ | |
GenericMenu genericMenu = new GenericMenu (); | |
for (int i = 0; i < this.m_EventTypes.Length; i++) | |
{ | |
bool flag = true; | |
for (int j = 0; j < this.m_DelegatesProperty.arraySize; j++) | |
{ | |
SerializedProperty arrayElementAtIndex = this.m_DelegatesProperty.GetArrayElementAtIndex (j); | |
SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative ("eventID"); | |
if (serializedProperty.enumValueIndex == i) | |
{ | |
flag = false; | |
} | |
} | |
if (flag) | |
{ | |
genericMenu.AddItem (this.m_EventTypes [i], false, new GenericMenu.MenuFunction2 (this.OnAddNewSelected), i); | |
} | |
else | |
{ | |
genericMenu.AddDisabledItem (this.m_EventTypes [i]); | |
} | |
} | |
genericMenu.ShowAsContext (); | |
Event.current.Use (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment