using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AC
{
[System.Serializable]
public class ActionApplyResolution : Action
{
public override ActionCategory Category { get { return ActionCategory.Engine; }}
public override string Title { get { return "Apply resolution"; }}
override public float Run ()
{
int chosenIndex = GlobalVariables.GetIntegerValue (266); // Replace '0' with your own variable's ID number
if (chosenIndex >= 0)
{
Resolution chosenResolution = Screen.resolutions [chosenIndex];
Screen.SetResolution (chosenResolution.width, chosenResolution.height, Screen.fullScreen);
KickStarter.playerMenus.RecalculateAll ();
}
bool windowed = GlobalVariables.GetBooleanValue (267);//CHANGE TO WINDOWED/FULLSCREEN VARIABLE
Screen.fullScreen = windowed;
return 0f;
}
}
}
and
using UnityEngine;
using AC;
public class SetupOptionsMenu : MonoBehaviour
{
void Start ()
{
GenerateResolutionCycleOptions();
InitResolutionVariable();
}
void GenerateResolutionCycleOptions()
{
MenuCycle resolutionCycle1 = PlayerMenus.GetElementWithName ("Options1", "ScreenResolution") as AC.MenuCycle;
MenuCycle resolutionCycle2 = PlayerMenus.GetElementWithName ("Options", "ScreenResolution") as AC.MenuCycle;
resolutionCycle1.optionsArray.Clear();
resolutionCycle2.optionsArray.Clear();
foreach (Resolution resolution in Screen.resolutions)
{
string optionLabel = resolution.width.ToString () + " x " + resolution.height.ToString ();
resolutionCycle1.optionsArray.Add (optionLabel);
resolutionCycle2.optionsArray.Add (optionLabel);
}
}
void InitResolutionVariable ()
{
GVar variable = GlobalVariables.GetVariable (266); // Replace '0' with your own variable's ID number
if (variable.IntegerValue == -1)
{
for (int i=0; i<Screen.resolutions.Length; i++)
{
if (Screen.resolutions[i].width == Screen.currentResolution.width &&
Screen.resolutions[i].height == Screen.currentResolution.height &&
Screen.resolutions[i].refreshRate == Screen.currentResolution.refreshRate)
{
variable.IntegerValue = i;
return;
}
}
variable.IntegerValue = 0;
}
//REPLACE THE VARIABLE NUMBER WITH WHAT WILL BE USED FOR FULLSCREEN OPTION
GVar variable = GlobalVariables.GetVariable (267);
variable.booleanValue = Screen.fullScreen;
}
}
I am getting these errors with the SetupOptionsMenu script shown above:
Assets/TMOWM/Scripts/SetupOptionsMenu.cs(46,8): error CS0128: A local variable or function named 'variable' is already defined in this scope
and
Assets/TMOWM/Scripts/SetupOptionsMenu.cs(47,12): error CS1061: 'GVar' does not contain a definition for 'booleanValue' and no accessible extension method 'booleanValue' accepting a first argument of type 'GVar' could be found (are you missing a using directive or an assembly reference?)
void InitResolutionVariable ()
{
//REPLACE THE VARIABLE NUMBER WITH WHAT WILL BE USED FOR FULLSCREEN OPTION
GVar variable = GlobalVariables.GetVariable (267);
variable.BooleanValue = Screen.fullScreen;
variable = GlobalVariables.GetVariable (266); // Replace '0' with your own variable's ID number
if (variable.IntegerValue == -1)
{
for (int i=0; i<Screen.resolutions.Length; i++)
{
if (Screen.resolutions[i].width == Screen.currentResolution.width &&
Screen.resolutions[i].height == Screen.currentResolution.height &&
Screen.resolutions[i].refreshRate == Screen.currentResolution.refreshRate)
{
variable.IntegerValue = i;
return;
}
}
variable.IntegerValue = 0;
}
}
And then do I just create a new cycle option for full screen/windowed and follow the previous steps?
Nothing as complicated as the previous steps, as it's not dynamic - it's always either On or Off. For this you can use a Toggle element, linked to the correct Global Variable.
Ok fab, and have I set it up correctly? I am using same ActionApplyResolution?
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AC
{
[System.Serializable]
public class ActionApplyResolution : Action
{
public override ActionCategory Category { get { return ActionCategory.Engine; }}
public override string Title { get { return "Apply resolution"; }}
override public float Run ()
{
int chosenIndex = GlobalVariables.GetIntegerValue (266); // Replace '0' with your own variable's ID number
if (chosenIndex >= 0)
{
Resolution chosenResolution = Screen.resolutions [chosenIndex];
Screen.SetResolution (chosenResolution.width, chosenResolution.height, Screen.fullScreen);
KickStarter.playerMenus.RecalculateAll ();
}
bool windowed = GlobalVariables.GetBooleanValue (267);//CHANGE TO WINDOWED/FULLSCREEN VARIABLE
Screen.fullScreen = windowed;
return 0f;
}
}
}
Please see screenshots, bare in mind I need it to effect 2 menus again, Options and Options 1:
using UnityEngine;
using AC;
public class SetupOptionsMenu : MonoBehaviour
{
void Start ()
{
GenerateResolutionCycleOptions();
InitResolutionVariable();
}
void GenerateResolutionCycleOptions()
{
MenuCycle resolutionCycle1 = PlayerMenus.GetElementWithName ("Options1", "ScreenResolution") as AC.MenuCycle;
MenuCycle resolutionCycle2 = PlayerMenus.GetElementWithName ("Options", "ScreenResolution") as AC.MenuCycle;
resolutionCycle1.optionsArray.Clear();
resolutionCycle2.optionsArray.Clear();
foreach (Resolution resolution in Screen.resolutions)
{
string optionLabel = resolution.width.ToString () + " x " + resolution.height.ToString ();
resolutionCycle1.optionsArray.Add (optionLabel);
resolutionCycle2.optionsArray.Add (optionLabel);
}
}
void InitResolutionVariable ()
{
GVar variable = GlobalVariables.GetVariable (26);
variable.BooleanValue = Screen.fullScreen;
variable = GlobalVariables.GetVariable (24); // Replace '0' with your own variable's ID number
if (variable.IntegerValue == -1)
{
for (int i=0; i<Screen.resolutions.Length; i++)
{
if (Screen.resolutions[i].width == Screen.currentResolution.width &&
Screen.resolutions[i].height == Screen.currentResolution.height &&
Screen.resolutions[i].refreshRate == Screen.currentResolution.refreshRate)
{
variable.IntegerValue = i;
return;
}
}
variable.IntegerValue = 0;
}
}
}
and
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AC
{
[System.Serializable]
public class ActionApplyResolution : Action
{
public override ActionCategory Category { get { return ActionCategory.Engine; }}
public override string Title { get { return "Apply resolution"; }}
override public float Run ()
{
int chosenIndex = GlobalVariables.GetIntegerValue (24); // Replace '0' with your own variable's ID number
if (chosenIndex >= 0)
{
Resolution chosenResolution = Screen.resolutions [chosenIndex];
Screen.SetResolution (chosenResolution.width, chosenResolution.height, Screen.fullScreen);
KickStarter.playerMenus.RecalculateAll ();
}
bool windowed = GlobalVariables.GetBooleanValue (0);//CHANGE TO WINDOWED/FULLSCREEN VARIABLE
Screen.fullScreen = windowed;
return 0f;
}
}
}
So, I have the windowed on/off working for one of my games, but am having trouble with my existing game. The one with two Options menus (Options, and Options 1) - It just won't change window size. Please see screenshots and code below:
using UnityEngine;
using AC;
public class SetupOptionsMenu : MonoBehaviour
{
void Start ()
{
GenerateResolutionCycleOptions();
InitResolutionVariable();
}
void GenerateResolutionCycleOptions()
{
MenuCycle resolutionCycle1 = PlayerMenus.GetElementWithName ("Options1", "ScreenResolution") as AC.MenuCycle;
MenuCycle resolutionCycle2 = PlayerMenus.GetElementWithName ("Options", "ScreenResolution") as AC.MenuCycle;
resolutionCycle1.optionsArray.Clear();
resolutionCycle2.optionsArray.Clear();
foreach (Resolution resolution in Screen.resolutions)
{
string optionLabel = resolution.width.ToString () + " x " + resolution.height.ToString ();
resolutionCycle1.optionsArray.Add (optionLabel);
resolutionCycle2.optionsArray.Add (optionLabel);
}
}
void InitResolutionVariable ()
{
GVar variable = GlobalVariables.GetVariable (266);
variable.BooleanValue = Screen.fullScreen;
variable = GlobalVariables.GetVariable (267); // Replace '0' with your own variable's ID number
if (variable.IntegerValue == -1)
{
for (int i=0; i<Screen.resolutions.Length; i++)
{
if (Screen.resolutions[i].width == Screen.currentResolution.width &&
Screen.resolutions[i].height == Screen.currentResolution.height &&
Screen.resolutions[i].refreshRate == Screen.currentResolution.refreshRate)
{
variable.IntegerValue = i;
return;
}
}
variable.IntegerValue = 0;
}
}
}
And
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AC
{
[System.Serializable]
public class ActionApplyResolution : Action
{
public override ActionCategory Category { get { return ActionCategory.Engine; }}
public override string Title { get { return "Apply resolution"; }}
override public float Run ()
{
int chosenIndex = GlobalVariables.GetIntegerValue (267); // Replace '0' with your own variable's ID number
if (chosenIndex >= 0)
{
Resolution chosenResolution = Screen.resolutions [chosenIndex];
Screen.SetResolution (chosenResolution.width, chosenResolution.height, Screen.fullScreen);
KickStarter.playerMenus.RecalculateAll ();
}
bool windowed = GlobalVariables.GetBooleanValue (266);//CHANGE TO WINDOWED/FULLSCREEN VARIABLE
Screen.fullScreen = windowed;
return 0f;
}
}
}
As the available resolutions is dynamic, based on the machine the game is running on, they must be used to populate the Cycle element at runtime. In the tutorial, this is done within the GenerateResolutionCycleOptions function.
This is an old post, however. Since then, a dedicated "Graphic options" UI template can be found on the Downloads page.
Comments
Great thanks! And any idea how I add the windowed/non windowed option as put forward by this posts user?
It's a case of using the principle covered in the tutorial to affect Unity's Screen.fullScreen property.
ok thanks Chris, so could i combine them like so?
and
Swap the setting of Screen.fullScreen and Screen.SetResolution around in your Run function.
I am getting these errors with the SetupOptionsMenu script shown above:
and
Swap the setting of Screen.fullScreen and Screen.SetResolution around in your Run function.
Sorry, where exactly?
Nothing as complicated as the previous steps, as it's not dynamic - it's always either On or Off. For this you can use a Toggle element, linked to the correct Global Variable.
Ok fab, and have I set it up correctly? I am using same ActionApplyResolution?
Please see screenshots, bare in mind I need it to effect 2 menus again, Options and Options 1:
https://www.dropbox.com/sh/63lh16u2f9g5opg/AACcynIlzraRZadivzOI5MlPa?dl=0
Looks OK to me.
So, my windowed toggle doesn't seem to do anything as it is currently set up, see video and screenshots:
https://www.dropbox.com/sh/99q5jyg5wh4gsaz/AABqD5lu5kHEu1v6FceY9kSza?dl=0
And just for clarity again:
and
Your Action script refers to the wrong variable, and your "Set resolution" asset should also be assigned in the Toggle's "ActionList on click" field.
Unity 2019.4.32f1
AC V1.74.5
So, I have the windowed on/off working for one of my games, but am having trouble with my existing game. The one with two Options menus (Options, and Options 1) - It just won't change window size. Please see screenshots and code below:
And
Screenshots below:
https://www.dropbox.com/sh/l4t0phmwet496l7/AACPQ9XTU1DYlT7wh14ZSejDa?dl=0
The window's size, or its fullscreen state?
That there are two Options menus should no impact on things. In the project that works, are you using the same code?
Try temporarily commenting out the line:
so i am wanting to make screen windowed. It is exactly the same code bar the variable numbers. Commenting out the code didn't do anything.
If it's the same code, the code is not the problem - it'll be down to the way you're implementing it.
So, I have the windowed working, but for some reason it won't now cycle through any options, just has the one. Using the code above.
is it because I don't have this in there?
(AC.PlayerMenus.GetElementWithName ("Options1", "ScreenResolution") as AC.MenuCycle).optionsArray[0]
and
(AC.PlayerMenus.GetElementWithName ("Options", "ScreenResolution") as AC.MenuCycle).optionsArray[0]
See photo of variable and menu here, thanks:
https://www.dropbox.com/sh/xxyl864va9h4hr1/AAA47ASWDfy4uMAEJR5J1VlEa?dl=0
As the available resolutions is dynamic, based on the machine the game is running on, they must be used to populate the Cycle element at runtime. In the tutorial, this is done within the GenerateResolutionCycleOptions function.
This is an old post, however. Since then, a dedicated "Graphic options" UI template can be found on the Downloads page.