I’m trying to convert my previous GUI to NGUI, but I have some strange problems with it.
My previous UnityGUI-code (NewMainMenu.js) was this, which worked 100% fine:
#pragma strict
var customGuiStyle : GUIStyle;
var textureToDisplay : Texture2D;
//-------------- STUFF
private var OptionsMenuEnabled = false;
private var LevelMenuEnabled = false;
private var CreditsMenuEnabled = false;
static var toggleAA : boolean = true;
static var toggleBloom : boolean = true;
static var toggleDoF : boolean = true;
static var toggleSSAO : boolean = true;
static var toggleCCC : boolean = true;
static var toggleMB : boolean = true;
static var toggleFS : boolean = false;
static var toggleSO : boolean = true; //ScreenOverlay in Snow-Level
//-------------- GUI MATRIX (thanks to http://www.bensilvis.com/?p=500)
public static function AutoResize(screenWidth:int, screenHeight:int):void
{
var resizeRatio:Vector2 = Vector2(Screen.width / parseFloat(screenWidth), Screen.height / parseFloat(screenHeight));
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(resizeRatio.x, resizeRatio.y, 1.0));
}
function OnGUI()
{
AutoResize(1366, 768);
MainMenu();
if (OptionsMenuEnabled)
OptionsMenu();
if (LevelMenuEnabled)
LevelMenu();
if (CreditsMenuEnabled)
CreditsMenu();
GUI.Label (Rect (45, 485, 290, 40), "ver 1.0.2b // 5-19-2013");
}
function MainMenu()
{
GUI.Label (Rect (45, 25, textureToDisplay.width, textureToDisplay.height), textureToDisplay);
if (GUI.Button (Rect (125, 195, 130, 40), "Start", customGuiStyle))
LevelMenuEnabled = !LevelMenuEnabled;
if (GUI.Button (Rect (125, 245, 130, 40), "Options", customGuiStyle))
OptionsMenuEnabled = !OptionsMenuEnabled;
if (GUI.Button (Rect (125, 295, 130, 40), "Credits", customGuiStyle))
CreditsMenuEnabled = !CreditsMenuEnabled;
if (GUI.Button (Rect (125, 345, 130, 40), "Exit", customGuiStyle))
Application.Quit();
}
function OptionsMenu()
{
GUI.Label (Rect (430, 195, 290, 40), "Options...", customGuiStyle);
GUI.Label (Rect (430, 235, 990, 40), "______________________________________________");
//--------------------------------- QUALITY
GUI.Label (Rect (430, 275, 290, 40), "Anti Aliasing");
//SETTING FOR SUPER SAMPLING ANTI ALIASING
if(toggleAA != GUI.Toggle(Rect(630, 275, 40, 40), toggleAA, "" + (toggleAA ? "On" : "Off")))
{
toggleAA = !toggleAA;
Camera.main.GetComponent(AntialiasingAsPostEffect).enabled = toggleAA;
}
GUI.Label (Rect (430, 315, 290, 40), "Bloom");
//SETTING FOR BLOOM
if(toggleBloom != GUI.Toggle(Rect(630, 315, 40, 40), toggleBloom, "" + (toggleBloom ? "On" : "Off")))
{
toggleBloom = !toggleBloom;
Camera.main.GetComponent(Bloom).enabled = toggleBloom;
}
GUI.Label (Rect (430, 355, 290, 40), "Depth of Field");
//SETTING FOR DoF
if(toggleDoF != GUI.Toggle(Rect(630, 355, 40, 40), toggleDoF, "" + (toggleDoF ? "On" : "Off")))
{
toggleDoF = !toggleDoF;
Camera.main.GetComponent(DepthOfFieldScatter).enabled = toggleDoF;
}
GUI.Label (Rect (430, 395, 290, 40), "SSAO");
//SETTING FOR SSAO
if(toggleSSAO != GUI.Toggle(Rect(630, 395, 40, 40), toggleSSAO, "" + (toggleSSAO ? "On" : "Off")))
{
toggleSSAO = !toggleSSAO;
Camera.main.GetComponent(SSAOEffect).enabled = toggleSSAO;
}
GUI.Label (Rect (430, 435, 290, 40), "Color Correction");
//SETTING FOR COLOR CORRECTION CURVES
if(toggleCCC != GUI.Toggle(Rect(630, 435, 40, 40), toggleCCC, "" + (toggleCCC ? "On" : "Off")))
{
toggleCCC = !toggleCCC;
Camera.main.GetComponent(ColorCorrectionCurves).enabled = toggleCCC;
}
GUI.Label (Rect (430, 475, 290, 40), "Motion Blur");
//SETTING FOR COLOR CORRECTION CURVES
if(toggleMB != GUI.Toggle(Rect(630, 475, 40, 40), toggleMB, "" + (toggleMB ? "On" : "Off")))
{
toggleMB = !toggleMB;
Camera.main.GetComponent(CameraMotionBlur).enabled = toggleMB;
}
//--------------------------------- OTHER (I.E RESOLUTION, FULLSCREEN)
GUI.Label (Rect (750, 275, 290, 40), "Fullscreen");
//SETTING FOR FULLSCREEN
if(toggleFS != GUI.Toggle(Rect(890, 275, 110, 40), toggleFS, "" + (toggleFS ? "Activated" : "Deactivated")))
{
toggleFS = !toggleFS;
Screen.fullScreen = !Screen.fullScreen;
// http://answers.unity3d.com/questions/16216/how-to-get-native-screen-resolution.html
Screen.SetResolution (Screen.currentResolution.width, Screen.currentResolution.height, true);
}
GUI.Label (Rect (750, 355, 290, 40), "Screen Overlay");
//SETTING FOR FULLSCREEN
if(toggleSO != GUI.Toggle(Rect(890, 355, 40, 40), toggleSO, "" + (toggleSO ? "On" : "Off")))
{
toggleSO = !toggleSO;
Camera.main.GetComponent(ScreenOverlay).enabled = toggleSO;
}
}
function LevelMenu()
{
GUI.Label (Rect (430, 195, 290, 40), "Levels...", customGuiStyle);
GUI.Label (Rect (430, 235, 990, 40), "______________________________________________");
// GUI.Label (Rect (430, 275, 290, 40), "Coming soon...");
if (GUI.Button(Rect(430, 275, 90, 70), "Snowy Mountains")) {
Debug.Log("Load Alpine Level");
Application.LoadLevel(1);
}
GUI.Label (Rect (430, 415, 590, 40), "Helpful hint: Pressing 'ESC' in the levels brings");
GUI.Label (Rect (430, 435, 590, 40), "you back to the Main Menu!");
}
function CreditsMenu()
{
GUI.Label (Rect (430, 195, 290, 40), "Credits...", customGuiStyle);
GUI.Label (Rect (430, 235, 990, 40), "______________________________________________");
GUI.Label (Rect (430, 275, 490, 40), "Idea, Programming, Level Design: Maurice Pape");
GUI.Label (Rect (430, 355, 490, 40), "Some Textures from cgtextures.com");
GUI.Label (Rect (430, 375, 490, 40), "Some Sounds from freesound.com");
GUI.Label (Rect (430, 415, 490, 40), "Font: Walkway - dafont.com/walkway.font");
GUI.Label (Rect (430, 435, 490, 40), "Songs: Danosongs - The Streatham Hills Gods");
GUI.Label (Rect (430, 455, 490, 40), " ExileFX - Resurface");
GUI.Label (Rect (430, 495, 490, 40), "Special Thanks to the members of unity-insider.de for helping.");
}
StaticVarHandler.js (for “global saving”, i haven’t figured out PlayerPrefs yet, which is on every scenes maincamera):
#pragma strict
function Start () {
print ("AA is "+(NewMainMenu.toggleAA ? "on" : "off"));
Camera.main.GetComponent(AntialiasingAsPostEffect).enabled = NewMainMenu.toggleAA;
print ("Bloom is "+(NewMainMenu.toggleBloom ? "on" : "off"));
Camera.main.GetComponent(Bloom).enabled = NewMainMenu.toggleBloom;
print ("DoF is "+(NewMainMenu.toggleDoF ? "on" : "off"));
Camera.main.GetComponent(DepthOfFieldScatter).enabled = NewMainMenu.toggleDoF;
print ("SSAOEffect is "+(NewMainMenu.toggleSSAO ? "on" : "off"));
Camera.main.GetComponent(SSAOEffect).enabled = NewMainMenu.toggleSSAO;
print ("Color Correction is "+(NewMainMenu.toggleCCC ? "on" : "off"));
Camera.main.GetComponent(ColorCorrectionCurves).enabled = NewMainMenu.toggleCCC;
print ("ScreenOverlay is "+(NewMainMenu.toggleSO ? "on" : "off"));
Camera.main.GetComponent(ScreenOverlay).enabled = NewMainMenu.toggleSO;
}
My problem now is that my new script (for my new NGUI menu):
#pragma strict
static var toggleAA : boolean = true;
static var toggleBloom : boolean = true;
static var toggleDoF : boolean = true;
static var toggleSSAO : boolean = true;
static var toggleCCC : boolean = true;
//static var toggleSO : boolean = true;
function OnActivateAA () {
//print("TEST");
toggleAA = !toggleAA;
Camera.main.GetComponent(AntialiasingAsPostEffect).enabled = toggleAA;
}
function OnActivateBloom () {
//print("TEST");
toggleBloom = !toggleBloom;
Camera.main.GetComponent(Bloom).enabled = toggleBloom;
}
function OnActivateDoF () {
//print("TEST");
toggleDoF = !toggleDoF;
Camera.main.GetComponent(DepthOfFieldScatter).enabled = toggleDoF;
}
function OnActivateSSAO () {
//print("TEST");
toggleSSAO = !toggleSSAO;
Camera.main.GetComponent(SSAOEffect).enabled = toggleSSAO;
}
function OnActivateCCC () {
//print("TEST");
toggleCCC = !toggleCCC;
Camera.main.GetComponent(ColorCorrectionCurves).enabled = toggleCCC;
}
function OnActivateSO () {
//print("TEST");
toggleSO = !toggleSO;
Camera.main.GetComponent(ScreenOverlay).enabled = toggleSO;
}
isn’t working as I expected. For the Options menu itself, I’m using the normal NGUI checkboxes with these settings applied:
My problem is that, whenever I start the game, all ImageEffects are automatically disabled! The strange thing here: I don’t disable them via code or manually by myself. They should be activated from the beginning. As soon as I enter another Level and go back to the main menu again (via “if pressed ESC, Loadlevel(“MainMenu”)”-thing), all ImageEffects are magically enabled. But also this depends: Sometimes all are enabled, another time only one or two IE are enabled when i switch back and forth.
I, however, made another strange observation:
When I print out the bloom part (as example), the “TEST”-print itself is showing up in the console after I start the game. It appears that the code is already executing then. Normally, only when pressing on a checkbox, the code (in this case “function OnActivateBloom”) should execute.
Any idea why this is happening and how I can fix this?
-Mauri