Unity Prefabs (Unity 2018) – Overall Coverage

August 24, 2019

Unity Prefabs

Youtube – Unity Prefabs – The Complete Animated Guide | Game Dev Classroom

By: Lost Relic Games

While this tutorial on Unity prefabs is slightly behind (the version they’re using in the tutorial looks to be 2018.3.5) it still covers a lot of the fundamentals of using prefabs in Unity, as well as some of the pros and cons of using them and how to use them properly. It will still be important to follow this up with more recent Unity prefab information since some concepts (like prefab variants) may already outdate some of the information in this tutorial once I understand them better.

The basic starting point is that a prefab is a container for a gameobject and all its children that you can package together into one nice, tidy, and reuseable object. You can then make several instances of this object from this one prefab.

Editing Prefabs

You can approach editing prefabs and their instances from multiple directions. You can edit the master prefab and apply those changes to every other instance of the prefab. You can edit a prefab instance, and then apply those changes back to the master prefab, and then apply those changes back to all other instances. You can also edit individual instances to have them vary from the original prefab.

You can edit the master prefab in its own prefab window now. When doing this, these changes default to being applied to all other instances as soon as they are committed to.

When editing an instance, you have options on whether to push those changes to the master or keep them separate. This can be observed with the overrides tab at the top of your prefab instance in the inspector. This lists all of the differences between your master prefab and this current instance, so you can see and decide which, if any, changes to push to the master prefab.

Nested Prefabs

You can child prefabs into other prefabs. The child prefab is still its own prefab that can be edited, and the change can be reflected in the parent nested prefab. The parent prefab now just contains that child prefab as part of its overall prefab package.

Using Prefabs

While they are generally thought to be used for objects that you will make many instances of (rightfully so), there are also benefits to having core objects of your game be prefabs as well. Objects such as your scene managers or player controllers can be prefabs to serve as sort of a backup object. If you are working on these types of objects in an isolated instance in a single scene, and somehow you lose that object or that entire scene, it will be gone forever. However, if you made these objects into prefabs, they will still exist as their own object in your assets.

Unpacking Prefabs

There are two versions of unpacking prefabs: unpack and completely unpack. Normal unpacking will just make the current prefab instance you have selected not be bound as a prefab anymore. All of its individual parts will be freed from the prefab connection. If there were any nested prefabs within this prefab, they will still be their own prefabs. It is just the overall selected prefab that is no longer a prefab. Completely unpacking however will unpack the selected prefab as well as any child nested prefabs. This ensures every single object in this selected object’s hierarchy is no longer connected to a prefab in any way.

It is important to note that unpacking a prefab does not destroy your master prefab object. It simply makes the selected prefab instance not a prefab anymore, so it is no longer connected to your master prefab. The master prefab will still exist to be used in the future/will not remove your other prefab instances. This made it a useful way to make similar prefabs, by unpacking your similar prefab and editing it and making that into a new prefab (this may be somewhat outdated with Unity’s new prefab variants).

Unite Europe 2017 – Multi-scene editing in Unity for FAR: Lone Sails

March 15, 2019

Unite Europe 2017

Multi-scene editing in Unity for FAR: Lone Sails

Youtube – Unite Europe 2017 – Multi-scene editing in Unity for FAR: Lone Sails

By: Goran Saric

I wanted to look into pushing the tower defense tutorial series I did from Brackeys into a more polished little game project, so one of the first upgrades I wanted to look into for the project was the suggestion about breaking up the scenes in a more efficient manner.

This was mentioned as the best way to set up the overall project if you wanted to make it significantly larger but keep it very easy to build upon. This is because there are a lot of objects in a given level currently that will persist between every level and right now they are just copied into every single level. If you make an edit to any of these objects, they need copied into every other scene again. This can be minimized by using prefabs much more extensively, but having a scene solely for these objects keeps everything much cleaner and easier to edit.

So searching for how to properly implement this idea of broken up scenes, I came across this Unite Europe 2017 talk where the makes of FAR: Lone Sails detail exactly how they approached the used of multi-scene editing in their game.

