Unity load scene in editor playmode synchronously - c#

Main Question
I'm trying to load a scene in the editor while in playmode and get the exact behaviour of:
SceneManager.LoadScene(ScenePath, LoadSceneMode.Single);
Except without requiring the scene to be in the build settings.
I would have expected this to work:
EditorSceneManager.LoadSceneInPlayMode(ScenePath, new UnityEngine.SceneManagement.LoadSceneParameters(LoadSceneMode.Single));
However the scene appears to load asynchronously, which I don't want for my particular use case.
Is there a way to acomplish what I'm looking for here?
Additional details (my use case and "why" I want this)
What I'm doing is creating a loader for scene sets that will additively load a bunch of scenes with different constraints on their loading behaviour (such as: load async, always reload when transitioning same->same, and other features). I have this working fine in the editor while I'm not in play mode, I have it working fine in a built player, but the last case is getting this to work while in the editor and in play mode.
The reason I need "single" loading at all is when you transition from one scene set to another and they share no common scenes. Since I can't unload every scene, I'm just skipping the unload step all together and having the first synchronously loading scene load with LoadSceneMode.Single to cause everything else to unload.
On the off chance it's relevant (I really hope not) I'm trying to do this from inside a custom editor's OnInspectorGUI call. I'm triggering it from a button press in there.
Options I'm aware of:
EditorSceneManager.OpenScene with OpenSceneMode.Single
problem: Does not work in play mode
EditorSceneManager.LoadSceneInPlayMode with LoadSceneParameters(LoadSceneMode.Single)
problem: Scene is not loaded synchronously
SceneManager.LoadScene with LoadSceneMode.Single
problem: Scene is required to be in the build settings
EditorSceneManager.LoadScene with LoadSceneMode.Single
problem: Scene is required to be in the build settings

Turns out I had a misunderstanding of what SceneManager.LoadScene(ScenePath, LoadSceneMode.Single); would even do.
A friend had pointed out the docs clearly state the load will happen within 1 frame, not actually at the call-site.
If you're facing a similar issue there are a few options. You could load async and subscribe to that callback to perform activation them. Another option is to subscribe to SceneManager.sceneLoaded and perform activation there.

Related

Unity (new) InputSystem does respond to mouse clicks

I am building this simple game, where a bunch of fellows that I have are supposed to seek a point indicated by a mouse click. The issue is that the Editor does not seem to notice mouse clicks at runtime. I have used the exact Input Action Asset before for detecting presses of "g" button, but it seemed to have stopped working when I played with it some more sometime later. I have since removed the asset and created w new Action Assets (one that I created manually and another that I created through the Input Action component button). Neither of them works. I have no idea what is going on and I have been looking at this for several hours now. Can someone please explain what I might have done wrong?
Different functions I used to try and get the Editor to respond to my code are below. MouseClick() is the original function that I needed to run, but did not work, onFire() is my attempt at running the default one that is given if Actions Asset is isnantiated through the component.
So it turns out that the issue was that the functions are supposed to be capitalized at the start.
Have you tried using the performed event ?
Also your Fire action needs to be a button type

Save current state of scene and load it

How do i save current scene state and load it later?
I'm making a game where you are building a bridge and then press start to run over it with a car and if you fail you have a restart button that reloads the scene from origin state(without anything built on it). However, i want the player to be able to press an "Edit" button that will go back to the state right before you pressed "start" so you can keep building on your bridge without having to rebuild the whole bridge over and over again. So, how do i do this?
If you don't want to code it yourself, you can use the PlayerPrefs of Unity. If you don't know how to use it look at the documentation (https://docs.unity3d.com/ScriptReference/PlayerPrefs.html) or you can also find tuto on Youtube, even some examples.

How to make a loading screen in Unity, that can figure out if the scene is fully loaded

I have my main menu; when the game start, it takes between 2 and 12 seconds to display the scene. During this time everything is still, and the user may think that the application is hanging or crashed.
I would like to add a simple screen (I can use a canvas and place a simple animation on it with the loading text on it), but I can't get how do you actually tell that the scene loaded.
I did try the delegate "OnLevelWasLoaded", and it is a big lie, because the code running in there is not necessarly happening when everything "pop" on screen and you can start to use the application.
I did try to add a timed function, but it is totally bogus, since I have no way to find out if the scene loaded or not, so on some machines it may be faster, and the game would run behind the loading canvas; while if I do it too short then the scene will not be fully loaded and I would end up showing again the frozen screen.
Beside OnLevelWasLoaded, is there anything else that can be used to intercept when the scene loaded?
You can use:
SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
LoadSceneAsync returns AsyncOperationthat you can use in coroutine.
Here you can find more details: Doc
So you can add a loading scene that will start to load proper level asynchronously, this allow you to check when loading is completed.

Where to put Fb.init() when changing levels?

I have two different levels and in my second level I want to use facebook to submit a highscore. So I've added the facebook sdk functions (from the fb doc) to my 2nd level unity c# script.
First time loading this level works fine but after I'm done and returning to the first level and loading the second level again it complains because fb.init is already loaded.
My awake() looks like this:
if (!FB.IsLoggedIn) {
FB.Init(SetInit, OnHideUnity);
}
Where do I need to put the FB.Init function so its not getting called again? Both levels are getting loaded again.
As user3811917 has stated, FB init should only be called once. There are many times in Unity development that you only want a function to be called once during the lifecycle of the application, and the most common way of addressing this is to create a separate loading or bootstrapping scene.
Basically, what this scene does is load, then immediately (on start perhaps) transition to the first scene of your game. By adding scripts to this scene (that run on awake), you can call several functions that will only be called once throughout the life of you application. This is useful for setting up third party plugins which need to be initialised only once. You can also place non-destroyable singleton objects in this scene, so that you can create and initialise scene persistent objects, that are only created once.
If you mean levels as the scene in Unity3D, You should call FB.Init once in Unity. Don't try it call method again.

Unity3d script seems to work in one scene but not another

I am working on a group of scenes for an opening menu (Start, Options, Exit, etc).
Right now, I have the exact same "Back" 3d Text GameObject in two of my scenes that have the same script that when clicked, uses Application.LoadLevel("Main") to return to the Main scene.
The problem is, "Back" only works in one of the scenes. It does not seem to be working in the other. They are identical (Script attached, position, name, etc.).
Any ideas what might be causing this or why this is happening?
Thanks in advance!
Fixed the problem by copying over all of the assets in the problem scene to a new scene. Now it works like a charm.

Categories