Exemple #1
0
        /// <inheritdoc />
        protected override void RenderMemberControl(Rect position)
        {
            var options = GetNameOptions();

            PopupOption <AnimatorParameter> selectedOption = null;
            PopupOption <AnimatorParameter> noneOption     = new PopupOption <AnimatorParameter>(null, "No Parameter");

            AnimatorParameter value = GetValue();

            if (value != null)
            {
                string label = value.name;

                AnimatorParameter valueInOptions = options.Select(option => option.value).FirstOrDefault(ap => ap.Corresponds(value));

                if (valueInOptions != null)
                {
                    selectedOption = new PopupOption <AnimatorParameter>(valueInOptions, label);
                }
                else
                {
                    selectedOption = new PopupOption <AnimatorParameter>(value, label);
                }
            }

            // Make sure the callback uses the property of this drawer, not at its later value.
            var propertyNow = property;

            bool enabled = targets.Any(target => target != null);

            if (!enabled)
            {
                EditorGUI.BeginDisabledGroup(true);
            }

            PopupGUI <AnimatorParameter> .Render
            (
                position,
                newValue =>
            {
                Update(propertyNow);
                SetValue(newValue);
                propertyNow.serializedObject.ApplyModifiedProperties();
            },
                options,
                selectedOption,
                noneOption,
                nameProperty.hasMultipleDifferentValues
            );

            if (!enabled)
            {
                EditorGUI.EndDisabledGroup();
            }
        }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);

        List <PopupOption <string> > options = new List <PopupOption <string> >();

        if (UnityObject.FindObjectOfType <Timekeeper>() != null)
        {
            Timekeeper timekeeper = Timekeeper.instance;

            foreach (GlobalClock globalClock in timekeeper
                     .GetComponents <GlobalClock>()
                     .Where(gc => !string.IsNullOrEmpty(gc.key)))
            {
                options.Add(new PopupOption <string>(globalClock.key));
            }
        }

        PopupOption <string> selectedOption;

        if (options.Any(o => o.value == property.stringValue))
        {
            selectedOption = new PopupOption <string>(property.stringValue);
        }
        else if (!string.IsNullOrEmpty(property.stringValue))
        {
            selectedOption = new PopupOption <string>(property.stringValue, property.stringValue + " (Missing)");
        }
        else
        {
            selectedOption = null;
        }

        PopupOption <string> noneOption = new PopupOption <string>(null, "None");

        var currentProperty = property;

        position = EditorGUI.PrefixLabel(position, label);

        PopupGUI <string> .Render
        (
            position,
            gc => ChangeValue(currentProperty, gc),
            options,
            selectedOption,
            noneOption,
            property.hasMultipleDifferentValues
        );

        EditorGUI.EndProperty();
    }
Exemple #3
0
        /// <inheritdoc />
        protected override void RenderMemberControl(Rect position)
        {
            // Other Targets
            // Some Unity Objects, like Assets, are not supported by the drawer.
            // Just display an error message to let the user change their target.

            if (targetType == UnityObjectType.Other)
            {
                EditorGUI.HelpBox(position, "Unsupported Unity Object type.", MessageType.None);
                return;
            }

            // Display a list of all available reflected members in a popup.

            var options = new List <PopupOption <TMember> >();

            TMember value = GetValue();

            PopupOption <TMember> selectedOption = null;
            PopupOption <TMember> noneOption     = new PopupOption <TMember>(null, string.Format("No {0}", memberLabel));

            if (targetType == UnityObjectType.GameObject)
            {
                // Check if all targets have a GameObject (none are empty).
                // If they do, display all members of the GameObject type.

                if (HasSharedGameObject())
                {
                    var gameObjectOptions = GetMemberOptions(typeof(GameObject));

                    foreach (var gameObjectOption in gameObjectOptions)
                    {
                        // Prefix label by GameObject for popup clarity.

                        gameObjectOption.label = string.Format("GameObject/{0}", gameObjectOption.label);

                        options.Add(gameObjectOption);
                    }
                }

                // Find all shared component types across targets.
                // Display all members of each one found.

                foreach (Type componentType in GetSharedComponentTypes())
                {
                    var componentOptions = GetMemberOptions(componentType, componentType.Name);

                    foreach (var componentOption in componentOptions)
                    {
                        // Prefix label and option by component type for clear distinction.

                        componentOption.label = string.Format("{0}/{1}", componentType.Name, componentOption.label);

                        options.Add(componentOption);
                    }
                }

                // Determine which option is currently selected.

                if (value != null)
                {
                    string label;

                    if (value.component == null)
                    {
                        label = string.Format("GameObject.{0}", value.name);
                    }
                    else
                    {
                        label = string.Format("{0}.{1}", value.component, value.name);
                    }

                    UnityMethod method = value as UnityMethod;

                    if (method != null)
                    {
                        string parameterString = string.Join(", ", method.parameterTypes.Select(t => t.PrettyName()).ToArray());

                        label += string.Format(" ({0})", parameterString);
                    }

                    TMember valueInOptions = options.Select(option => option.value).FirstOrDefault(member => member.Corresponds(value));

                    if (valueInOptions != null)
                    {
                        selectedOption = new PopupOption <TMember>(valueInOptions, label);
                    }
                    else
                    {
                        selectedOption = new PopupOption <TMember>(value, label);
                    }
                }
            }
            else if (targetType == UnityObjectType.ScriptableObject)
            {
                // ScriptableObject Target
                // Make sure all targets share the same ScriptableObject Type.
                // If they do, display all members of that type.

                Type scriptableObjectType = GetSharedScriptableObjectType();

                if (scriptableObjectType != null)
                {
                    options.AddRange(GetMemberOptions(scriptableObjectType));

                    // Determine which option is currently selected.

                    if (value != null)
                    {
                        selectedOption = options.Find(o => o.value.Corresponds(value));

                        if (selectedOption == null)
                        {
                            selectedOption = new PopupOption <TMember>(value, value.name);
                        }
                    }
                }
            }

            // Make sure the callback uses the property of this drawer, not at its later value.
            var propertyNow = property;

            bool enabled = targetType != UnityObjectType.None;

            if (!enabled)
            {
                EditorGUI.BeginDisabledGroup(true);
            }

            PopupGUI <TMember> .Render
            (
                position,
                newValue =>
            {
                Update(propertyNow);
                SetValue(newValue);
                propertyNow.serializedObject.ApplyModifiedProperties();
            },
                options,
                selectedOption,
                noneOption,
                hasMultipleDifferentValues
            );

            if (!enabled)
            {
                EditorGUI.EndDisabledGroup();
            }
        }