Exemple #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            prefabDropdown = attribute as PrefabDropdownAttribute;
            //finding all prefabs with the desired component
            if (prefabs == null)
            {
                FindPrefabs(property);
            }

            //displaying a message if the user provided an invalid path
            if (!validPath)
            {
                EditorGUI.LabelField(position, label.text + " - Invalid PrefabDropdown path");
                return;
            }

            //displaying a message if there are no matching prefabs in the folder
            if (prefabs.Count == 0)
            {
                EditorGUI.LabelField(position, label.text + " - No prefabs of type at path");
                return;
            }

            //finding the correct index when the component with this drawer is reshown in the inspector
            if (index == -1)
            {
                index = Mathf.Max(0, 1 + prefabs.IndexOf((GameObject)property.objectReferenceValue));
            }

            position.width -= 20;
            //showing the popup, setting the properties object refrence when index is changed
            int i = EditorGUI.Popup(position, label.text, index, names);

            if (i != index || property.objectReferenceValue == null)
            {
                index = i;
                if (index == 0)
                {
                    property.objectReferenceValue = null;
                }
                else if (prefabDropdown.propertyType == "GameObject")
                {
                    property.objectReferenceValue = ((Component)prefabs[i - 1]).gameObject;
                }
                else
                {
                    property.objectReferenceValue = prefabs[i - 1];
                }
            }

            position.x    += position.width;
            position.width = 20;
            //displaying a button to allow you to quickly find the selected object in the project window
            if (GUI.Button(position, ">"))
            {
                if (property.objectReferenceValue != null)
                {
                    if (prefabDropdown.propertyType == "GameObject")
                    {
                        Selection.activeGameObject = (GameObject)property.objectReferenceValue;
                    }
                    else
                    {
                        Selection.activeGameObject = ((Component)property.objectReferenceValue).gameObject;
                    }
                }
            }
        }
Exemple #2
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        prefabDropdown = attribute as PrefabDropdownAttribute;

        if (editorSkin == null)
        {
            GetStyles();
        }

        //finding all prefabs with the desired component
        if (prefabs == null)
        {
            FindPrefabs(property);
        }

        //displaying a message if the user provided an invalid path
        if (!validPath)
        {
            EditorGUI.HelpBox(position, string.Format("{0} - path \"{1}\" does not exist in Assets", label.text, prefabDropdown.path), MessageType.Error);
            return;
        }

        //displaying a message if there are no matching prefabs in the folder
        if (prefabs.Count == 0)
        {
            EditorGUI.HelpBox(position, label.text + " - No prefabs of type at path", MessageType.Error);
            return;
        }

        //finding the correct index when the component with this drawer is reshown in the inspector
        if (index == -1)
        {
            index = Mathf.Max(0, 1 + prefabs.IndexOf((GameObject)property.objectReferenceValue));
        }

        position.width -= 20;
        //showing the popup, setting the properties object refrence when index is changed
        int i = EditorGUI.Popup(position, label.text, index, names);

        if (i != index || property.objectReferenceValue == null)
        {
            index = i;
            if (index == 0)
            {
                property.objectReferenceValue = null;
            }
            else if (prefabDropdown.propertyType == "GameObject")
            {
                property.objectReferenceValue = ((Component)prefabs[i - 1]).gameObject;
            }
            else
            {
                property.objectReferenceValue = prefabs[i - 1];
            }
        }

        position.x    += position.width - 1;
        position.width = 21;

        EditorGUI.BeginDisabledGroup(!property.objectReferenceValue);
        //displaying a button to allow you to quickly find the selected object in the project window
        if (GUI.Button(position, GUIContent.none, prefabSelectStyle))
        {
            if (property.objectReferenceValue != null)
            {
                if (prefabDropdown.propertyType == "GameObject")
                {
                    Selection.activeGameObject = (GameObject)property.objectReferenceValue;
                }
                else
                {
                    Selection.activeGameObject = ((Component)property.objectReferenceValue).gameObject;
                }
            }
        }
        EditorGUI.EndDisabledGroup();
    }