c# - Scene is persisting in the background even though I don't want it to -
i have strange issue game i'm making. reason, whenever change scene menu, menu can seen in background , buttons active. player tap on space menu button (while not being on menu screen) , unintended response. happens on 1 scene; other scenes work correctly. here's script problem.
using unityengine; using system.collections; public class menu : monobehaviour { //public texture2d background; public guitexture backgroundcopy; public guitexture background; float bgpos, bgcopypos; public guiskin buttonskin; public guiskin recordskin; public guitext guirecordlabel; public guitext guitime; public guitext guicomplete; public guitext guiturns; public guitext guilosses; public guitext guiquits; records records = new records(); //public static menu instance = null; private bool onmenuscreen; //this prevents menu buttons being pressed when i'm not on menu. weird issue. void awake() { //screenfader.fadetoblack = false; //create singleton there's 1 instance of menu @ time. //if (instance != null) // destroyobject(this); //destroys new instance //else // instance = this; //menu needs persist duration of game because want music keep playing across multiple screens. -this no longer case //dontdestroyonload(this); //load records records.loadrecords(); } // use initialization void start () { bgpos = 0; //initial x position of background bgcopypos = -screen.width; //this placed before original background. onmenuscreen = true; //i shouldn't need } // update called once per frame void update () { if (!onmenuscreen) //i need check because menu persists , remains on screen. { guirecordlabel.enabled = false; guitime.enabled = false; guicomplete.enabled = false; //i shouldn't need of guiturns.enabled = false; guilosses.enabled = false; guiquits.enabled = false; } else { guirecordlabel.enabled = true; guitime.enabled = true; guicomplete.enabled = true; guiturns.enabled = true; guilosses.enabled = true; guiquits.enabled = true; } //show records guirecordlabel.material.color = color.red; guitime.text = "total time: " + records.totaltime; guicomplete.text = "completion: " + records.completionrate + "%"; guiturns.text = "turn total: " + records.turntotal; guilosses.text = "total losses: " + records.losscount; guiquits.text = "rage quits: " + records.ragecount; //scroll background. todo: provide different backgrounds bgpos = (bgpos > screen.width) ? -screen.width + 2 : bgpos += 1; bgcopypos = (bgcopypos > screen.width) ? -screen.width + 2 : bgcopypos += 1; background.pixelinset = new rect(bgpos, background.transform.position.y, screen.width, screen.height); backgroundcopy.pixelinset = new rect(bgcopypos, background.transform.position.y, screen.width, screen.height); } void ongui() { //buttons gui.skin = buttonskin; if (onmenuscreen && gui.button(new rect(60 * title.scale.x, 400 * title.scale.y, 130 * title.scale.x, 33 * title.scale.y), "level select")) { application.loadlevel("levelselectscreen"); onmenuscreen = false; } if (onmenuscreen && gui.button(new rect(300 * title.scale.x, 400 * title.scale.y, 130 * title.scale.x, 33 * title.scale.y), "help & options")) { application.loadlevel("helpscreen"); onmenuscreen = false; } if (onmenuscreen && gui.button(new rect(540 * title.scale.x, 400 * title.scale.y, 130 * title.scale.x, 33 * title.scale.y), "back title")) { //destroyobject(this); //kill menu whenever return title screen. application.loadlevel("titlescreen"); onmenuscreen = false; } } }
i thought issue might have been second script have on scene, disabled , have same problem. here's other script.
/* script used allow music persist between screens. uses singleton prevent more once instance * being created. script must placed in menuscreen scene. */ using unityengine; using system.collections; using system.io; using system; public class musicplayer : monobehaviour { public static musicplayer instance = null; audioclip track; //copy of music playing. public audioclip[] musictracks; audiosource source; private bool musicplaying; private short tracknumber; void awake() { //create singleton there's 1 instance of music track @ time. if (instance != null) destroyobject(this); //destroys new instance else instance = this; dontdestroyonload(this); } // use initialization void start () { source = getcomponent<audiosource>(); loadtrackfile(); } public string trackname { { return musictracks[tracknumber].name; } } public short tracknumber { { return tracknumber; } set { tracknumber = value; } } public bool musicplaying { { return musicplaying; } set { musicplaying = value; } } /* create/update file save track number. */ public void updatetrackfile() { string directory = application.persistentdatapath + "/tile crusher/data/"; string filename = "trackfile.savefile"; //initialize track number if file doesn't exist. if (!file.exists(directory + filename)) { tracknumber = 0; } filestream fs = new filestream(directory + filename, filemode.openorcreate); streamwriter writer = new streamwriter(fs); //write track number file writer.writeline(tracknumber); writer.close(); fs.close(); } void loadtrackfile() { //start searching , reading files string directory = application.persistentdatapath + "/tile crusher/data/"; string filename = "trackfile.savefile"; //locate file. if doesn't exist, created. if (!file.exists(directory + filename)) { updatetrackfile(); } //read data. file read in specific order. filestream fs = new filestream(directory + filename, filemode.open); streamreader fileread = new streamreader(fs); //load track number string track = fileread.readline(); tracknumber = int16.parse(track); //done fileread.close(); fs.close(); } // update called once per frame void update () { if (!musicplaying && tracknumber >= 0) { //play music source.clip = musictracks[tracknumber]; source.play(); musicplaying = true; } else if (tracknumber < 0) source.stop(); }
what's bugging me issue occurring on 1 scene. other scenes work fine. used workaround, don't think need such thing 1 scene. can this?
i refactor menu class can disable a whole when no needed 2 reasons:
- the code on
update
needed when in menu scene - ongui pretty expensive called several times per frame
you can make empty game object menumanager
or better scenemanager
kind of persistent using object.dontdestroyonload. enabling , disabling menu
object can done in class.
maybe unity singleton manager classes can give inspiration. use approach keep hierarchy small , clean. scenes contain game object _applicationstartup
prefab contains initial bootstrap code , enhancements development. when project growing , has couple of scenes, highly recommend have scenes designed in way can testing scenes directly. when working on scene level-5, it's totally annoying if have switch main scene testing.
Comments
Post a Comment