Things you can do better in Unity!
This is the second part of our tips list, that can help you increase your productivity with Unity.
The first part can be found here. So definitely check it out if you haven’t already!
1. Unity project setup wizard
When you’re creating a new project it often takes at least half an hour to setup everything. There are at least 2 ways for you to save time here:
- Simple: create a project template with basic setup, folder structure and version control ignore file. This will be the most efficient one to make and you can put stuff in it already. But it is not the most customizable one, so probably you will need to spend this additional time on finishing your configuration.
- Advanced: create a module that will be responsible for setting up your whole project in a way you want it. With as many game/folder structure/architecture variants as you’d like to have. And in a perfect world, it would be great to have such module but it takes time to make it.
No matter which option you choose, it is always helpful to have a solid foundation for you project 😉
2. Finally you drag the list of objects into the component
Sometimes you may come across a case where you have to fill the list of objects with objects from a scene or a project. When you’ll first try to select all of the objects you’d like to assign, you’ll realise that Inspector is no longer showing you object with your list to fill. There is a very simple solution to that, and not many people know about it.
In a top right corner of Inspector there’s a lock button, that locks Inspector focus on current view.
Now you can select as many objects as you like and you can easily drag and drop them all into the list in Inspector.
Done!
3. Use build-in json tools
Normally, when you are working with JSONs, you have to have JSON parser. After parsing a string into JSON object, you have to assign all the values from JSON to the desired object, which is super boring to write each time…
However, since the Unity 5.3 they gave us built-in JsonUtility which will do most of it for you!
using System.Collections; using System.Collections.Generic; using UnityEngine; public class JsonSerializationExample : MonoBehaviour { void Start () { // Creating object that will be serialized ResearchObjectRoot array = new ResearchObjectRoot(); array.researches = new List<ResearchData>() { new ResearchData() { researchName = "JSON", researchValue = 0}, new ResearchData() { researchName = "Utility", researchValue = 4}, new ResearchData() { researchName = "Serialization", researchValue = 8}, }; // Serialization string json = JsonUtility.ToJson(array); // Object created from serialized json ResearchObjectRoot newArray = JsonUtility.FromJson<ResearchObjectRoot>(json); } }
[System.Serializable] public class ResearchObjectRoot { public string researcherName; public List<ResearchData> researches; }
[System.Serializable] public class ResearchData { public string researchName; public int researchValue; }
Warning: JsonUtility currently works only with ‘structured’ JSON and does not support types, like dictionaries. If this is a problem for you, then you probably need to find a fully-featured JSON library.
For more information about JsonUtility you can go to: https://docs.unity3d.com/Manual/JSONSerialization.html
4. One script that handles all of the platforms?
If you are new to programming, you probably don’t know much about platform dependent compilation. If you are into programming, then you have probably seen a code that uses platform dependent compilation a few times, but you might not understand how it works. It’s simple! It gives you the power to write the code that will be executed only on a specific platform. So you might ask why it’s good to use it? Because you can use libraries designed only for one platform.
For example: you are making the greatest game of all time for Android and iOS. And you want to implement that last feature, namely leaderboards. You’ve created a button to show these native leaderboards and assign method OpenMainLeaderboard() to it. And now it’s time to implement this method, but how will you run Google Play Services on Android and Game Center on iOS?! That’s simple! Use platform dependent compilation!
Example:
using UnityEngine; public class OpeningLeaderboards : MonoBehaviour { public void OpenMainLeaderboard() { #if UNITY_ANDROID // open google play games leaderboard #elif UNITY_IOS // open game center leaderboard #else Debug.Log("Opening main leaderboard view!"); #endif } }
With that you don’t have to worry about the missing references from libraries that are not designed to run on other platforms.
If you’d like to find more platform directives go to: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
5. Don’t waste your time for reimporting project over and over again
Aren’t you tired of waiting for your project to be reimported each time you change build platform? For me it’s not only boring, but also wasting a lot of time! But there is a mighty solution for that, which no one ever told you about!
In Unity there is something called Cache Server. It can be configured to work on your local machine or on a dedicated server. This cache server will save your reimported assets and when you change your build platform, it will give you back your reimported assets, so you don’t have to reimport them again! This is great! And the only thing you have to do is to go to Unity > Preferences > Cache Server and change Cache Server Mode to Local or Remote.
You can read more about Cache Server on our blog:
http://blog.theknightsofunity.com/using-unity-cache-server/
http://blog.theknightsofunity.com/unity-cache-server-localhost-make-sense/
Summary
That was top 10 things that you can do better in Unity (with those in the previous part)!
I hope that this list will help you save a little more time that you will still waste by watching another cat video on the internet 🙂
If you have your own trick to speed up your work, then don’t hesitate to share it in a comment section below.
And don’t forget! Optimize yourself!
You can also subscribe to our newsletter, so you won’t miss our posts in the future!