Exemplo n.º 1
0
    protected virtual void OnEnable()
    {
        if (configuration == null)
        {
            // not bound to any configuration. Bound it to active
            if (MadLevel.hasActiveConfiguration)
            {
                configuration = MadLevel.activeConfiguration;
            }
            else
            {
                Debug.LogWarning("There's no active level configuration. Please prepare one and activate it.");
            }
        }
        else if (configuration != MadLevel.activeConfiguration)
        {
            if (Application.isPlaying)
            {
                Debug.LogWarning("This layout was prepared for different level configuration than the active one. "
                                 + "http://goo.gl/AxZqW2", this);
            }
        }

        var panel = MadTransform.FindParent <MadPanel>(transform);

        panel.onFocusChanged += (MadSprite sprite) => {
            if (activeIcon != null && sprite != activeIcon)
            {
                DeactivateActiveIcon();
            }
        };

        onIconActivate += (icon, levelName) => {
            if (onIconActivateMessage && onIconActivateMessageReceiver != null)
            {
                onIconActivateMessageReceiver.SendMessage(onIconActivateMessageMethodName, icon);
            }

            if (onIconActivatePlayAudio && onIconActivatePlayAudioClip != null && cachedAudioListener != null)
            {
                AudioSource.PlayClipAtPoint(
                    onIconActivatePlayAudioClip,
                    cachedAudioListener.transform.position,
                    onIconActivatePlayAudioVolume);
            }
        };

        onIconDeactivate += (icon, levelName) => {
            if (onIconDeactivateMessage && onIconDeactivateMessageReceiver != null)
            {
                onIconDeactivateMessageReceiver.SendMessage(onIconDeactivateMessageMethodName, icon);
            }

            if (onIconDeactivatePlayAudio && onIconDeactivatePlayAudioClip != null && cachedAudioListener != null)
            {
                AudioSource.PlayClipAtPoint(
                    onIconDeactivatePlayAudioClip,
                    cachedAudioListener.transform.position,
                    onIconDeactivatePlayAudioVolume);
            }
        };

#if UNITY_EDITOR
        EditorApplication.playmodeStateChanged = () => {
            if (configuration != null &&
                EditorApplication.isPlayingOrWillChangePlaymode &&
                !EditorApplication.isPlaying)
            {
                if (!configuration.IsValid())
                {
                    if (!EditorUtility.DisplayDialog(
                            "Invalid Configuration",
                            "Your level configuration has errors. Do you want to continue anyway?",
                            "Yes", "No"))
                    {
                        EditorApplication.isPlaying = false;
                        Selection.activeObject      = configuration;
                        return;
                    }
                }

                if (configuration != MadLevel.activeConfiguration ||
                    !configuration.CheckBuildSynchronized() ||
                    !configuration.active)
                {
                    if (EditorUtility.DisplayDialog(
                            "Not Synchronized",
                            "Build configuration of choice is not activate/synchronized with this level select layout "
                            + "(errors will occur). Do it now?",
                            "Yes", "No"))
                    {
                        MadLevelConfiguration.GetActive().active = false; // workaround
                        configuration.active = true;
                        configuration.SynchronizeBuild();
                    }
                }
            }
        };
#endif
    }