private void Draw(Rect position, GUIContent label, SerializedProperty property, ScriptableObjectMultiSelectDropdownAttribute attribute) { if (label != null && label != GUIContent.none) { position = EditorGUI.PrefixLabel(position, label); } if (!ValidateAttribute(attribute)) { EditorGUI.LabelField(position, "PropertyAttribute baseType does not inherit ScriptableObject"); return; } if (!ValidateProperty(property)) { EditorGUI.LabelField(position, "Use it with ScriptableObjectReference"); return; } if (_scriptableObjects.Count == 0) { EditorGUI.LabelField(position, "This type asset does not exist in the project"); return; } UpdateScriptableObjectSelectionControl(position, label, property, attribute); }
/// <summary> /// Gets ScriptableObjects just when it is selected or new ScriptableObject added to the project /// </summary> private void GetScriptableObjects(ScriptableObjectMultiSelectDropdownAttribute attribute) { if (attribute.BaseType.IsClass) { string[] guids = AssetDatabase.FindAssets(String.Format("t:{0}", attribute.BaseType)); for (int i = 0; i < guids.Length; i++) { _scriptableObjects.Add(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[i]), attribute.BaseType) as ScriptableObject); } } if (attribute.BaseType.IsInterface) { var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => attribute.BaseType.IsAssignableFrom(p)); foreach (Type type in types) { string[] guids = AssetDatabase.FindAssets(String.Format("t:{0}", type)); for (int i = 0; i < guids.Length; i++) { _scriptableObjects.Add(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[i]), attribute.BaseType) as ScriptableObject); } } } }
/// <summary> /// Gets ScriptableObjects just when it is a first time or new ScriptableObject added to the project /// </summary> private static void GetScriptableObjects(ScriptableObjectMultiSelectDropdownAttribute attribute) { UnityEngine.Object[] loadedObject = Resources.LoadAll("", attribute.BaseType); for (int i = 0; i < loadedObject.Length; i++) { _scriptableObjects.Add(loadedObject[i] as ScriptableObject); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ScriptableObjectMultiSelectDropdownAttribute castedAttribute = attribute as ScriptableObjectMultiSelectDropdownAttribute; if (_scriptableObjects.Count == 0) { GetScriptableObjects(castedAttribute); } Draw(position, label, property, castedAttribute); }
private bool ValidateAttribute(ScriptableObjectMultiSelectDropdownAttribute attribute) { if (attribute.BaseType.IsInterface) { return(true); } if (attribute.BaseType.IsSubclassOf(typeof(ScriptableObject))) { return(true); } return(false); }
private static void Draw(Rect position, GUIContent label, SerializedProperty property, ScriptableObjectMultiSelectDropdownAttribute attribute) { if (label != null && label != GUIContent.none) { position = EditorGUI.PrefixLabel(position, label); } if (ValidateProperty(property)) { if (_scriptableObjects.Count != 0) { UpdateScriptableObjectSelectionControl(position, label, property.FindPropertyRelative("values"), attribute); } else { EditorGUI.LabelField(position, "No this type asset in Resources folder"); } } else { EditorGUI.LabelField(position, "Use it with ScriptableObjectReference"); } }
private ScriptableObject[] DrawScriptableObjectSelectionControl(Rect position, GUIContent label, ScriptableObject[] scriptableObjects, ScriptableObjectMultiSelectDropdownAttribute attribute) { bool triggerDropDown = false; int controlID = GUIUtility.GetControlID(_controlHint, FocusType.Keyboard, position); switch (Event.current.GetTypeForControl(controlID)) { case EventType.ExecuteCommand: if (Event.current.commandName == "ScriptableObjectReferenceUpdated") { if (_selectionControlID == controlID) { if (scriptableObjects != _selectedScriptableObjects.ToArray()) { scriptableObjects = _selectedScriptableObjects.ToArray(); _isChanged = true; } _selectionControlID = 0; _selectedScriptableObjects = null; } } break; case EventType.MouseDown: if (GUI.enabled && position.Contains(Event.current.mousePosition)) { GUIUtility.keyboardControl = controlID; triggerDropDown = true; Event.current.Use(); } break; case EventType.KeyDown: if (GUI.enabled && GUIUtility.keyboardControl == controlID) { if (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.Space) { triggerDropDown = true; Event.current.Use(); } } break; case EventType.Repaint: if (scriptableObjects.Length == 0) { _popupContent.text = "Nothing"; } else if (scriptableObjects.Length == _scriptableObjects.Count) { _popupContent.text = "Everything"; } else if (scriptableObjects.Length >= 2) { _popupContent.text = "Mixed ..."; } else { _popupContent.text = scriptableObjects[0].name; } EditorStyles.popup.Draw(position, _popupContent, controlID); break; } if (triggerDropDown) { _selectionControlID = controlID; _selectedScriptableObjects = scriptableObjects.ToList(); DisplayDropDown(position, scriptableObjects, attribute.grouping); } return(scriptableObjects); }
private void UpdateScriptableObjectSelectionControl(Rect position, GUIContent label, SerializedProperty property, ScriptableObjectMultiSelectDropdownAttribute attribute) { SerializedProperty values = property.FindPropertyRelative("values"); ScriptableObject[] output = DrawScriptableObjectSelectionControl(position, label, Read(values), attribute); if (_isChanged) { _isChanged = false; Write(values, output); } }
private static void UpdateScriptableObjectSelectionControl(Rect position, GUIContent label, SerializedProperty property, ScriptableObjectMultiSelectDropdownAttribute attribute) { ScriptableObject[] output = DrawScriptableObjectSelectionControl(position, label, Read(property), property, attribute); if (isChanged) { isChanged = false; Write(property, output); } }