1 Line Fix for your Unity Project
Vložit
- čas přidán 24. 10. 2023
- game.courses/mp/ - Multiplayer Mastery Course
* for anyone who wants the code, here it is
using Unity.Services.Core;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Bootstrapper : MonoBehaviour
{
async void Start()
{
Application.runInBackground = true;
await UnityServices.InitializeAsync();
if (SceneManager.loadedSceneCount == 1)
SceneManager.LoadScene("CarSelection", LoadSceneMode.Additive);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Init()
{
#if UNITY_EDITOR
var currentlyLoadedEditorScene = SceneManager.GetActiveScene();
#endif
if (SceneManager.GetSceneByName("Bootstrapper").isLoaded != true)
SceneManager.LoadScene("Bootstrapper");
#if UNITY_EDITOR
if (currentlyLoadedEditorScene.IsValid())
SceneManager.LoadSceneAsync(currentlyLoadedEditorScene.name, LoadSceneMode.Additive);
#else
SceneManager.LoadSceneAsync("CarSelection", LoadSceneMode.Additive);
#endif
}
}
A whole video with examples and use cases of this, please.
great format, fast, and on the point! Not all topics can be covered this way, but this one is :D
It's really useful for error logging as well. Let's say there's something SUPER CRITICALLY wrong with your build and it crashes on load. We can accomplish basic sanity checking by reporting program state as CLOSE TO the program execution as possible.
So do something like "write 'boot success' to log file" so even if you can't get your program to render, you know AT LEAST that you made it to a point where Unity can fire off that static function.
Obviously you can extrapolate more data from there when debugging.
Hope this helps!
Already knew this, but I like the fact that the video is short. Thank you for not beating arround the bush !
Extremely useful . Thank you for this tip.
Oh wow. This is awesome... I have been using a Bootstrapper scene for quite a while, but it's always annoying when I don't want to follow the default startup sequence for whatever reason. This little trick of forcefully loading the Bootstrapper in the editor on entering play mode and then resuming to the loaded scene is really neat! Great tip, Jason, thanks!
Perfect! Bootstrapping is a must.
Thank You for sharing these Quick useful Tips. 💯👍🏻
Simply amazing! Thank you very much!
i was looking for exectly this for my last steam project and didn't find it, now i know :) Thanks Jason
I really like this style of tips video, it's very helpful thank you 🙏
I'll have to give this a go! I've built out some custom editor UIs that essentially do the same thing with some optional parameters (can disable bootstrapping, specify the bootstrap scene, etc) but this seems like a really clean and fast approach, especially for newer projects.
Thanks so much for this!
Thanks Jason keep them coming! You know unity and all its assets make it easy to drag and drop an entire game. But I'm slowly learning to code as I go. Almost like backward engineering!
Short and Sweet!
helpful as ever Mr W, good stuff :)
great tip thanks!
Sweet. Thanks ☺
Nice tip !
Feels somewhat familiar like it's the startup class in a webapplication for businesses
honestly, no idea what you was trying to show here :D
this is GREAT tip. You don't get this in tutorials. THANK YOU!
I have a "checkbox" in my Unity Editor UI called "Load First Scene" attached to this script so I can enable/disable it whenever I need, before entering play mode.
You can see it in my videos showing Unity Editor UI, on top of the screen, next to the play/pause buttons.
how did you put that checkbox there?
Thank you! It actually made sense. I used to use a singleton and check if bootstrap is loaded or not. Your method also makes sense.
But, how about bootstrap has some async operations or coroutines, so I need them to finish?
I still understand the purpose of bootstrapping. I understand it can be used for persistent scenes
I'm pretty new to unity and mostly been using it to make vrchat worlds but I want to create my own game one day and bring it to life so anything is helpful. Thank you for this I keep this in mind when developing.
I found I can set play mode start scene, using same initialize on load scheme:
UnityEditor.SceneManagement.EditorSceneManager.playModeStartScene = (SceneAsset contining boot scene)
And then when hitting play that scene gets loaded no matter what scene was opened in editor.
I don't know why I constantly start new projects because I keep having this issue I use urp assets and I start a new project that is urp but I still have this issue with the assets being pink it just becomes discouraging that nothing ever works when I try it
For what do you build in the Start() Method?
You make a Bootstrapper Monobehavior which loads other scenes additively. And where does the GameObject that has this Monobehavior live? Is it required to exist in every other scene? From what I see in the documentation, it looks like that attribute will call a static function on any class, so you wouldn't need to have a BootStrapper GameObject at all. Is this correct?
Correct. The Game object and scene part is totally optional
I've been developing a fan made Smash Bros. for years, and am embarrassed to say, I don't know wtf a boostrap is.
I wish you could clone yourself and start an unreal channel :P still, in a way i learn more trying to conceptualise it in unreal terms
Okay so while it's cool that you show the code and such.... WHY should someone use this? Like what's the use case? What benefit does it give to the project?
One idea is because a lot of time you want certain objects to be persistent across the entire game. You can put it as a prefab in every scene and move it to DontDestroyOnLoad, but then you have to manage what happens when scenes load containing that object. This way, you can have certain code run when the game runs, regardless of what scenes are in your game, or how they're configured. This way you can guarantee your editor play mode starts the same way your built game does
I also want to know the point.
If you have fast startup enabled in the editor settings you have to use this method to call a method that will reset/cleanup any static variables in your class, otherwise you’ll see weird errors while running the game
@@PixelDough I've been using one scene and pooling and just replacing everything in the scene 🤣
Also you can init your services in right order, I guess
I like cookies
Didn't understand
Is Good, But (IMHO):
SceneManager.loadedSceneCount = SceneManager.sceneCount ?
and
Must be Noted that It demanding to install Service.Core package and include Unity.Service.Core in corresponding asmdef
cant see any value in this.
My line that fixed unity was rmdir
Unity is as trash as the company behind it.
why VS underlines red "using Unity.Services.Core; " and " await UnityServices.InitializeAsync(); "