Tip of the day

November 2015

 *  Tip #88 – Show In Explorer 

If you need your editor scripts to have the same functionality as “Assets->Show In Explorer” check out the cross platform method EditorUtility.RevealInFinder. This method is currently undocumented in Unity 5.2.1.


 *  Tip #87 – Proper handling of EditorWindow close and recompile events 

When writing editor scripts involving EditorWindow’s it is often important to initialize and cleanup your code when the window is shown and hidden. Specifically it is important to differentiate between the OnDisable and OnDestroy methods if you need to perform some kind of cleanup before the window is closed or disposed of. OnDestroy is called when the user closes the window, where as OnDisable is called after unity recompiles scripts. Think of the OnDestroy method as a close event for the window but the window still resides in memory, where the OnDisable method signals that the window is about to be unloaded from memory such as during a script recompile.

This differentiation is important when you need to save data to disk before the window is destroyed during a recompile. OnDestroy will not get called during recompile only OnDisable does. The OnEnable method is typically intended as a initialization method where you can load data related to the window.

I only wish the Unity team had made these methods more descriptive ala .NET window forms naming scheme. OnDestroy & OnDisable are not the most descriptive for what they do as well as being somewhat similar in spelling. But I digress.

October 2015

 *  Tip #86 – Environment.GetFolderPath for mac and windows 

When compiling your Unity games & applications to different platforms, there may be times when you need to get the location of special folder locations on the system that your app is running on.

The Environment.GetFolderPath method can be use to retrieve those specific folder locations. Below is a list of folder locations for both Windows 7 and Mac OS X. The source of this information is available here along with code examples.

July 2015

 *  Tip #85 – Disallow Multiple Components Attribute 

If you are writing custom scripts there may be situations where you don’t want the same component added more then once to a game object. For such situations Unity provides the DisallowMultipleComponent attribute that you can specify in your scripts code.

May 2015

 *  Tip #84 – Don’t just show errors alert them 

It’s not something people think about these days. Back in the 90’s when MS-DOS was king, alerting the user to a problem involved both a visual alert as well as a auditory alert in the form of a “beep”. It’s actually something I miss from those days. Luckily the unity editor has an API to alert the user and wouldn't you know it, it’s called “Beep”!

April 2015

 *  Tip #83 – Detect when unity compiles scripts *Redux* 

If you are developing unity editor extensions often it is necessary to know weather or not unity is compiling the projects scripts. To determine if unity is currently compiling you can simply check the EditorApplication.isCompiling flag in your code before deciding what to do. See older related tip #64 from April 2013.


 *  Tip #82 – Enhancing the Project and Hierarchy windows 



Unity provides hooks into the Project and Hierarchy windows OnGui event. This allows you to add additional flair to these windows. To learn how to enhance these windows see the fallowing documentation pages


 * HierarchyWindowChanged
 * HierarchyWindowItemOnGUI
 * ProjectWindowItemOnGUI
 * ProjectWindowChanged

If you want an example project to work off of and learn from there is an open source project called “UnityEditorEnhancements” that you can download as a zip file or you can fork the Mercurial repository.


 *  Tip #81 – Per-Camera Fog 

The fallowing script allows you to control fog settings on a per camera basis, allowing you to use say green fog for one camera but red fog for another camera.

Unity 5 package demo is available here CameraFog.unitypackage (46.66 kb) See related page CameraFog



CameraFog.cs script code


 *  Tip #80 – Unity5 Material Reference Charts 

The unity 5 documentation has a couple of handy reference charts/Info Graphics for when you need to help determining what material values to set to get a particular look and feel.

http://docs.unity3d.com/Manual/StandardShaderMaterialCharts.html



March 2015
With the introduction to Unity 5 there comes some api changes. Specifically this foot note was interesting “[2] in Unity5 we also cache the transform component on the c# side, so there should no longer be a performance reason to cache the transform component yourself.”
 *  Tip #79 – Unity 5 Transform performance 

I decided to test it out by writing a few performance test scripts and comparing performance numbers. Below is a screen shot of my results along with the scripts used.

