private static int CheckCommand <T>(ref T selected) { var id = GUIUtility.GetControlID(FocusType.Passive); ObjectPickerWindow.TryGetPickedObject(id, out selected); return(id); }
////////////////////////////////////////////////////////////////////////// public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //Type.Get var typeNameProperty = property.FindPropertyRelative("m_TypeName"); // update selected var id = GUIUtility.GetControlID(FocusType.Passive); ObjectPickerWindow.TryGetPickedObject(id, out Type selected); if (selected != null) { //Debug.Log($"selected {selected.FullName}"); typeNameProperty.stringValue = selected.AssemblyQualifiedName; property.serializedObject.ApplyModifiedProperties(); } // select type btn GUI.Label(new Rect(position.x, position.y, EditorGUIUtility.labelWidth, position.height), property.displayName); if (GUI.Button(new Rect(position.x + EditorGUIUtility.labelWidth, position.y, position.width - EditorGUIUtility.labelWidth, position.height), getDisplayTypeName(typeNameProperty.stringValue))) { var typeList = new List <Type>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { filterTypes(assembly, typeList); } typeList.Sort((x, y) => string.CompareOrdinal(x.FullName, y.FullName)); ObjectPickerWindow.Show(id, null, typeList, 0, n => new GUIContent(n.FullName)); } ///////////////////////////////////// string getDisplayTypeName(string assemblyQualifiedName) { if (string.IsNullOrEmpty(assemblyQualifiedName)) { return(""); } return(assemblyQualifiedName.Remove(assemblyQualifiedName.IndexOf(','))); } void filterTypes(Assembly assembly, List <Type> output) { var filters = fieldInfo .GetCustomAttributes(typeof(TypeReferenceFilterAttribute), false) .Cast <TypeReferenceFilterAttribute>() .ToList(); foreach (var type in assembly.GetTypes()) { if (type.IsVisible == false) { continue; } /*if (type.IsSerializable == false) * continue;*/ if (filterCheck(type) == false) { continue; } output.Add(type); } bool filterCheck(Type type) { foreach (var filter in filters) { if (filter.Verify(type) == false) { return(false); } } return(true); } } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //if (property.propertyType != SerializedPropertyType.String) var soundManager = Object.FindObjectOfType <SoundManager>(); if (soundManager != null) { var keyList = soundManager.GetAudioDataKeys().ToList(); var currentKeyIndex = keyList.IndexOf(property.stringValue); var presented = currentKeyIndex != -1; var controlID = GUIUtility.GetControlID(FocusType.Passive); // label EditorGUI.LabelField(position, property.displayName); // apply selected if (ObjectPickerWindow.TryGetPickedObject(controlID, out string picked)) { property.stringValue = picked; } if (presented) { // selection button if (GUI.Button(new Rect(position.x + EditorGUIUtility.labelWidth, position.y, position.width - EditorGUIUtility.labelWidth, position.height), property.stringValue)) { ObjectPickerWindow.Show(controlID, property.stringValue, keyList, 0, s => new GUIContent(s), "Select an audio key"); } } else { // change & save color var tmpColor = GUI.backgroundColor; GUI.backgroundColor = Color.red; // selection button if (GUI.Button(new Rect(position.x + EditorGUIUtility.labelWidth, position.y, position.width - EditorGUIUtility.labelWidth, position.height), property.stringValue)) { ObjectPickerWindow.Show(controlID, property.stringValue, keyList, 0, s => new GUIContent(s), "Select an audio key"); } // restore bg color GUI.backgroundColor = tmpColor; } // label click if (Event.current.type == EventType.ContextClick && new Rect(position.x, position.y, EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight) .Contains(Event.current.mousePosition)) { // create context menu var context = new GenericMenu(); // fill the menu, list could be large but for small projects, this is more convenient foreach (var key in keyList) { context.AddItem(new GUIContent(key), key == property.stringValue, setKey, key); // do not create lambda for all items void setKey(object userData) { property.stringValue = (string)userData; property.serializedObject.ApplyModifiedProperties(); } } // show menu context.ShowAsContext(); } } else { // show default string EditorGUI.PropertyField(position, property, true); } }
////////////////////////////////////////////////////////////////////////// public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { switch (Event.current.type) { case EventType.MouseMove: case EventType.Layout: case EventType.DragUpdated: case EventType.DragPerform: case EventType.DragExited: case EventType.Ignore: case EventType.Used: //case EventType.ValidateCommand: //case EventType.ExecuteCommand: return; default: break; } // try get localization manager var core = Object.FindObjectOfType <Core>(); if (core == null) { EditorGUI.LabelField(position, "Core not presented on scene"); return; } var localizationManager = core.GetModule <Localization>()?.GetEditorLocalizationManager(); if (localizationManager == null) { EditorGUI.LabelField(position, "Core doesn't include active serialization module"); return; } var key = property.FindPropertyRelative(LocalizedString.c_KeyProperty); var hasKey = localizationManager.ContainsKey(key.stringValue); var controlID = GUIUtility.GetControlID(FocusType.Passive); // select existing key from menu ObjectPickerWindow.TryGetPickedObject(controlID, out string picked); if (picked != null) { key.stringValue = picked; property.serializedObject.ApplyModifiedProperties(); } // in header rect if (Event.current.type == EventType.ContextClick && new Rect(position.x, position.y, EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight) .Contains(Event.current.mousePosition)) { // create context menu var context = new GenericMenu(); // select existing key context.AddItem(new GUIContent("Select"), false, () => { // ReSharper disable once ConvertClosureToMethodGroup selectKey(); }); // if presented in dictionary key if (hasKey) { // rename key context.AddItem(new GUIContent("Rename"), false, () => { TextEditWindow.Show(controlID, key.stringValue, s => { if (string.IsNullOrWhiteSpace(s)) { return; } if (s == key.stringValue) { return; } // rename key if the name is not taken if (localizationManager.ChangeKey(key.stringValue, s, LocalizationManager.ChangeLocalizationKeyMode.Block)) { key.stringValue = s; property.serializedObject.ApplyModifiedProperties(); // reload reloadDictionary(); } }); }); // delete key context.AddItem(new GUIContent("Delete"), false, () => { if (string.IsNullOrWhiteSpace(key.stringValue)) { return; } // remove key, save changes localizationManager.RemoveKey(key.stringValue); key.stringValue = string.Empty; property.serializedObject.ApplyModifiedProperties(); // reload reloadDictionary(); }); } // refresh database context.AddItem(new GUIContent("Refresh"), false, () => { // ReSharper disable once ConvertClosureToMethodGroup reloadDictionary(); }); // show context window context.ShowAsContext(); } // foldout check property.isExpanded = EditorGUI.Foldout(new Rect(position.x, position.y, EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight), property.isExpanded, property.displayName, true); if (property.isExpanded) { // key edit, update text var keyValue = EditorGUI.TextField( new Rect(position.x + EditorGUIUtility.labelWidth, position.y, position.width - EditorGUIUtility.labelWidth - c_LanguagePopupWidth - c_SelectButtonWidth, EditorGUIUtility.singleLineHeight), "", key.stringValue); // is key changed if (keyValue != key.stringValue) { // rename key if (localizationManager.ChangeKey(key.stringValue, keyValue, LocalizationManager.ChangeLocalizationKeyMode.None)) { // save changes key.stringValue = keyValue; key.serializedObject.ApplyModifiedProperties(); } } // select key button if (GUI.Button(new Rect(position.x + position.width - c_LanguagePopupWidth - c_SelectButtonWidth, position.y, c_SelectButtonWidth, EditorGUIUtility.singleLineHeight), "*")) { selectKey(); } // current language var languages = localizationManager.GetLanguages(); var selectedLanguage = EditorGUI.Popup(new Rect(position.x + position.width - c_LanguagePopupWidth, position.y, c_LanguagePopupWidth, EditorGUIUtility.singleLineHeight), languages.IndexOf(localizationManager.Language), languages.ToArray()); // change current localization if (selectedLanguage != -1 && languages[selectedLanguage] != localizationManager.Language) { localizationManager.Language = languages[selectedLanguage]; } // draw textField & update key updateValue(new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight)); } else { // draw text only version updateValue(new Rect(position.x + EditorGUIUtility.labelWidth, position.y, position.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight)); } ///////////////////////////////////// void updateValue(Rect textRect) { if (hasKey) { // update data var value = localizationManager.Localize(key.stringValue); var localizationValue = EditorGUI.TextArea(textRect, value); // value was changed, update key value if (localizationValue != value) { localizationManager.ChangeValue(key.stringValue, localizationValue); } } else { // show placeholder, if changed then create new key var localizationValue = EditorGUI.TextArea(textRect, LocalizationManager.c_DefaultKeyValue); // not default value, initialize key if (localizationValue != LocalizationManager.c_DefaultKeyValue) { localizationManager.CreateKey(key.stringValue, localizationValue); } } } void reloadDictionary() { // remove key, save changes localizationManager.Read(null, true); } void selectKey() { var keys = localizationManager.GetDictionary().Select(n => n.Key).ToList(); keys.Sort(StringComparer.CurrentCulture); ObjectPickerWindow.Show(controlID, key.stringValue, keys, 0, s => new GUIContent(s), "Select localization key"); } }
////////////////////////////////////////////////////////////////////////// public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.propertyType != SerializedPropertyType.ManagedReference) { // draw default EditorGUI.PropertyField(new Rect(position.x, position.y, position.width, position.height), property, true); return; } // update selected var id = GUIUtility.GetControlID(FocusType.Passive); if (ObjectPickerWindow.TryGetPickedObject(id, out Type selected)) { property.managedReferenceValue = selected == typeof(object) ? null : Activator.CreateInstance((Type)selected); property.serializedObject.ApplyModifiedPropertiesWithoutUndo(); } // draw select type btn if (GUI.Button(new Rect(position.x + EditorGUIUtility.labelWidth, position.y, position.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight), property.managedReferenceFullTypename)) { // object type means null value var typeList = _GetTypeList(); typeList.Insert(0, typeof(object)); ObjectPickerWindow.Show(id, null, typeList, 0, n => n == typeof(object) ? new GUIContent("Null") : new GUIContent(n.FullName)); } // in header rect if (Event.current.type == EventType.ContextClick && new Rect(position.x, position.y, EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight) .Contains(Event.current.mousePosition)) { // create context menu var context = new GenericMenu(); // null option context.AddItem(new GUIContent("Null"), false, () => { property.managedReferenceValue = null; property.serializedObject.ApplyModifiedPropertiesWithoutUndo(); }); // fill context menu types foreach (var type in _GetTypeList()) { context.AddItem(new GUIContent(type.FullName), false, () => _SetProperty(type)); } // show context window context.ShowAsContext(); } // draw default EditorGUI.PropertyField(new Rect(position.x, position.y, position.width, position.height), property, true); ///////////////////////////////////// void filterTypes(Assembly asm, List <Type> output) { Type fieldType = null; // is list field if (fieldInfo.FieldType.IsGenericType && fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(List <>)) { fieldType = fieldInfo.FieldType.GetGenericArguments()[0]; } else // is array if (fieldInfo.FieldType.IsArray) { fieldType = fieldInfo.FieldType.GetElementType(); } else { // default field fieldType = fieldInfo.FieldType; } foreach (var type in asm.GetTypes()) { if (type.IsVisible == false) { continue; } if (type.IsClass == false) { continue; } if (type.IsGenericType) { continue; } if (type.IsAbstract) { continue; } if (type.IsSerializable == false) { continue; } if (fieldType.IsAssignableFrom(type) == false) { continue; } //if (filterCheck(type) == false) //continue; output.Add(type); } } List <Type> _GetTypeList() { var typeList = new List <Type>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { filterTypes(assembly, typeList); } typeList.Sort((x, y) => string.CompareOrdinal(x.FullName, y.FullName)); return(typeList); } void _SetProperty(Type type) { property.managedReferenceValue = Activator.CreateInstance(type); property.serializedObject.ApplyModifiedPropertiesWithoutUndo(); } }