[英]Issues with fullscreen in unity
I'm making a simple menu in Unity 2D. 我正在Unity 2D中制作一个简单的菜单。 I created a toggle that actives the full screen option.
我创建了一个激活全屏选项的开关。 At this point, everything is alright.
至此,一切都很好。 Then, I wanted to do the same, but pressing the F11 key.
然后,我想做同样的事情,但是按F11键。 Here starts the problem.
从这里开始问题。 When I press F11, the screen becomes in fullsize, but the toggle doesn't activates and vice versa.
当我按F11键时,屏幕将变为完整尺寸,但切换开关不会激活,反之亦然。 So, I wanted to create a C# script that checks if Screen.fullscreen is true, it activates the toggle, and if it's false, it deactivates it.
因此,我想创建一个C#脚本来检查Screen.fullscreen是否为true,是否激活切换,如果为false,则将其禁用。 I thonk that this will solve the problem, but no.
我认为这可以解决问题,但是没有。 When I try to press F11 or click on the toggle, the entire window goes crazy.
当我尝试按F11或单击切换开关时,整个窗口都会发疯。 I don't know how to explain it, but the window will start to shake on its own.
我不知道如何解释它,但是窗口将开始动摇。 I would appreciate if somebody helps me, thank you!
如果有人帮助我,我将不胜感激,谢谢!
The code of the toggle is this: 切换的代码是这样的:
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;
}
}
}
The code of the F11 key is this: 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;
}
}
}
}
And the code of the activate and deactivate is this: 而激活和停用的代码是这样的:
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;
}
}
}
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
}
}
}
You are repeating the fullScreen
logic in the ToggleScreen
script and the FullScreenMode
script. 您将在
ToggleScreen
脚本和FullScreenMode
脚本中重复fullScreen
逻辑。 I would combine these (and simplify them) into a ScreenManager
class: 我将这些(并简化它们)组合到
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);
}
}
Then, simply call ScreenManager.ToggleFullScreen()
in the FullScreenMode
script's Update()
method: 然后,只需在
FullScreenMode
脚本的Update()
方法中调用ScreenManager.ToggleFullScreen()
:
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.