全屏统一问题

时间:2019-03-05 17:39:03

标签: c# unity3d toggle

我正在Unity 2D中制作一个简单的菜单。我创建了一个激活全屏选项的开关。至此,一切都很好。 然后,我想做同样的事情,但是按F11键。从这里开始问题。当我按F11键时,屏幕将变为完整尺寸,但切换开关不会激活,反之亦然。 因此,我想创建一个C#脚本来检查Screen.fullscreen是否为true,是否激活切换,如果为false,则将其禁用。 我认为这可以解决问题,但是没有。当我尝试按F11或单击切换开关时,整个窗口都会发疯。我不知道如何解释它,但是窗口将开始动摇。 如果有人帮助我,我将非常感谢,谢谢!

切换的代码是这样的:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ToggleScreen : MonoBehaviour
{
    public void togglescreen()
    {
        if(Screen.fullScreen == true)
        {
            Screen.fullScreen = false;
        }

        else
        {
            Screen.fullScreen = true;
        }
    }
}

F11键的代码是这样的:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FullScreenMode : MonoBehaviour
{
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.F11))
        {
            if (Screen.fullScreen == true)
            {
                Screen.fullScreen = false;
            }
            else
            {
                Screen.fullScreen = true;
            }
        }


    }
}

激活和停用的代码是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ChangeValue : MonoBehaviour
{
    private Toggle onOffSwitch;

    private void Awake()
    {
        onOffSwitch = GetComponent<Toggle>();
    }

    private void Update()
    {
        //onOffSwitch.isOn = Screen.fullScreen;
        if(Screen.fullScreen == true)
        {
            onOffSwitch.isOn = true;
        }
        else
        {
            onOffSwitch.isOn = false;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

public class Toggler
{

bool state = false;

public void ToggleState()
{

    state = !state; // This sets a Boolean value to the opposite of itself

    if (state)
    {

        // State was just toggled on

    }else{

        // State was just toggled off

    }

}

}

答案 1 :(得分:0)

您要在fullScreen脚本和ToggleScreen脚本中重复FullScreenMode逻辑。我将这些(并简化它们)组合成一个ScreenManager类:

using UnityEngine;

public class ScreenManager : MonoBehaviour
{
    Toggle onOffSwitch;

    void Start()
    {
        // Don't forget to attach the Toggle component to this game object
        onOffSwitch = GetComponent<Toggle>(); 

        // Subscribe to onOffSwitch's onValueChanged event handler
        onOffSwitch.onValueChanged.AddListener(delegate 
        {
            // delegate method to call when onOffSwitch.onValueChanged is raised.
            ToggleValueChanged(onOffSwitch); 
        });
    }

    public static void ToggleFullScreen(bool updateToggleUI)
    { 
        var toggledValue = !Screen.fullScreen;
        Screen.fullScreen = toggledValue;
        onOffSwitch.isOn = updateToggleUI ? toggledValue : onOffSwitch.isOn;
    }

    void ToggleValueChanged(Toggle change)
    {
        // The toggle was already changed via the UI, don't flip it back
        ToggleFullScreen(false); 
    }
}

然后,只需在ScreenManager.ToggleFullScreen()脚本的FullScreenMode方法中调用Update()

using UnityEngine;

public class FullScreenMode : MonoBehaviour
{
    // Don't forget to drag the ScreenManager instance to this reference in the inspector
    [Serializable]
    ScreenManager _screenManager;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F11))
        {
            // Change the Toggle UI since this was triggered with F11
            _screenManager.ToggleFullScreen(true);
        }
    }
}