Best Way to Build a House in Unity
Unity has several kinds of special folders. I of them is theResources folder. Simple concept of storing assets is well-explained in the official documentation:
Mostly, you create instances of avails in a scene to use them in gameplay but Unity likewise lets you load assets on demand from a script. Yous do this past placing the assets in a folder called Resources or a sub-binder (y'all tin actually have any number of Resources folders and place them anywhere in the projection). These avails can so exist loaded using the Resources.Load function.
All the same the reason why we might want to utilise theResources folder may exist a petty disruptive. First you lot have to understand how Unity build process is works and how Unity is able to access game assets.
Unity build process
Before you volition build your game, you have to declare what scenes your game consists of. All of this tin be done inBuild Settings window.
There are at least ii reasons why Unity asks you lot to do this:
- It needs to know what scene should exist loaded first (the top scene)
- It needs to know what assets should be included in your build (dependencies)
What are scene dependencies? They're avails which are connected to the scene bureaucracy in any way, usually every bit a component field.
The dependency diagram may look like this:
In this case there are two scenes. Scene one is using Asset one and Asset 2. Scene ii is using Asset 2 and Asset 3. What happens if yous make up one's mind non to build Scene 2?
Only Nugget 1 and Asset 2 volition be included in the build since Nugget three is referenced only by Scene 2, that is no longer included in the build. Thank you to this dependency trackingUnity will include in your build only these assets, which are actually used. Needless to say that you don't take to worry about storing assets you're not using at the moment. It will non affect your build size in whatsoever way.
Override!
There's a way to get around this procedure. If you lot put your assets into aResourcesfolder, they volition exist always included in your build. But be careful! You need a really skilful reason to do so!
Every bit I said before, in almost cases when you need to use an nugget, you make a reference to it within a scene. Information technology's really easy to utilise any kind of attached nugget this way. So why would you need to use an asset without keeping a reference to it? There may be several reasons and each one depends on specific needs of the, but let'due south await at ane instance what is quite common for most games.
When an nugget is directly referenced from the scene, it will be loaded into the retentivity before your scene will be launched. Thankfully to that, thespian will not experience any frame-drops related to assets loading (with minor exceptions). The cost is of grade the time needed for these avails to exist loaded. Sometimes it may exist not acceptable.
Example – loading screen with unlike backgrounds
Loading screen is something that usually is also a scene. Let's think of a case when you want to display a random paradigm on the background while your actual game level is loading. You've collected 15 images and you add these to loading scene images rotation script. It is working great, but when y'all play your game you realize that your loading scene requires more than fourth dimension to load than you need to pass your actual game levels!
This is caused by avails pre-loading mechanism and can be hands fixed usingResources binder. Showtime remove all the references to your textures from the scene. Then put your images into Resources/LoadingImages directory like this:
Then somewhere in the code yous can use a code like this one:
int texturesCount = iii; int textureId = Random.Range(i, texturesCount + 1); Texture2D texture = Resources.Load<Texture2D>("LoadingImages/" + textureId);
Note that Random.Range() returns a random number between kickoff argument inclusive and second argument exclusive, that's why there's +ane.
If y'all volition need to attach this texture to anParadigm component, you lot tin can exercise it like this:
Sprite sprite = Sprite.Create( texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); Image image = GetComponent<Image>(); image.sprite = sprite;
A word of caution
UtilizeResources binder only when you really need to. Loading assets on demand will make your FPS charge per unit drop, and having indirect dependencies is makes your piece of work much more difficult. It's worth to mention again that these assets volition ever be included in your build, even if you don't apply them. You have been warned!
Source: https://blog.theknightsofunity.com/unity-resources-folder-how-to-use-it/
0 Response to "Best Way to Build a House in Unity"
Post a Comment