As you can see caching a reference to the transform component in the Start method then using that reference is still faster then calling “this.transform” directly albeit only slightly by about 10-20 ticks. And calling “this.GetComponent” is almost twice as slow.



The code for the PerformanceTesting class is available here.

TransformCachedGetComponentPerformance script

TransformGetComponentPerformance script

TransformFieldPerformance script

Archived Years

 * Tip of the day entries for 2014 Contains tips 73 to 79
 * Tip of the day entries for 2013 Contains tips 48 to 72
 * Tip of the day entries for 2012 Contains tips 1 to 47

Older Tips
Note: Place your new tip at the top of the list, not at the bottom.
 * When move a GameObject, hold the Key V,will fast align.
 * A skinned character updates its vertices every frame, even when not animating. To freeze a skinned character in place without making it disappear or revert to a T-pose, set its SkinnedMeshRenderer.bones field to null.
 * There are two really nice hotkeys to insert or delete array elements in the inspector. SHIFT + DEL will delete the selected element and CTRL + D will duplicate it. SHIFT is only needed for reference types like GameObject[].
 * When you're using Debug.Log or related methods, provide the instance of the script so you can click the log output and have the relevant object selected in editor! Debug.Log("Test", this);
 * If you're placing objects in the screen and want them to "snap" into place (great for placing items on the ground) you can hold the Command key (Control on Windows) while dragging the object's axis (snap increments are defined in Edit->Snap Settings)
 * Importing classes in JavaScript. Tired of writing Debug.Log and SendMessageOptions.DontRequireReceiver? Did you know that static members of classess and enums can be imported just like importing namespaces. Simply add import UnityEngine.Debug; and import UnityEngine.SendMessageOptions and then you can write Log("my log string") and SendMessage("ApplyDamage",2.0,DontRequireReceiver);.
 * Deleting objects. Use Command + Backspace in the Scene, Hierarchy and Project views to remove objects. In the Scene and Hierarchy views this will just remove the selected instance of the object. In the Project view this will remove the selected object file and put it in your system Trash.
 * If you need some textures, like font textures, to not be affected by Texture.masterTextureLimit, disable Generate Mip Maps in the texture's import settings. On the down side, this can adversely affect clarity of rendering at smaller scales.
 * Boo has some handy built-in functions like shell, join and reversed. And the cool part? They are available from JavaScript as well.
 * Every built-in component in the inspector has a small question mark in the right of it's heading. That's the quickest way to open the context-help for that component. It's your friend.
 * To remember which color means what on an axis in Unity, just remember the mnemonic: RGB = XYZ.
 * You can use keyboard shortcuts for Edit->Load Selection and Edit->Save Selection to speed up the selection of commonly used parts in your scene.
 * If you option-click an object in your hierarchy, the entire hierarchy within it will expand and unexpand.
 * You can write Editor scripts to greatly reduce project-specific tedium when making something with Unity. These are just as easy to write as regular scripts, and can save you a lot of hassle. See the Wizard Archive for some examples.
 * If you have the problem when importing models that Vector3.up points in the wrong direction (or for any of the other directions), make the game model in question the child of a blank game object, and orient it correctly in local space.
 * When assigning properties of components in the Inspector, you can click on the triangle to pop up a menu of choices. Type in the name of the thing you want and press enter to assign it.  This can be a speedy alternative to using drag-and-drop.
 * When entering text in the Inspector view, you can press option-return to get a new line. This is useful for writing multiline things with GUIText objects.
 * If you only have one editor view and no game views in the Unity pane set up, the editor view will be automatically replaced with a game view when you enter play mode and back again when you press stop. This is a nice feature when you are working on a small monitor.
 * Use Tags to assign your own categories and attributes to game objects. Then scripts can refer to the tags. Example: if (contact.otherCollider.tag == "Enemy") or GameObject.FindWithTag ("Enemy")
 * If you want to move a file inside of a project use Unity's project view to move it and not the Finder. This way all references and import settings of the file will be maintained.