5 things I wish I’d known about Unity 3 months ago

I just spent 3 months on a contract to develop a prototype for an app in Unity, these are five things I learned during development that you might find useful:

Proper commenting:

Just a simple one to start, using triple slashes above a variable declaration:

///A descriptive description
public GameObject myGameObject;

Now when we type the name of the variable the tool-tip box will have our nice description, handy!

Declutter your inspector window:

If you want public variable, but don’t want it to show in your editor:

[HideInInspector]

For some that will be obvious, for others I might have just changed your life.

Reclutter your inspector window:

Now we freed up some space in the inspector, lets fill it with something useful:

https://unity3d.com/learn/tutorials/topics/interface-essentials/adding-buttons-custom-inspector

This tutorial will get you started on making some custom editor buttons. The time you can save here will make you far more productive, for example a quick button to bypass your menus, or something like this:

if(GUILayout.Button("Print useful data"))
{
    Debug.Log(usefulData);
}

Just as a forewarning these buttons will work when you are still in the editor, so use EditorApplication.isPlaying to check if you’re playing if you need to.

C sharp is kinda nifty:

C sharp has some pretty nice built in functionality we can exploit for example:

MailMessage mail = new MailMessage();
[...]
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
		smtpServer.Port = 587;
		smtpServer.Credentials = new System.Net.NetworkCredential("myGmailEmail@gmail.com", "myPassword") as ICredentialsByHost;
		smtpServer.EnableSsl = true;
		ServicePointManager.ServerCertificateValidationCallback = 
			delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
		{ return true; };
		smtpServer.Send(mail);

and voila you’re sending emails from you game!

There is also some extension from Unity too, e.g.

[System.Serializable]

This lets your serialize Unity Lists, Vector3, Quats, lists of Vectors and Quats, etc.

DO NOT REINVENT THE WHEEL

Building stuff with Unity is great because there is so much in there already done, and if it isn’t included you can probably find the answer online. A few quick things I figured wouldn’t be there that are just hidden in the documentation:

  1. JSON read/write functionality, you can serialize a whole class to a cross platform and readable format,but wait there’s more.
  2. WWW, the WWW class is a treasure trove of functionality.
  3. DateTime is a datatype that comes from csharp but is certainly handy.

There is a lot of stuff that you might want to write your own though, for example a percentage based approximation function is pretty handy. Chances are there is one in there and I just haven’t found it.

 

Unity has a lot of gems in the engine that’ll make your life easier, unfortunately you might have to dig a bit to find them.