- Home /
Camera Switching or Scene Changes?
Ok this is something I have been kind of wondering for a while now. Lets say by application is simple just one big GUI or in my case many GUI's, I have them currently in different scenes but some of the problems I have is when I change scenes things get lost like "Trip Counters" and states of a button being on or off from the previous scene. I have heard that Singletons or whatever they are called can cure that but I have not been able to wrap my head around them or have any progress trying to figure them out.
Would just camera switching maybe be a better way to go in my case? Like for instance if I took all of the GUI's from all the separate scenes and imported them into one scene that had a camera set up for each of the GUI's. Or would that just make things more complicated?
Answer by cjdev · Sep 07, 2015 at 09:39 PM
Scenes can be useful as a way to organize a game, especially with large static levels. Personally though, I like to run my game with scripting in one scene with just an empty GO and scripts attached. Then I can instantiate anything I need on demand, such as GUI prefabs, whenever I need it and destroy it when I don't. But it's really just a matter of preference, the scene objects are there for your convenience so you should use them or not as your project demands.
Singletons are essentially just objects where only one of them can exist. You can create one to store data and have it not be destroyed when your scene changes so that your data persists between scenes. Camera switching isn't something I would recommend unless perhaps you need it for a small application just because of the added overhead the extra instantiated items create when you don't need them.
Thanks for responding cjdev, For a project like this what do you think would be the best way to go, scene changes for each different interface screen or camera switching, keeping in $$anonymous$$d too that Arduino is being used to communicate to external devices through the serial port.
Take a look at this video to get an idea of the scope of the overall project ;) [Demo Video][1] [1]: https://www.youtube.com/watch?v=IbD81ur$$anonymous$$0lw
With that many GUI screens and an external connection to worry about I guess I'd go with camera switching if it has to be a choice between the two. Ideally though making each screen a prefab and loading them from the Resources folder would make it much easier to deal with all of them.
Thanks cjdev, I'm using canvases for my GUI how would I go about managing so many different ones, like switching between them? Is there a tutorial anywhere that you know of that walks you through the whole process?
I'm not aware of any specific tutorials but the process is actually fairly straightforward. You just need to have an EventSystem in your Scene and then you make a prefab out of each of your Canvas objects and put them in a folder named Resources. Then when you want to load that Canvas you just do:
GameObject canvas = Resources.Load("CanvasPrefab") as GameObject;
Instantiate(canvas);
This way you can Instantiate and Destroy your Canvases on demand because you have access to the prefabs as a reference.
What if my timer text is on a canvas and it gets unloaded would that not effect the timer like stop it for instance?
Ins$$anonymous$$d of instantiating/destroying GameObjects, it might be easier to enable/disable them ins$$anonymous$$d. You can just drag the canvases (or any other GameObjects) into some public variables in a script that will enable/disable all of them. This removes the need for making prefabs.
Thanks Gkxd, Now would that be a good option give that I have a lot of canvases to work with, currently they are in different scenes but if they were all somehow merged into one scene would that still be a good idea?
$$anonymous$$eep in $$anonymous$$d these are full screen GUIs with a full screen PNG as the background with swapping graphic buttons on top of them, some full screen animations too.
You can essentially just drag everything into one scene, and hit the checkboxes in the inspector to disable/enable them. I haven't tried having multiple canvases in one scene, but I think it will work.
You can use a script to toggle those checkboxes by using GameObject.SetActive().
While your timer text object would be destroyed (if you were instantiating and destroying GameObjects) the timer variable you use to keep track of time would not be as it would reside in another class and object entirely, say a 'Timer' script component on a 'GlobalScripts' GameObject.
Thanks cjdev, This sounds like a very good solution, I sure wish though that three was a tutorial that explained this in step by step detail just so I could comprehend the process better.
Answer by chomps32 · Sep 11, 2015 at 04:40 PM
Scene switching is going to be the best way to keep your game clean and simple. The basics of Singletons is this:
1) There can only be one in your game. 2) Every object in the game can access its data.
Here is an example of a simple singleton:
public class Singleton : MonoBehaviour {
// Static singleton property
public static Singleton Instance { get; private set; }
bool accessableData; //type Singleton.Instance.accessableData to access this data from any other class
void Awake()
{
// First we check if there are any other instances conflicting
if(Instance == null)
{
// Furthermore we make sure that we don't destroy between scenes
DontDestroyOnLoad(gameObject);
Instance = this;
}else
{
//if the gameObject is already on the scene
Destroy(gameObject);
}
}
}
Watch this video to get further explanation. Including how to switch between scenes and why the Singleton is used.
Answer by KnightRiderGuy · Sep 12, 2015 at 04:16 PM
Chomps32, Ok i took a look at that tutorial, they are using the on GUI stuff and I'm using UI text for my trip timer. I'm not sure what I'm missing, I made this script and put it on an empty game object and then made a prefab of it.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class InterfaceControl : MonoBehaviour {
public static InterfaceControl control;
public GameObject timeText;
public int time = 00;
void Awake () {
if (control == null) {
DontDestroyOnLoad (gameObject);
control = this;
} else if (control != this) {
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
timeText.GetComponent<Text> ().text = "00:00:00:00"+time;
}
}
I them made a prefab of my "TmerText" and saved that as a prefab (Not sure if I needed too)? But I'm not getting something as I can't seem to get my timer to keep it's count across a scene change. I have attached a screen capture of my Hierarchy, inspector and prefabs folder, hopefully it will help?
[1]: /storage/temp/54169-screen-shot-2015-09-12-at-111309-am.png
Your answer
Follow this Question
Related Questions
Multiple Camera Switching 1 Answer
How do i remove the gui windows in my scene 1 Answer
have a camera from a different scene? 1 Answer
Display 2 Scenes at Once 1 Answer
ASP .NET: How do I link gui components to Unity web player? 0 Answers