Before getting into their general additive scene setup, they mention how they broke down the main level content from a giant full story board type setup into equally sized level scenes. They then had two level scenes loaded at any given time to keep the memory usage down to a minimum, but ensure the next scene was ready when the player arrived.

The Scene Abstraction:

  • Logic Scene
    • Always loaded
    • Contains all relevant managers to keep game running
    • Examples: Scene Manager; Save Manager
  • Base Scene
    • All elements of game that are always present during gameplay and level independent
    • Examples: character(player); camera; their giant vehicle
  • Level Content 1 Scene
    • The rest of the level content that is unique for that area/level
  • Level Content 2 Scene
    • This is the same type as the previous scene
    • Just enforcing that the game has 2 level scenes loaded at any one time

They then detail some of their work flow with these level content scenes in the editor. Even though the game only ever has two scenes loaded at once, sometimes they had several level scenes all open at once to ensure the overall theme was consistent, for aligning geometry, etc. It is also noted the Editor play time gets faster by only having the scenes loaded that you need when testing. More broken up scenes also helps reduce merge conflicts.

Helper Tools

There were two main tools they mentioned being helpful for the designers to keep them organized: Highlight Scene Borders and Teleportation Keyboard Shortcuts.

Highlight Scene Borders: This tool just had a large colored plain at the ends of the scenes to help indicate where new scenes were starting/ending when having multiple open at once. This was especially helpful since they are dealing with more of a 2D platforming game world. This just helps ensure objects are placed in the correct scene, as well as helping determine camera frustrum angles.

Teleportation Keyboard Shortcuts: They had an issue where constantly during testing they would have to slide some of the major game components through the scenes to locate them. They discovered a much easier solution to this was to just have a keyboard shortcut that teleported these constantly moving pieces to the current mouse position. If done during the running of the game, this also has an added benefit that it doesn’t mess with the editor at all and will be reset to the proper location after testing.

Scene Collection

Unity doesn’t have a built in way to save scene hierarchies in the editor yet, but there are many tutorials online about creating your own editor tools to do this. Unity offers the corresponding API to do so, it just needs some work to be user friendly. They created a scriptable object that can save the constellation of all loaded scenes to be loaded again at a later time.

Cross-Scene References

Unity does not normally allow for cross scene references between objects within the Inspector. There are several ways to access an object from another scene though.

GameObject.Find: this is very slow and can be tricky to find the correct instance

Singleton/Statics: most programmers dislike using these, but they can be used for this purpose

Scriptable Objects: keep references of instances in a scriptable object and link them to your scripts

Zenject – Dependency Injection Framework: offers some built-in features to support reference injections over multiple scenes

For FAR, they didn’t need to do very many cross scene references in the way they setup the scenes. When they did need to do this however, they had a simple monobehavior component in the Base Scene which would access a reference in a static reference pool. This static field reference could then be influenced/updated by simple monobehavior components in the current Level Scene. This exposes some of the methods from that original component in the Base Scene. Their setup also helps keep the Level Designers from messing with scripts they shouldn’t be modifying.

Finally, if you want some really advanced features already setup for you, there is a tool you can buy on the Unity store. It’s titled “Advanced Multi-Scene: Cross-Scene References”. It has some nice features such as scene merging and cross scene Inspector referencing.

Unity Player Pref Editor

March 8, 2019

Unity Player Prefs Editor Script

Editor Script

Unity – Player Prefs Editor in Unity

By: Romejanic

As I was finishing my Unity tower defense tutorial, I wanted to look into deleting player pref values to do bug testing for the level unlocking system. When I searched how to do this, I came across this very helpful Unity editor script that gives a few very basic functionalities dealing with player prefs in Unity.

This tool lets you set or get the values assigned to player pref variables you’ve created. This can be useful to double check exactly what a player pref is set at currently. It also has an option to delete the value assigned to a specific player pref, or just delete all player pref values. This gave me the exact option I needed, as well as giving a nice option for checking how the game works for a new player just starting the game.