Building Unity UI that scales for a real game - Prefabs/Scenes?
Learn how to build a UI that works across multiple scenes, loads dynamically, and binds to whatever you want in Unity. I'll show you the steps I use to create a user interface as a separate scene instead of a prefab, the collaboration benefits you'll get from it, and how to bind everything easily so it all dynamically updates without writing too much code or overcomplicating things.
More Info: unity3d.college
Join the Group: unity3d.group
Patreon: / unity3dcollege
Learn more about UI in my Architecture Course: game.courses/game-architectur...
Пікірлер: 128
Here's the link to the code / scripts folder download - bit.ly/3azVykW
@gktt5274
4 жыл бұрын
maybe the same could be done with Extenject - it has great support of binding and stuff injection. You can even declare an interface and you can bind class implementing the interface and easily change whole systems: assetstore.unity.com/packages/tools/utilities/extenject-dependency-injection-ioc-157735 I highly reccomend it :)
@cyberpunkdarren
3 жыл бұрын
Thanks for the sharing this code Jason. You're a true maven in this field and your videos have inspired me to start designing and building my game idea.
@mioeu5399
Жыл бұрын
Hi Jason! What do you think about MVC and similar patterns in Unity? I'd like to hear your thoughts on that matter as many developers try to apply it for better or worse and I have mixed feelings about its practical usage.
Another great tutorial Jason! I was searching for how to do this about an hour before you uploaded. I must support you on Patron!
Great video! I like how you provide solutions that can be used in real projects and do not stop where it gets a little more advanced. It also do appreciate the work which must go in setting everything up so the explanation is well structured and everything works flawlessly. Would love to see more videos of this kind.
Thanks! Your Videos and Tutorials are on the level that I need right now and it's very useful to me!
@codeHusky
3 жыл бұрын
Didn't expect to find you here, lol.
Well written. Putting the UI in a separate scene... seems like a good idea. I'll try it.
@denisgoodman4492
2 жыл бұрын
Yes, But Load and Unload "UI scene" every time when user turn On or Turn off interface not very good. May more efficient to make the Scene "Game Manager" and put here the UI + other related stuff and mark it as "DontDestroyOnLoad scene" + add script which will hide and unhide the UI. But the solution to change the content of UI ("on fly") by use Dictionary is very nice, is Awesome, i didn't see it before
Very interesting idea about having static events and exporting UI into own scene. I have to give it a try. Thanks!
Thanks Jason! Nice idea putting the UI into its own scene. The data binding stuff is cool!
It' s more about event system for me, and it helped a lot to understand event system better. You rock Jason bro, thanks for the effort.
That debbuing thing at 14:00 is probably one of the most useful thinks I've ever seen. Thanks a lot!
@pedroromanobarbosa9990
3 жыл бұрын
He is probably using Rider, the Jetbrains IDE fro C# .NET projects, which is a very nice IDE to work in as with almost all of their IDEs
this was great!, some people do tutorials, your's feel more like a class. I've specially found useful all the content related to game architecture, as that's been for me the most obscure part of learning game design self thaught cause is so hard to know about these thing until you are deep into the consequences of not taking them into consideration in early developement phase. I wanna correct my self now, im not self thaught, i'm self motivated, it's actually people like you, brackeys and others that actually guided that motivation to become learning and progress, so i sincerely thank you for your time and for your generosity towards the community.
Took Me a few passes of this video to fully get was going on. A lot of information for my little brain lol awesome concept Jason. I really like this Idea cleans up so many things. Thanks
Thank you for this very informative tutorial. Now I have a much clearer understanding of how to use events and callbacks rather than dumping everything in Update()! When I read the title, I was kinda expecting a tutorial on how to make UI scale across all devices (especially for mobile). Would be great if you could share with us any best practices for that :)
Excellent tutorial as always!! Thanks so much.
Jason, you are a legend!
i just want to say: Thank you!
Thank you, unity branded Dan Harmon, great tutorial :)
This is SO helpful! Thanks a lot!
I noticed that Unity released a Best Practices Guide book for free today. I was wondering if you could go through it live or in a video in the future. I bet you'd have a lot of great stuff to say about it.
@Unity3dCollege
4 жыл бұрын
Sounds like fun, gonna check it out!
@jahoopyjaheepu497
4 жыл бұрын
Can you please link us the guide?
@Chris_t0
4 жыл бұрын
Please send a link I am curious
@Chris_t0
4 жыл бұрын
think he means this create.unity3d.com/nine-ways-to-optimize-game-development
@l2mbu2ra
4 жыл бұрын
@@Chris_t0 Yes I did, thanks for linking it
I do like that you use event-driven approach via public events. No wonder, because I've probably learned to use them in Unity in one of your earlier tutorials. Your channel is pure gold for Unity programmers.
thanks for the vid, really good stuff, well done
Thank you. Did not know that Events works across scenes.
I’d love to see a video going more in-depth into a multi-scene workflow :D
Another great one!
Jason, please keep the advanced tutorials, demonstrations and talks coming, I love them. I listen to your videos in the background while I code, or I sometimes binge watch them and learn about 5 or 7 new Unity skills in one sitting lol. I've just come to Unity after being away from coding a few years, but I had a solid background in C, C++, C# and DirectX ... it's been a fun and smooth transition thanks to videos like this! 👍
Wou it solved a lot of issues in my game, thanks
You are a genius!!!! Thank you so much for sharing this!!!!!!!!
Thankyou! Helps me a lot
Thanks for the great video, awesome ideas. I'm just wondering isn't the value passed in the invoke of the Action, why would we set another static property in the object and call the handler again? Correct me if I'm wrong.
Hi Jason, thank you for this video and the many many more on your channel. I wanted to ask how does the rendering of the statlist work? It looks like when adding to the stats array the UI renders the element automatically in the canvas.
tHIS IS EXACTLY WHAT i NEEDED
@22:49 - place holder. - ironically I was designing my UI's IN separate screens its nice to know that i can just call them additively.
This will definitely save us some trouble! 👍🏽
@Unity3dCollege
4 жыл бұрын
Glad to hear it :)
Hey, I am curious on the class StatData, would it be sensible to have this as a struct instead? If not when would it be appropriate to use it? Also, would it make sense to have the health value as a stat?
I havent started a thing but im happy a answer is ready
In these days UI Builder will be released, I think it's pretty interesting
Hey Jason, thx for that really nice and huge tutorial. I've tried to use the UI as separate scene too, but I have a problem with the camera. I can't really figure our why I can't see the UI. I'm not quite sure what settings are necessary for the main camera or if I have to use an overlay? It doesn't matter in which order I load the scenes. even unloading and reloading the UI scene doesn't let it show up. Do you have any suggestions where I have to search the problem? Greetings Umoy. and btw.. keep on rolling out these nice kind of little serious helpers in game dev :) *thumbsUP*
Just wondering are you planning or working on a video about designing a game with mods in mind. I cant find information outside of its needing to be designed from the beginning. I know about xml modding but what about object or script modding?
I was just trying to do something like this myself, and this video is helping me with a lot of the issues I was facing. By the way, what do you think of Unity's new UIElements? It's sort of like building UI using HTML and CSS.
@Unity3dCollege
4 жыл бұрын
It seems like an interesting system, just waiting to see where it ends up before buying in fully :)
How would you combined this with a hotbar? A year ago you did a video about hotbar do you think the hotbar should be included inside this ui system or do you think that it should be separate system?
This is great as usual, Jason, but I do have one question - how would you cases where changing one stats affect other stats? For example, increasing strength would increase health and damage, which, I don't know, would increase the threat level of your character and give some sort of a percentage boost to critical damage to boot? Here, a change in one stat can effectively trigger changes in multiple stats. I always found this proliferation difficult to scale, often resulting in me relying on simply constantly recalculating stats on the fly and refreshing the text via Update, rather than using events.
Jason, this is incredibly valuable. It's been atrocious working with UI Prefabs across levels, especially when certain values have been overwritten by accident and new changes don't reflect without reverting. Is it good practice to keep all UIs in the UI Scene and manage them with the observer pattern? I'm working on a mobile game and was worrying about the performance of having multiple canvas/panels all preloaded and taking up memory even if they don't pertain to that scene.
My go to solution to the problem presented at the beginning is DontDestroyOnLoad. Curious to see what you come up with...
Cool video
When I first started learning to code they simply didn't have videos like this teaching you everything. You had to get on some programming forums or an IRC chat to get help and ask questions, and a lot of the really skilled guys were mean and didn't want to talk to a "silly n00b", lmao. Nowadays you can learn almost any skill on KZread, even quantum astrophysics. 🤣
How would you "glue" this together without the static event? I split my UI to multiple classes for example "MainMenuView", "DialogView" etc and I have state classes which need different UI classes to show, for example the DialogState has to communicate with the Dialog "view" to show/hide it and set some parameters. Currently I just pass the needed view(s) in the state constructor. But the "problem" is that the classes which create the states needs a reference to all kind of views, otherwise it can't create state instances. Is there any good solution/pattern for this kind of things? Currently I have one class which creates all the state instances and this class is a MonoBehavior and has all kind of views as serialized fields, so it can create the states. It works, but I would like to here if there is a better pattern for something like this.
What do you think of the method of not destroying ui on load and then just re initialising data on scene changes
Because some people were confused, Jason's use of the class "Entity" is his own custom class and has nothing to do with any existing C#/Unity frameworks.
Thanks!
There are lots of Unity tutorials on youtube, but it's hard to find any with clean and well architected code. Thank You. I have one question btw, Is it normal to use multiple Canvas Layer in your scene? For example one for Player Health, another for Weapon HUD, another for GameOver and so on?
@iamDRW
4 жыл бұрын
It is best to split your canvases to achieve better performance because of update things. The best person to explain this is the man in the velvet suit in a unity 2017 talk, and I think still holds true today still (could be wrong, though): kzread.info/dash/bejne/kausy9aglNuThaw.html
Love the video, very useful, but the title is misleading. I thought this was about how to make a UI that scales to different resolutions
May be I wrong, but I think if UI turn on and user switch to other Level, in this moment the UI "show wrong information", because it's a data from the Entity from previous Level. And it will be corrected after user is selected any new Entity on the new Level, then the UI will become synced with Entities. May be will be right at changing the Level turn off the UI automatically? Maybe I something miss in your explanations. And other interesting the strategical question: Is the the class Entity must be a part of Level, may be it must be a part of some Gamebjects which exist on some the MAIN scene, which exist always?
Does using the [SerializeField] attribute on a private variable function similar-ish to using the "protected" access modifier in a traditional C# application, in that you can see it in Unity, but that variable is not reachable through code from another class? Basically you're saying "I want to set this value once through Unity, but I'm not going to be setting it programatically from another class anywhere else." Am I understanding this correctly?
@phobos2077_
4 жыл бұрын
You're messing up unrelated concepts. Member access modifiers (private, protected, public) and unity attributes that alter serialization behaviour (like SerializeField) are different things. By default unity serializes only public fields of a class. By adding this attribute you force it to serialize a field, even if it's private or protected. That's all.
37:28 what is written in line 23?
I'm a little disturbed by the use of static events. Just the assumption there will only be one of something at any time I get my new computer tomorrow and will be able to start making unity test projects. Maybe I'll come around to the way of thinking eventually
Null propagation in general is the better way because the operation of checking whether the event subscribers is null and if not invoking the subscribers is atomic. That's of value in a multithreded application where another thread could remove an event subscription between the if (MyEvent == null) check and the call to invoke MyEvent. Maybe that's not a concern with Unity I'm only a noob with it. But anything that's of no cost that you can to to avoid potential issues is a good thing.
Thanks! But how to render ui camera over gamescene camera? In my case when i press F1 it loads empty scene with only UI.
@jarekshark1356
2 жыл бұрын
Nwm. It's Background option
Interesting, and I was wondering if there is a reason why you do it that way : loading a scene first, and loading / unloading the UI in "additive" mode Why not having by default the UI which remains forever, and then loading / unloading levels in additive mode ? It seems more logical to me... Am I missing something ? (I'm developing VR apps, I plan to use your trick replacing UI by the "VR headset + VR controllers" stuff. It's seems more logical to me to have this part loaded by default, and eventually load / unload levels in additive mode)
Gracias por el video, lo voy a mirar con tranquilidad
If I understand null propagation correctly, we can write the part at 19:13 : if (_healthPanel != null) _healthPanel.Bind(entity); like that: _healthPanel?.Bind(entity); Am I right?
@PerfectlyNormalBeast
4 жыл бұрын
I was thinking the same thing :)
@anthonymarquez2542
4 жыл бұрын
Prolly super late but yeah it’s basically what’s known as syntactic sugar, which is just condensing verbose common patterns or logic checks into something more succinct and readable.
It seems all of those best practice solutions end up revolving around solving the binding problem with statics. So basically the same thing as singletons. Also I don't understand what's different in your solution by placing UI objects in a Scene instead of a Prefab? The only difference I can see is that designers are restricted from adding any overrides to UI, which may be a good thing. But it doesn't help to solve the original issue (which is basically worked around with a static event field).
Hello, one question about the UI Why is it better to divide the health and stats in different canvas instead of different panels? I tried to search an explanation on your videos but you have a lot Thanks in advance :D
@blockify
Жыл бұрын
I’m a little late, however, I read somewhere that if your UI is causing your game to lag (too many objects) it’s recommended to split it into as many canvases as possible, it’s got something to do with how unity renders canvasses. The big O notation must be exponential.
1:33 what i find as solution without watching the video is to have a prefab canvas with a canvas singleton/instance code that hold referances of itself that i can call from outside by calling that code. That solve reference problem because referances hold tight on prefab
wait - are you passing around entities now? So you're working with ECS and not monobehaviors? (edit: oooohhh... it is a monobehavior named entity)
You had me at "load the UI scene additively"
Why there's no links to assets in description?
@pawciut
3 жыл бұрын
Finally I've found this :D Village, houses etc assets assetstore.unity.com/packages/3d/environments/landscapes/rpg-poly-pack-lite-148410 Skeleton King asset assetstore.unity.com/packages/3d/characters/humanoids/fantasy/skeleton-king-92552
Where is the documentation for 'Action' or 'Action' ? Searching the online Unity manual and scripting ref for 2020.1 and for 2019.3 turns up nothing. EDIT: Fount it, it is a M$ thing, docs at their C# dotnet system website area, (search docs.microsoft.com).
How did you get the parameter names "start, dir, duration" on Debug.DrawRay()?
@Tymon0000
4 жыл бұрын
You can just type parameter names in C#. For example if you have method declared like this: *void MyMethod(int myInt, int otherInt){}* You can call it like this: *MyMethod(myInt: 4, otherInt: 5);* and it will work as expected.
@DogsOnAcid
4 жыл бұрын
@@Tymon0000 is there a way to populate the method with parameter names automatically instead of typing?
@Tymon0000
4 жыл бұрын
@@DogsOnAcid Probably, idk.
@RiorXD
4 жыл бұрын
@@DogsOnAcid if games could code themselves everyone would have made a game already. You need a reference to something before it can auto add which would still require atleast starting to type.
Can we get a UI Toolkit tutorial please? =)
getting mind blown at @1.24 when realising multi scene editing is possible and forgetting why we are here in the first place...
Are these ECS entities? Or some custom Entity class?
@karmoq_
4 жыл бұрын
Since he is comparing them to "null" and not "Entity.NULL", I guess they are custom entities
@euden_yt
4 жыл бұрын
Sylvain Dubrofsky is just a script called entity, an abstract way of saying any interactive character/object.
My ui only worked in original level it created..even after prefab it ..it wont work in other levels..you saved me.
What about DontDestroyOnLoad?
i love you
Interesting but this may be too long a video, I find myself quickly lost in it (I'm doing other things there's that too XP). Again, a diagram before and/or after for what is needed/how do we want to do things could be useful. :P
how would you do floating healthbar with 100+ characters?
@daslolo
4 жыл бұрын
since unity events generate a copious amount of garbage...
@Ziplock9000
3 жыл бұрын
Object pooling
That devil in the thumbnail reminds me of Black & White
I think ideally prefabs shouldn't reference other prefabs. And using events to communicate between prefabs. This way they can be easily reused without any hassle.
I'm confused as to what the difference between using a Canvas and not using a Canvas is? In your tutorials, you never seem to use a Canvas when designing a UI.
@Unity3dCollege
4 жыл бұрын
Its automatically added when you make a ui. I just don't talk about that part normally:)
5:46
Entities are just too much for my brain... I just learned about them few weeks back in Master Architects and I think this will take months for me to understand all that...
My player controls his ui. Itll zone with him. Why wouldnt it.
@Ziplock9000
3 жыл бұрын
Because some UI elements are not associated with the player logically, just like in this videos example
Jason start your Discord Server also
these tutorials have A LOT of info.. but damn i'm such a noob that sometimes I get lost :/ nevertheless info is info.
@tyleranders9972
4 жыл бұрын
I'm having the same issue. I know this is important stuff I'm just not here yet
Developing an MMORPG with C# in Unity 3D. Follow the DevLog, Listen to music and more at twitch.tv/snstarrgames
First :)
Hey, I sent an email that says urgent! I could use your advice please!
Minimalist. Not one scriptable object.
Please use dark theme visual studio like every normal person.
Calling this a ‘best practice’ and not showing the input system is …. 👎👎👎
Jason, you are a legend!
Thanks!