I'm making a simple menu in 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. Here starts the problem. When I press F11, the screen becomes in fullsize, but the toggle doesn't activates and vice versa. 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. 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. 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:
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. I would combine these (and simplify them) into a ScreenManager
class:
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:
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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.