Ejemplo n.º 1
0
    // Start is called before the first frame update
    public override void OnInspectorGUI()
    {
        if (CannotModifyInPlayMode())
        {
            GUI.enabled = false;
        }

        UpdatePrefabModifiedProperties();

        var availableMessagesTypes = TypeResolver.TypesWithMessageAttribute.OrderBy(message => message.ToString()).ToArray();
        var availableMessages      = availableMessagesTypes.Select(type => type.ToString())
                                     .ToArray();

        targetScript.UnityEditorSelectedMessageTypeIndex = Array.IndexOf(
            availableMessages,
            targetScript.UnityEditorSelectedMessageName
            );

        var defaultLabelStyle = EditorStyles.label.fontStyle;

        EditorStyles.label.fontStyle = viewModelMethodPrefabModified
               ? FontStyle.Bold
               : defaultLabelStyle;

        var newSelectedIndex = EditorGUILayout.Popup(
            new GUIContent("MessageType", "Type of the view model that this template will be bound to when it is instantiated."),
            targetScript.UnityEditorSelectedMessageTypeIndex,
            availableMessages
            .Select(viewModel => new GUIContent(viewModel))
            .ToArray());

        EditorStyles.label.fontStyle = defaultLabelStyle;
        if (newSelectedIndex >= 0)
        {
            var newSelectedType = (Type)availableMessagesTypes[newSelectedIndex];
            targetScript.UnityEditorSelectedMessageName = newSelectedType.ToString();
        }

        // Don't let the user set anything else until they've chosen a view property.
        var guiPreviouslyEnabled = GUI.enabled;

        if (string.IsNullOrEmpty(targetScript.UnityEditorSelectedMessageName))
        {
            GUI.enabled = false;
            return;
        }


        var bindableMethods = TypeResolver.FindBindableMethods(targetScript, (Type)availableMessagesTypes[newSelectedIndex]);

        InspectorUtils.DoPopup(
            new GUIContent(targetScript.UnityEditorViewModelMethodHandler),
            new GUIContent("View-model method", ""),
            m => m.ViewModelType + "/" + m.MemberName,
            m => true,
            m => m.ToString() == targetScript.UnityEditorViewModelMethodHandler,
            m =>
        {
            UpdateProperty(
                updatedValue =>
            {
                targetScript.UnityEditorViewModelMethodHandler = updatedValue;
            },
                targetScript.UnityEditorViewModelMethodHandler,
                m.ToString(),
                "Set bound view-model method"
                );
        },
            bindableMethods
            .OrderBy(m => m.ViewModelTypeName)
            .ThenBy(m => m.MemberName)
            .ToArray()
            );
    }