features:
- Easy to setup, easy to use pooling solution, simplicity is key.
- Allows you to choose what to load on a scene by scene basis.
- Manage preloading of prefabs to avoid hiccups during gameplay.
- Manage how many elements a pool can contain, so you choose the perfect balance between memory and efficiency.
- Manage how many elements of a type can exist period, allows you to get all the control over allocations in your game.
- Organization, dont bother creating prefabs the old way anymore, let this tool organize them for you, automatically creates a prefab from a game object in the scene when drag and dropping on the group (see the tutorials on how to add elements to a group) and puts them in a folder created for you using the groups’s name. You dont have to look for the prefabs as well, use the custom window to select prefabs that are inside any group.
- Lightning fast performance and self managed, you really dont have to worry about keeping any kind of reference to any prefab, any instance of any pool, anything, just setup the atlas and use. The spawning is as fast as it can get.
- Dedicated development with over three years of improvements and counting. I hear the feedback of everyone and take it in consideration when adding/improving features, I always answer within 48 hours and you can count on my continuous support.
BASIC USAGE TUTORIAL
This tutorial seeks to illustrate how to setup step by step the Prefab Atlas and how to use it in code.
Open the Prefab Atlas Window by clicking on Tools->Prefab Atlas Editor:
By doing this, the prefab atlas will check if you have a prefab atlas object already created within your Assets folder, if it cant find one it will create one. You should see this window:
The window is divided in 3 sections (Groups, Prefabs, Scene Object Setup), this window reads your Scene Object, if it cant find one then it will display the option to create an Scene Object:
Click the Create Scene Object button and the option will hide. After doing this we require a new Group, click on the plus sign on the Groups section:
This will create a new Group with the default name “GroupName”, you can click on this group name to change it (Note: Groups need to start with a letter or code friendly symbol such as underscore “_”, this is because the groups get parsed into C#).
Now we only need to add an element to our new group, there are two ways of doing this, the first one is the classic way, drag and drop a prefab that was created previously:
But in recent updates, a new way of adding prefabs was introduced, and it is also the recommended way, simply drag and drop a GameObject in the hierarchy to automatically create a prefab from it and add it to the Group:
All the elements inside the group can be seen within the Scene object setup, where you can modify their Preloads, Capacity and Max Instance, a bit of explanation on each element:
Preload: The amount of instances the pool will create once the game starts, this is useful to avoid making instances during gameplay which could cause hiccups.
Capacity: The amount of elements a pool can hold at any given moment, this is useful to limit how much memory you want to actually hold, say you create 1000 elements freshly from your pool, but you only want to keep a 100, so you set your capacity to this number and once 100 go back in, the pool will stop accepting new elements and will destroy them, freeing up memory. -1 will disable this feature.
Max instance: Think of this a rule of how many can be alive in the pool or out of it, combined, extremely useful for particle effects or sound related elements. Only want 400 snow flakes at all times? set this to 400.
You will also notice a “DEFAULT VALUES” section in all groups, in this section you can massively apply the defaults to all the elements in the group, this is for convenience to quickly setup the values in mass, just press the “apply defaults” button.
Finally to conclude the setup of our Prefab Atlas, we require to commit our changes, press the button on the top right corner of the window:
Your project will reload if there were any changes, this is because the prefab atlas when you click commit it will parse the information from the editor to C# code that will allow you to use the prefab atlas in code and in the editor. That concludes the basic setup, you can create more groups, add more prefabs to the groups, and tweak your Scene Object setup.
Now onto the code usage:
Fire up your favorite IDE and create a new Monobehaviour class, I will name mine PAtlasExample:
We will first use the most straight forward way to use the prefab atlas, on the OnEnable function we will do a direct call and create an instance of the prefab we added to the atlas, remember my group was called “GroupName” and the prefab was called “Cube”:
This is doing the following:
- “PrefabAtlas.Spawn(GroupNameTag.Cube, new Vector3(0.0f, 0.0f, 0.0f));” gets the element from the pool with Tag “Cube” and creates an instance of it at position 0,0,0.
Note that there are several options for the “Spawn” function, you can specify its position, rotation, or none of them, if you dont specify anything it will create it at position 0 with the rotation also at 0 in all angles. Thats the most basic way to use the prefab atlas in code, now we will take a look at the PAT values:
The biggest difference here is that the code doesnt need to know before hand what it is creating, because the PAT values (Prefab Atlas Tag) are to be set within the editor. this makes it very handy when creating versatile game objects, for example, wave spawners in a tower defense game, or a bullet changing gun, or even different types of particles/sounds, the PAT is one of the features that makes the prefab atlas very versatile. Lets take a look at it in the inspector:
The PAT value (SpawnThis) is represented in two parts, the first one being the group, you can select any group inside the prefab atlas, secondly we have the ID of the prefab that only appears when a group is selected, this list will only display elements inside the group selected.
This feature also allows for some very nice organization (much better than dragging and dropping a prefab onto a public gameobject field). Once you have setup the PAT you will be able to spawn any prefab from this script.
Now lets take a look at how to return things to the pool in code. it is very simple and it doesnt create any intrusive or special calls, the only thing you need to do is “deactivate” your game object:
What the pooled object does is listen to when the prefab gets deactivated, and returns it to the pool. Simple as that, you can even destroy the object if you wish, the Prefab Atlas will handle the instance being destroyed as well.
If you would prefer some videos, here you go (I am terribly sorry for my accent):
Basic setup:
Examples of usage: