5 More Unity Tips

This is a continuation of my previous blog post, where I am reading through the online Unity manual and attempting to extract wisdom that others may not have heard yet! That said, here are 5 more useful pieces of info I've uncovered.


Clicks On Mobile

Input.GetMouseButton(0) works on mobile devices! A single touch on the screen registers as a left click, so GetMouseButton(), GetMouseButtonDown(), and GetMouseButtonUp() can all be used to catch it. Input.mousePosition can then be used to get the position of the touch. This allows porting of simple UIs from PC to mobile to be done with almost no extra work at all.


Editor-Only Objects

A game object in a scene can be tagged "EditorOnly" to prevent it from being included in a published build. To do so, simply select the object in the Hierarchy view, then select "EditorOnly" from the dropdown.

This can be useful for objects that help to debug during development, which you usually don't want sneaking their way into production.


Special Folders

There are several folder names that Unity has specific uses for. Unless you are using these folders for their specific purpose, you should avoid using their names. Their names are:

  • Editor
  • Editor Default Resources
  • Gizmos
  • Plugins
  • Resources
  • Standard Assets
  • WebPlayerTemplates

I was once working on a strategy game that had different types of resources in it, and I tried to keep their scripts, models, etc. in a "Resources" folder. You can imagine my confusion when things weren't working as I thought they would!


Asset Serialization Mode

By default, Unity stores many assets in binary format for efficiency reasons. This can be switched to Text format in the Editor settings (Edit -> Project Settings -> Editor). This can be extremely useful for merging changes of scenes, prefabs, etc.

We are using Git for our current project, and I really wish I had known this sooner! There have already been multiple times where I had to just overwrite changes due to merge issues.


Avoid DontDestroyOnLoad

According to Unity's documentation, DontDestroyOnLoad() should be avoided for manager/gamestate objects. This must be a fairly new recommendation, because they recommend using a separate scene to store all of your "manager" scripts. With this method, you will have a single scene that is always around, and load/unload individual scenes using SceneManager.LoadScene() and SceneManager.UnloadScene(). When you load the scene, be sure to set LoadSceneMode to additive. Failure to do so will result in losing your manager objects!

I can't speak much more to this style of using manager objects, because I still use DontDestroyOnLoad in my project. I will be trying out the scene idea in the future though. Who am I to disobey the creators?


That's all 5 tips for now! I hope you can find some of them useful, as I've already begun to do. Hopefully next month I will have 5 even better tips for you! Until then, it's back to the Unity mines for me.