Exemplo n.º 1
0
        private void Draw(Rect position, GUIContent label,
                          SerializedProperty property, ScriptableObjectDropdownAttribute 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets ScriptableObjects just when it is selected or new ScriptableObject added to the project
        /// </summary>
        private void GetScriptableObjects(ScriptableObjectDropdownAttribute 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);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ScriptableObjectDropdownAttribute castedAttribute = attribute as ScriptableObjectDropdownAttribute;

            if (_scriptableObjects.Count == 0)
            {
                GetScriptableObjects(castedAttribute);
            }

            Draw(position, label, property, castedAttribute);
        }
Exemplo n.º 4
0
        private bool ValidateAttribute(ScriptableObjectDropdownAttribute attribute)
        {
            if (attribute.BaseType.IsInterface)
            {
                return(true);
            }

            if (attribute.BaseType.IsSubclassOf(typeof(ScriptableObject)))
            {
                return(true);
            }
            return(false);
        }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);
        ScriptableObjectDropdownAttribute typeAttribute = attribute as ScriptableObjectDropdownAttribute;
        String baseObject = typeAttribute.scriptableObject;

        string[] guids      = AssetDatabase.FindAssets("t:" + baseObject);
        string[] assetNames = new string[guids.Length];
        string[] names      = new string[guids.Length];
        for (int i = 0; i < guids.Length; i++)
        {
            string   assetPath    = AssetDatabase.GUIDToAssetPath(guids[i]);
            string[] splittedPath = assetPath.Split("/" [0]);
            assetNames [i] = splittedPath [splittedPath.Length - 1];
            names [i]      = assetNames [i].Split("." [0]) [0];
        }

        if (names.Length <= 0)
        {
            EditorGUI.Popup(position, label.text, 0, new string[] { "No types of " + baseObject + " found" });
        }
        else
        {
            SerializedProperty stringProperty = null;

            if (property.propertyType == SerializedPropertyType.String)
            {
                stringProperty = property;
            }
            if (stringProperty != null)
            {
                string currentName  = stringProperty.stringValue;
                int    selected     = string.IsNullOrEmpty(stringProperty.stringValue) ? 0 : Array.IndexOf(names, currentName);
                int    newSelection = EditorGUI.Popup(position, label.text, selected, names);

                stringProperty.stringValue = names[Mathf.Max(0, newSelection)];
            }
        }

        EditorGUI.EndProperty();
    }
        private void Draw(Rect position, GUIContent label,
                          SerializedProperty property, ScriptableObjectDropdownAttribute attribute)
        {
            if (label != null && label != GUIContent.none)
            {
                position = EditorGUI.PrefixLabel(position, label);
            }

            if (ValidateProperty(property))
            {
                if (_scriptableObjects.Count != 0)
                {
                    UpdateScriptableObjectSelectionControl(position, label, property, attribute);
                }
                else
                {
                    EditorGUI.LabelField(position, "No this type asset in Resources folder");
                }
            }
            else
            {
                EditorGUI.LabelField(position, "Use it with non-array ScriptableObject or derived class of ScriptableObject");
            }
        }
Exemplo n.º 7
0
        private ScriptableObject DrawScriptableObjectSelectionControl(Rect position, GUIContent label,
                                                                      ScriptableObject scriptableObject, ScriptableObjectDropdownAttribute 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 (_selectedControlID == controlID)
                    {
                        if (scriptableObject != _selectedScriptableObject)
                        {
                            scriptableObject = _selectedScriptableObject;
                            _isChanged       = true;
                        }

                        _selectedControlID        = 0;
                        _selectedScriptableObject = 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 (scriptableObject == null)
                {
                    _popupContent.text = "Nothing";
                }
                else
                {
                    _popupContent.text = scriptableObject.name;
                }
                EditorStyles.popup.Draw(position, _popupContent, controlID);
                break;
            }

            if (_scriptableObjects.Count != 0 && triggerDropDown)
            {
                _selectedControlID        = controlID;
                _selectedScriptableObject = scriptableObject;

                DisplayDropDown(position, scriptableObject, attribute.grouping);
            }

            return(scriptableObject);
        }
Exemplo n.º 8
0
        private void UpdateScriptableObjectSelectionControl(Rect position, GUIContent label,
                                                            SerializedProperty property, ScriptableObjectDropdownAttribute attribute)
        {
            SerializedProperty value  = property.FindPropertyRelative("value");
            ScriptableObject   output = DrawScriptableObjectSelectionControl(position, label,
                                                                             value.objectReferenceValue as ScriptableObject, attribute);

            if (_isChanged)
            {
                _isChanged = false;
                value.objectReferenceValue = output;
            }
        }
        private static void UpdateScriptableObjectSelectionControl(Rect position, GUIContent label,
                                                                   SerializedProperty property, ScriptableObjectDropdownAttribute attribute)
        {
            ScriptableObject output = DrawScriptableObjectSelectionControl(position, label, property.objectReferenceValue as ScriptableObject, property, attribute);

            if (isChanged)
            {
                isChanged = false;
                property.objectReferenceValue = output;
            }
        }