In javafx ,I am switching multiple scenes. All scenes are on one stage. I want to retain the changes made in previous scene so that when i come back it will show the changes which had been made. How can i do this?
Related
I'm trying to make this game using the approach of multiple scenes to make things more modular.
In my actual case I have an "Initialization" scene which holds some global state objects and the one to control the state machine of all the scenes in the game.
As for the other scenes, for now I divided them just in two: the base scenes (which for now contains everything besides UI) and its UI scenes (which basically have a Canvas and all the UI elements and UI-related scripts).
The confusion in my mind is simple though: as I tried to make the UI scenes as modular and independent as possible, there are a lot of points of interactions between the base scene and its UI scene.
For the sake of illustrating this question please take this problem I'm facing right now: I have camera animations that should be played as a response to user inputs to the UI (like the click of a button should trigger a specific camera animation). Thing is: that camera is not in the UI scene. The way I'm resolving this problem right now is creating a ScriptableObject which holds events for important actions triggered in the UI scene that are fired in the UI scene and subscribed in any other place. The same can occur in the opposite direction: the UI scene need to react to many actions that happens in other scenes.
Considering that the "camera animation" problem I explained above can happen with many other objects, if there is not a better way to handle that wouldn't splitting a game into multiple scenes be just too much of work just for the benefit of modularity? And by that I also asks: am I handling this problem the right way?
If you want to keep things consistent between scenes, there are a few ways to do it.
PlayerPrefs lets you keep variables consistent, I don't need to do a whole tutorial here, look it up.
DontDestroyOnLoad lets you take an object and make it consistent throughout the whole game. If you want, you can use DontDestroyOnLoad on one of your cameras and just delete the others in the other scenes if you want to keep a consistent camera.
I don't know if this is some sort of bug or (which is likely) I'm doing something wrong, but I have two scenes.
menuScene is a starting point and it suppose to have nothing but canvas with some input fields, sliders, labels and a button.
Clicking the button moves to gameScene, which (by default) contains some objects.
This is what I see when I run the project:
What do I need to do to make only menuScene visible on launch?
In general: Unity 2020.1.0b12 is a BETA version and not unlikely to have a lot of bugs. If you are not investigating the most newest features but rather want to implement a serious project you should stick to the latest stable Unity Version (currently 2019.4 LTS).
You have opened both your Scenes in the Editor! In your Hierarchy I can see both scenes menuScene and gameScene.
In this case SceneManager.Load loads your already existing but currently unloaded (disabled) scene (basically only enables it) but keeps the other scene(s) in place.
Note that within the Editor Multi Scene Editing is possible and even if you then start the PlayMode both scenes are still there.
However, in a build later only the top-most scene of the list in the Build Settings is loaded in single mode!
The follow up error you got is due to your scenes not added to the Build Settings. Simply drag & drop your two scenes into the list in the Build Settings.
Then open only the first scene in the Editor (click Remove on the other ones) and then enter PlayMode again. Now it should load your scenes in single mode
We are building a mobile app built in Unity3D, which involves a lot of UI switch. For example, from this page to the other page, it involves a lot of disabling the previous UI and activate new UI elements.
A common way to do is to set old UI inactive and new UI active. When switch back, just deactive/active again. But I read that this will cuase Rebuild problem (Canvas.SendWillRenderCanvases or BuildBatch). I wonder what is the alternative?
Will these two solutions work?
Instead of activate/deactivate UI element, apply it on the UI canvas.
Deactivae the UI element by moving it far away. Will it cause UI Rect rebuild?
Set in different layer, change its layer to invisbile layer when deactivate.
I would discourage you from using a state machine for your UI if you consider developing an application with many UI layers.
It will result in a scene hierarchy with hundreds of objects, some of them only been used when going deep down the app (like settings of a specific page) but they end up in memory at all time.
Also, it makes things harder to debug since you cannot really jump to a specific state without going through all the previous ones (except if you are already advanced prog and you defined your classes so they can work from anywhere).
The easy solution is to use scenes to separate your states, just like it is done in Android/iOS with Activity/Fragments or views.
This will clean you memory when needed, you can still keep scenes alive on the stack (like mobile OS does) using additive scene loading, since you won't have a lot of objects to create, the scene changes will seem seamless.
You can easily reuse code over scenes with inheritance and extension with scene name:
public class MyUIClassWithManyCommonCode:MonoBehaviour{}
public class FrontPage : MyUIClassWithManyCommonCode{}
public class CatalogPage : MyUIClassWithManyCommonCode{}
And finally since, you limit the amount of objects in a scene to only what should be seen, it is easy to find an object in the scene.
My iOS game has a couple of scenes. I've noticed some lag between switching scenes, and I was wondering if it might be because I'm not removing all nodes and labels from parents when I transition to another scene. Is it good practice to remove all nodes from their parent when transitioning to another scene?
Also, I've noticed that when I do remove all nodes, the transition effect is kind of ruined, as the screen goes all black during the transition.
Is it possible to delete nodes(of previous scene) after the transition to next scene?
When you perform the transition, the scene and its nodes will be released from memory, unless you have a strong reference cycle. Also, you should know that SpriteKit has its own cache system for the SKTextures, so not all memory will freed.
The lag could be caused by a lot of thing, some possibilities:
If you instantiate the new scene on touchesEnded (or your custom button callback closure), the lag could be caused because you're doing too much work on the initialization. This can be solved by, for example, preloading the scene with a closure that run in background and, when you have to run the transition, you already have everything loaded. An example follows:
Maybe you're using assets that are too large and because they take longer to be loaded, you have the lag. You could solve this by, for example, converting images that don't need an alpha channel to .jpeg.
Another solution would be to preload assets. A code example follows.
This question already has answers here:
Unity game manager. Script works only one time
(5 answers)
Closed 6 years ago.
I have a game with three scenes, two level scenes and a menu scene. When I press play on my level scene the first time, everything works perfectly, but when I go to the menu scene and then later return to the level scene,
the references on the scripts attached to the NetworkManager GameObject were reset. On the scripts shown in the picture below, for example, all but the references that were linked to prefabs in my assets were reset.
I've tried assigning each and every variable with code instead of doing it manually, but even that doesn't work.
This GameObject does have dontdestroyonload on. Why are these references getting reset specifically on this object?
I don't have enough rep to comment, but did you save your scene after you added the references to the objects?
Answer (By Aaron Ge): I unchecked Don't Destroy On Load and Run In Background, and then my issue was fixed. I am unsure of how this fixed my issue, but it did.
Possible Explanation: http://answers.unity3d.com/questions/734445/scripts-in-other-scenes-still-executing.html
If you use DontDestroyOnLoad on objects that are placed in a scene, make sure that you either:
Load that scene only once. That's usually called a loading scene. The scene just contains certain manager objects and it immediately switches to the menu scene / whatever... You would never load that first scene again.
If you want to load the scene again and you used DontDestroyOnLoad on one or more object, you have to destroy the objects manually. You can't prevent the objects from being created again since they are part of the scene. Usually al objects get destroyed automatically when you load a new scene, DontDestroyOnLoad prevents that.
I unchecked Don't Destroy On Load and Run In Background, and then my issue was fixed. I am unsure of how this fixed my issue, but it did!