Tip of the day

From Unify Community Wiki
Revision as of 21:40, 4 April 2014 by Create3dgames (Talk | contribs)

Jump to: navigation, search

Contents

May 2013

  • Tip #67 – Get and set build order of scenes

You can get access to the build order for scenes in your editor scripts by calling EditorBuildSettings.scenes. This allows you to control what scenes are included with the build through your editor code.

 
public class SceneDump
{
    [MenuItem("Codefarts/Dump Scenes")]
    public static void Dump()
    {
        var parts = from part in EditorBuildSettings.scenes
                    select string.Format("{0} - {1}", part.enabled, part.path);
 
        Debug.Log(string.Join("\r\n", parts.ToArray()));
    }
}

BuildScenes.png

  • Tip #66 - QueueGameViewInputEvent

Need to send simulated user input to the game window? Unity provides the EditorGUIUtility.QueueGameViewInputEvent method for doing just that. Using this method you could construct a utility that captures and plays back user input as your game is running which would be handy for debugging and unit testing purposes.

  • Tip #65 – Get asset path of UnityEngine.Object

Below is a simple bit of code that will attempt to retrieve the asset path of the prefab stored on disk. The method will also try to walk up the prefab hierarchy if the prefab parameter appears to be a instance of the original.

 
/// <summary>
/// Gets the source path to a prefab if any.
/// </summary>
/// <param name="prefab">The prefab reference to get the asset path for.</param>
/// <returns>Returns a asset path for a prefab.</returns>
/// <remarks>This method will attempt to find the source asset of the given <see cref="UnityEngine.Object"/> by
/// walking up the parent prefab hierarchy.</remarks>
public static string GetSourcePrefab(UnityEngine.Object prefab)
{
    // if no prefab specified then return null
    if (prefab == null)
    {
        return null;
    }
 
    // attempt to get the path
    var path = AssetDatabase.GetAssetPath(prefab);
 
    // if no path returned it may be an instantiated prefab so try to get the parent prefab
    while (String.IsNullOrEmpty(path))
    {
        // try parent prefab
        var parent = PrefabUtility.GetPrefabParent(prefab);
 
        // no parent so must be generated through code so just exit loop
        if (parent == null)
        {
            break;
        }
 
        // attempt to get path for
        path = AssetDatabase.GetAssetPath(parent);
 
        // set prefab reference to parent for next loop
        prefab = parent;
    }
 
    // return the path if any
    return path;
}

April 2013

  • Tip #64 – Detect when unity compiles scripts

If you have some editor scripts and you want to detect weather or not unity has compiled your scripts you can try this simple method.

Created a private class within your editor window and declare a field of that private class.

 
public class YourEditor : EditorWindow
{
    private RecompileClass recompile;
 
    private class RecompileClass
    { 
    }
}

In the OnGUI method (or where appropriate) perform a check to see if the field is null. If it’s null you can assume that unity compiled your scripts again so do what you need to do is create a new instance of the private class and assign it to the field.

 
public class YourEditor : EditorWindow
{
    private RecompileClass recompile;
 
    private class RecompileClass
    {
    }
 
    public void OnGUI()
    {
        // check if recompile variable is null
        if (this.recompile == null  )
        {
            // if yes assume recompile then create a reference to a recompile class
            this.recompile = new RecompileClass();
 
            // do what you need to do here after detecting a recompile 
        }
 
        // do regular stuff here
    }
}

The next time unity re-compiles your scripts the field will loose it’s reference and will be null again.

  • Tip #63 – Right click “Open with Unity”

Not a unity specific tip but still a handy unity helper for opening a unity project from file explorer. *Works only for windows platforms.* Download the OpenInUnity.reg file below or open up Notepad and paste the snippet below then save the file with a *.reg file extension. Next navigate to the file and right click on it and select “Merge” from the popup menu. You should now be able to open a unity project folder by right clicking on it in File Explorer and selecting “Open with Unity”.

File:OpenInUnity.zip (Contains OpenInUnity.reg file)

OpenWithUnity.png

 
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\Open with Unity\command]
@="\"C:\\Program Files (x86)\\Unity\\Editor\\unity.exe\" -projectPath \"%1\""
  • Tip #62 - Generate Unique Asset Path

Unity comes with a helper method for generating a unique asset paths called AssetDatabase.GenerateUniqueAssetPath. The documentation for this method is rather minimal but the code example below should help you understand how to use it a little better.

UniqueAssetPath.png

 
[MenuItem("CBX/Test")]
public static void Test()
{
    // outputs "Assets/Rules.xml" if "Assets/Rules.xml" file does not already exist.
    // if "Assets/Rules.xml" already exists it outputs "Assets/Rules 1.xml" 
    Debug.Log(AssetDatabase.GenerateUniqueAssetPath("Assets/Rules.xml"));
 
    // expects path to start with "Assets/" & outputs a console error in the console "pathName.find("assets/") == 0" 
    // then outputs "Rules.xml"
    Debug.Log(AssetDatabase.GenerateUniqueAssetPath("Rules.xml"));
}
  • Tip #61 – DrawGenericGrid & DrawCheckBoxGrid

Below is a code sample containing drawing methods for drawing check boxes or any control type in a grid based layout similar to the SelectionGrid.

 
using System;
 
using UnityEngine;
 
public class ControlGrid
{
    /// <summary>
    /// Draw a grid of check boxes similar to SelectionGrid.
    /// </summary>
    /// <param name="checkedValues">Specifies the checked values of the check boxes.</param>
    /// <param name="text">The content for each individual check box.</param>
    /// <param name="columns">The number of columns in the grid.</param>
    /// <param name="style">The style to be applied to each check box.</param>
    /// <param name="options">Specifies layout options to be applied to each check box.</param>
    /// <returns>Returns the checked state for each check box.</returns>
    /// <remarks><p>Check boxes are drawn top to bottom, left to right.</p>  </remarks>
    /// <exception cref="IndexOutOfRangeException">Can occur if the size of the array is too small.</exception>
    public static bool[] DrawCheckBoxGrid(bool[] checkedValues, string[] text, int columns, GUIStyle style, params GUILayoutOption[] options)
    {
        // convert string content into gui content
        var content = new GUIContent[text.Length];
        for (var i = 0; i < content.Length; i++)
        {
            content[i] = new GUIContent(text[i]);
        }
        return DrawCheckBoxGrid(checkedValues, content, columns, style, options);
    }
 
    /// <summary>
    /// Draw a grid of check boxes similar to SelectionGrid.
    /// </summary>
    /// <param name="checkedValues">Specifies the checked values of the check boxes.</param>
    /// <param name="textures">The content for each individual check box.</param>
    /// <param name="columns">The number of columns in the grid.</param>
    /// <param name="style">The style to be applied to each check box.</param>
    /// <param name="options">Specifies layout options to be applied to each check box.</param>
    /// <returns>Returns the checked state for each check box.</returns>
    /// <remarks><p>Check boxes are drawn top to bottom, left to right.</p>  </remarks>
    /// <exception cref="IndexOutOfRangeException">Can occur if the size of the array is too small.</exception>
    public static bool[] DrawCheckBoxGrid(bool[] checkedValues, Texture2D[] textures, int columns, GUIStyle style, params GUILayoutOption[] options)
    {
        // convert texture content into gui content
        var content = new GUIContent[textures.Length];
        for (var i = 0; i < content.Length; i++)
        {
            content[i] = new GUIContent(string.Empty, textures[i]);
        }
        return DrawCheckBoxGrid(checkedValues, content, columns, style, options);
    }
 
    /// <summary>
    /// Draw a grid of check boxes similar to SelectionGrid.
    /// </summary>
    /// <param name="checkedValues">Specifies the checked values of the check boxes.</param>
    /// <param name="content">The content for each individual check box.</param>
    /// <param name="columns">The number of columns in the grid.</param>
    /// <param name="style">The style to be applied to each check box.</param>
    /// <param name="options">Specifies layout options to be applied to each check box.</param>
    /// <returns>Returns the checked state for each check box.</returns>
    /// <remarks><p>Check boxes are drawn top to bottom, left to right.</p>  </remarks>
    /// <exception cref="IndexOutOfRangeException">Can occur if the size of the array is too small.</exception>
    public static bool[] DrawCheckBoxGrid(bool[] checkedValues, GUIContent[] content, int columns, GUIStyle style, params GUILayoutOption[] options)
    {
        return DrawGenericGrid((e, i, s, o) => GUILayout.Toggle(e[i], content[i], style, options), checkedValues, content, columns, style, options);
    }
 
    /// <summary>
    /// Draw a grid of controls using a draw callback similar to SelectionGrid.
    /// </summary>
    /// <param name="drawCallback">Specifies a draw callback that is responsible for performing the actual drawing.</param>
    /// <param name="values">Specifies the values of the controls.</param>
    /// <param name="content">The content for each individual control.</param>
    /// <param name="columns">The number of columns in the grid.</param>
    /// <param name="style">The style to be applied to each control.</param>
    /// <param name="options">Specifies layout options to be applied to each control.</param>
    /// <returns>Returns the value for each control.</returns>
    /// <remarks><p>Controls are drawn top to bottom, left to right.</p>  </remarks>
    /// <exception cref="IndexOutOfRangeException">Can occur if the size of the array is too small.</exception>
    /// <exception cref="ArgumentNullException">If the drawCallback is null.</exception>
    public static T[] DrawGenericGrid<T>(Func<T[], int, GUIStyle, GUILayoutOption[], T> drawCallback, T[] values, GUIContent[] content, int columns, GUIStyle style, params GUILayoutOption[] options)
    {
        if (drawCallback == null)
        {
            throw new ArgumentNullException("drawCallback");
        }
 
        GUILayout.BeginVertical();
        var rowIndex = 0;
        var columnIndex = 0;
        var index = rowIndex * columns + columnIndex;
 
        GUILayout.BeginHorizontal();
        while (index < values.Length)
        {
            // draw control
            values[index] = drawCallback(values, index, style, options);
 
            // move to next column
            columnIndex++;
 
            // if passed max columns move down to next row and set to first column
            if (columnIndex > columns - 1)
            {
                columnIndex = 0;
                rowIndex++;
 
                // remember to start a new horizontal layout
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
            }
 
            // re-calculate the index
            index = rowIndex * columns + columnIndex;
        }
        GUILayout.EndHorizontal();
 
        GUILayout.EndVertical();
        return values;
    }
}

Here is a simple example of how to use the draw methods

var emptyContent = new[] { GUIContent.none, GUIContent.none, GUIContent.none,
                            GUIContent.none, GUIContent.none, GUIContent.none,
                            GUIContent.none, GUIContent.none, GUIContent.none };
 
GUILayout.BeginVertical(GUILayout.MinWidth(128));
rule.IgnoreUpper = GUILayout.Toggle(rule.IgnoreUpper, "Upper Neighbors");
rule.NeighborsUpper = ControlGrid.DrawCheckBoxGrid(rule.NeighborsUpper, emptyContent, 3, GUI.skin.button, GUILayout.MaxWidth(32), GUILayout.MaxHeight(32));
GUILayout.EndVertical();

Selectiongrid.png

  • Tip #60 – Set mouse cursor for unity UI control

You can set the mouse cursor for a UI control by using EditorGUIUtility.AddCursorRect and specifying a MouseCursor enum.

if (GUILayout.Button("Add"))
{
 
}
 
// show the "Link" cursor when the mouse is hovering over this rectangle.
EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);

March 2013

  • Tip #59 – PingObject

Ever wonder how unity makes assets in the project window pop out when you select a link to them in the inspector? See the EditorGUIUtility.PingObject method.

PingObject.png

[MenuItem("Examples/Ping Selected")]
static void Ping()
{
    if (!Selection.activeObject)
    {
        Debug.LogError("Select an object to ping");
        return;
    }
    EditorGUIUtility.PingObject(Selection.activeObject);
}
  • Tip #58 – LookLikeControls & LookLikeInspector

If you are writing UI code and you want the controls you are drawing to appear as if they are labels until they are in focus you can use the EditorGUIUtility.LookLikeInspector & EditorGUIUtility.LookLikeControls methods.

LookLikeControls.png

public class LookLikeControlsInspector : EditorWindow
{
    private int integer1;
    float float1 = 5.5f;
 
    [MenuItem("Examples/Look Like Controls - Inspector")]
    static void Init()
    {
        var window = GetWindow<LookLikeControlsInspector>();
        window.Show();
    }
 
    void OnGUI()
    {
        EditorGUIUtility.LookLikeInspector();
        EditorGUILayout.TextField("Text Field:", "Hello There");
        EditorGUILayout.IntField("Int Field:", integer1);
        EditorGUILayout.FloatField("Float Field:", float1);
        EditorGUILayout.Space();
        EditorGUIUtility.LookLikeControls();
        EditorGUILayout.TextField("Text Field", "Hello There");
        EditorGUILayout.IntField("Int Field:", integer1);
        EditorGUILayout.FloatField("Float Field:", float1);
    }
}
  • Tip #57 – Resource.Load/LoadAll Gotchas

I recently came across a strange behavior while loading text resource assets. In particular the Resources.LoadAll method does not accept Path.DirectorySeparatorChar characters in a path. In fact it only accepts Path.AltDirectorySeparatorChar characters. This behavior is different then standard .net file/folder methods that accept either Path.AltDirectorySeparatorChar or Path.DirectorySeparatorChar without distinction. What this means is that you can’t directly use Path.Combine to build a path and pass it to the Resources.LoadAll method you first have to replace any Path.DirectorySeparatorChar characters with Path.AltDirectorySeparatorChar characters.

The documentation for Resources.Load also does not mention this behavior.

I have submitted a bug report here.

 using System.IO;
 
using UnityEditor;
 
using UnityEngine;
 
/// <summary>
/// Handles settings registration.
/// </summary>
[InitializeOnLoad]
public class EditorInitialization
{
    /// <summary>
    /// Holds a value indicating whether the RunCallbacks method has been called at least once before.
    /// </summary>
    private static bool ranOnce;
 
    /// <summary>
    /// Initializes static members of the <see cref="EditorInitialization"/> class.
    /// </summary>
    static EditorInitialization()
    {
        EditorApplication.update += RunCallbacks;
    }
 
    private static void RunCallbacks()
    {
        if (!ranOnce)
        {
            // try to load resource
            var path = Path.Combine("Test/SubFolder", "testfile"); // result is Test/SubFolder\testfile
 
            // var data = Resources.LoadAll("Test/SubFolder/testfile", typeof(TextAsset)); // this line is successful
            var data = Resources.LoadAll(path, typeof(TextAsset));  // this line fails
 
            if (data != null && data.Length != 0)
            {
                Debug.Log("found it");
            }
            else
            {
                Debug.Log("not found! " + path);
            }
 
            ranOnce = true;
            return;
        }
 
        // do stuff
    }
}

Febuary 2013

  • Tip #56 – Get project folder & current directory

There is no Unity specific API for getting the project folder, but you can use System.IO.Directory.GetCurrentDirectory. The unity editor expects the current folder to be set to the project folder at all times.

If you are changing the current directory in your editor scripts using System.IO.Directory.SetCurrentDirectory you need to restore the directory back to the project folder when you are done. Otherwise the next time unity compiles your scripts it will prompt you with a dialog box and a message stating you need to restore the current directory back to the project directory and only gives you a “Quit” button to click on that will quit unity.

// save project folder
var projectFolder = System.IO.Directory.GetCurrentDirectory();
 
// set current directory to a folder of your choosing
System.IO.Directory.SetCurrentDirectory("c:\\some folder name");
 
// do what you need to do
// ...
 
// restore current folder back the the project folder
System.IO.Directory.SetCurrentDirectory(projectFolder);
  • Tip #55 – InitializeOnLoad & Resources.Load gotcha’s

I’ve recently come across what initially seems like a bug with the Unity 4 editor. it involves the use of the Resources.Load method from within a class marked InitializeOnLoad.

Take the fallowing example code …

namespace CBX.CoreProjectCode
{
    using UnityEditor;
 
    using UnityEngine;
 
    [InitializeOnLoad]
    public class EditorInitialization
    {
        static EditorInitialization()
        {
            LoadData(); // this will fail to see the test.txt file in the resource folder
        }     
 
        private static void LoadData()
        {
            // try to load data
            var data = Resources.Load("test", typeof(TextAsset)) as TextAsset;
            if (data == null)
            {
                Debug.LogWarning("No data found");
                return;
            }
 
            Debug.Log(data.text);
        }
    }
}

After Unity compiles the scripts it will invoke any static constructors of classes marked with InitializeOnLoad. But at this point I suspect that Unity has not yet identified assets you have within any Resources folders. When the class constructor calls the Resources.Load method it will fail to read the test.txt resource file and return a warning message out to the console.

What you could do at this point is right click the “Assets” folder in the project window and select “Reimport”. Unity will reimport the assets and again call classes marked with InitializeOnLoad. But this time Resources.load will successfully load the test.txt file resource and display it’s contents out to the Unity console window.

One work around to this is to hook into the EditorApplication.update callback and make a call to the LoadData method from there. An example is provided below …

namespace CBX.CoreProjectCode
{
    using UnityEditor;
 
    using UnityEngine;
 
    [InitializeOnLoad]
    public class EditorInitialization
    {
        private static bool ranOnce;
 
        static EditorInitialization()
        {
            EditorApplication.update += RunCallbacks;
        }
 
        private static void RunCallbacks()
        {
            if (!ranOnce)
            {
                // this will see the test.txt file in the Resource folder and display
                // its contents
                LoadData();
                ranOnce = true;
 
                // return or don't it’s up to you
                return;
            }
 
            // do some work here
        }
 
        private static void LoadData()
        {
            var data = Resources.Load("test", typeof(TextAsset)) as TextAsset;
            if (data == null)
            {
                Debug.LogWarning("No data found");
                return;
            }
 
            Debug.Log(data.text);
        }
    }
}

Again I am unsure whether this is an actual bug or not but I have submitted a Unity bug report anyway just in case. Here is a link to the my [bug report]

  • Tip #54 – ScaleTransform

The code below allows you to scale a GameObject transform to a specific size along the x & z axis by taking into account the GameObjects renderer bounds if a renderer component is attached.

/// <summary>
/// Scales a transform to specific dimensions along the x & z axis.
/// </summary>
/// <param name="transform">
/// Reference to the transform to scale.
/// </param>
/// <param name="width">The width along the x axis that represents the target size.</param>
/// <param name="height">The height along the z axis that represents the target size.</param>
public static void ScaleTransform(Transform transform, float width, float height)
{
    // get bounds of the prefab
    var bounds = new Bounds();
    var encapsulate = false;
    if (!Utilities.Helpers.GetBoundWithChildren(transform, ref bounds, ref encapsulate))
    {
        return;
    }
 
    // get minimum size from the size dimensions
    var min = Mathf.Min(width, height);
 
    // get the maximum x or z size of the transform
    var max = Mathf.Max(bounds.size.x, bounds.size.z);
 
    // calculate the scale factor 
    var scaleFactor = min / max;
 
    // apply scaling to the transform
    transform.localScale *= scaleFactor;
}
 
/// <summary>
/// Gets the rendering bounds of the transform.
/// </summary>
/// <param name="transform">The game object to get the bounding box for.</param>
/// <param name="pBound">The bounding box reference that will </param>
/// <param name="encapsulate">Used to determine if the first bounding box to be 
/// calculated should be encapsulated into the <see cref="pBound"/> argument.</param>
/// <returns>Returns true if at least one bounding box was calculated.</returns>
public static bool GetBoundWithChildren(Transform transform, ref Bounds pBound, ref bool encapsulate)
{
    var didOne = false;
 
    // get 'this' bound
    if (transform.gameObject.renderer != null)
    {
        var bound = transform.gameObject.renderer.bounds;
        if (encapsulate)
        {
            pBound.Encapsulate(bound.min);
            pBound.Encapsulate(bound.max);
        }
        else
        {
            pBound.min = bound.min;
            pBound.max = bound.max;
            encapsulate = true;
        }
 
        didOne = true;
    }
 
    // union with bound(s) of any/all children
    foreach (Transform child in transform)
    {
        if (GetBoundWithChildren(child, ref pBound, ref encapsulate))
        {
            didOne = true;
        }
    }
 
    return didOne;
}
  • Tip #53 – Undo.IncrementCurrentEventIndex

Using the Undo.IncrementCurrentEventIndex allows your editor scripts to undo a single action rather then have unity undo many actions that may have occurred in rapid succession. Place the code below into a “Editor” folder for it to run.

The “create object” button will create 10 cubes and call Undo.RegisterCreatedObjectUndo for each of them. Click this button then press ctrl+z to undo all of the objects that were created.

The second button “create single object” will also create 10 cubes but will also call the Undo.IncrementCurrentEventIndex method notifying unity that each object that is created should have a separate undo action. Click “create single object” to create 10 cubes then press ctrl+z to undo each object creation individually.

NOTE: At the time of this writing the unity documentation does not have information on the Undo.IncrementCurrentEventIndex method and searching for it also comes back with zero results.

    using UnityEngine;
    using UnityEditor;
 
    public class UndoTest : EditorWindow
    {
 
        public void OnGUI()
        {
            if (GUILayout.Button("create object"))
            {
                this.Create(false);
            }
            if (GUILayout.Button("create single object"))
            {
                this.Create(true);
            }
 
            GUILayout.Label("use ctrl+z to undo each action");
        }
 
        private void Create(bool single)
        {
            for (int i = 0; i < 10; i++)
            {
                if (single)
                {
                    Undo.IncrementCurrentEventIndex();
                }
 
                var obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                obj.transform.position = new Vector3(Random.Range(-10, 10), Random.Range(-10, 10), Random.Range(-10, 10));
 
                Undo.RegisterCreatedObjectUndo(obj, "create " + obj.name);
            }
        }
 
        [MenuItem("CBX/UndoTest")]
        public static void ShowWindow()
        {
            GetWindow<UndoTest>("UndoTest").Show();
        }
    }
  • Tip #52 – Providing proper links in version changes

If you are submitting packages to the asset store and are including version changes that contain html links, remember to specify target="_blank" in your link attributes. Failure to do so can result in the asset store window being replaced by the web page that your link was pointing to. In some occasions it can also cause unity to crash.

Unity101Tip52.png

  • Tip #51 – Texture Atlas support

I have been thinking that because texture atlases are so common Unity should have some build in support for generating them. Turns out it already does! Check out the Texture2D.PackTextures method.

  • Tip #50 – Ensure conditional compilation symbols are present

If your Unity code requires conditional compilation symbols to be present this bit of code may come in handy. After unity compiles your scripts it executes any classes that have the InitializeOnLoad attribute. You can call the SetupConditionalCompilation method provided in the code snippet below to ensure that the conditional compilation symbols persist in your unity project. If a symbol was not present and was added it will write out a notification in the unity console. ConditionalStartup.png

[InitializeOnLoad]
public class GridMappingSetup
{
    static GridMappingSetup()
    {
        var types = new[] { BuildTargetGroup.Standalone, BuildTargetGroup.WebPlayer };
        var toInclude = new[] { "CBXControls", "GridMapping", "QuickTools", "ToolService", "TileMaterialCreation" };
 
        SetupConditionalCompilation(types, toInclude);
    }
}
 
  public static void SetupConditionalCompilation(BuildTargetGroup[] platformTargets, string[] symbolsToInclude)
        {
            foreach (var type in platformTargets)
            {
                var hasEntry = new bool[symbolsToInclude.Length];
                var conditionals = PlayerSettings.GetScriptingDefineSymbolsForGroup(type).Trim();
                var parts = conditionals.Split(';');
                var changed = false;
 
                foreach (var part in parts)
                {
                    for (int i = 0; i < symbolsToInclude.Length; i++)
                    {
                        if (part.Trim() == symbolsToInclude[i].Trim())
                        {
                            hasEntry[i] = true;
                            break;
                        }
                    }
                }
 
                for (int i = 0; i < hasEntry.Length; i++)
                {
                    if (!hasEntry[i])
                    {
                        conditionals += (String.IsNullOrEmpty(conditionals) ? String.Empty : ";") + symbolsToInclude[i];
                        changed = true;
                    }
                }
 
                PlayerSettings.SetScriptingDefineSymbolsForGroup(type, conditionals);
 
                if (changed)
                {
                    Debug.Log(String.Format("Updated player conditional compilation symbols for {0}: {1}", type, conditionals));
                }
            }
        }

January 2013

  • Tip #49 – Visualizing Render Bounds

Here is a C# script for visualizing render bounds. Just attach it to a game object and it will draw the render bounds of the game object and all it’s children. Handy for debugging! RenderBoundsScript.png

    public class RendererBoundsGizmo : MonoBehaviour
    {
        public bool ShowCenter;
 
        public Color Color = Color.white;
 
        public bool DrawCube = true;
 
        public bool DrawSphere = false;
 
        /// <summary>
        /// When the game object is selected this will draw the gizmos
        /// </summary>
        /// <remarks>Only called when in the Unity editor.</remarks>
        private void OnDrawGizmosSelected()
        {
            Gizmos.color = this.Color;
 
            // get renderer bonding box
            var bounds = new Bounds();  
            var initBound = false;
            if (CBX.Utilities.Helpers.GetBoundWithChildren(this.transform, ref bounds, ref initBound))
            {
                if (this.DrawCube)
                {
                    Gizmos.DrawWireCube(bounds.center, bounds.size);
                }
                if (this.DrawSphere)
                {
                    Gizmos.DrawWireSphere(bounds.center, Mathf.Max(Mathf.Max(bounds.extents.x, bounds.extents.y), bounds.extents.z));
                }
            }
 
            if (this.ShowCenter)
            {
                Gizmos.DrawLine(new Vector3(bounds.min.x, bounds.center.y, bounds.center.z), new Vector3(bounds.max.x, bounds.center.y, bounds.center.z));
                Gizmos.DrawLine(new Vector3(bounds.center.x, bounds.min.y, bounds.center.z), new Vector3(bounds.center.x, bounds.max.y, bounds.center.z));
                Gizmos.DrawLine(new Vector3(bounds.center.x, bounds.center.y, bounds.min.z), new Vector3(bounds.center.x, bounds.center.y, bounds.max.z));
            }
 
            Handles.BeginGUI();
            var view = SceneView.currentDrawingSceneView;
            var pos = view.camera.WorldToScreenPoint(bounds.center);
            var size = GUI.skin.label.CalcSize(new GUIContent(bounds.ToString()));
            GUI.Label(new Rect(pos.x - (size.x / 2), -pos.y + view.position.height + 4, size.x, size.y), bounds.ToString());
            Handles.EndGUI();
        }
    }

And also the code for the GetBoundsWithChildren method.

        /// <summary>
        /// Gets the rendering bounds of the transform.
        /// </summary>
        /// <param name="transform">The game object to get the bounding box for.</param>
        /// <param name="pBound">The bounding box reference that will </param>
        /// <param name="encapsulate">Used to determine if the first bounding box to be calculated should be encapsulated into the <see cref="pBound"/> argument.</param>
        /// <returns>Returns true if at least one bounding box was calculated.</returns>
        public static bool GetBoundWithChildren(Transform transform, ref Bounds pBound, ref bool encapsulate)
        {
            var bound = new Bounds();
            var didOne = false;
 
            // get 'this' bound
            if (transform.gameObject.renderer != null)
            {
                bound = transform.gameObject.renderer.bounds;
                if (encapsulate)
                {
                    pBound.Encapsulate(bound.min);
                    pBound.Encapsulate(bound.max);
                }
                else
                {
                    pBound.min = bound.min; 
                    pBound.max = bound.max; 
                    encapsulate = true;
                }
 
                didOne = true;
            }
 
            // union with bound(s) of any/all children
            foreach (Transform child in transform)
            {
                if (GetBoundWithChildren(child, ref pBound, ref encapsulate))
                {
                    didOne = true;
                }
            }
 
            return didOne;
        }


  • Tip #48 – Refresh Inspector

Recently I needed to force the inspector to redraw itself from a piece of code outside of a Editor class. The solution I came up with was to call SetDirty on all selected objects of the type I was interested in. The inspector then picked up on the change and refreshed itself immediately. Although this code works under Unity 4 it is a hack and there may be a better way to force the inspector to redraw itself.

var objects = Selection.GetFiltered(typeof(GridMap), SelectionMode.TopLevel);
foreach (var o in objects)
{
    EditorUtility.SetDirty(o);
}

December 2012

  • Tip #47 - AssetPreview

Did you know your editor scripts can acquire a preview texture for an asset? Check out the members of the AssetPreview class.

  • Tip #46 – ExecuteInEditMode attribute

if you have MonoBehavior code that you want to run while in the unity editor you can use the ExecuteInEditMode attribute and your mono behavior will run as though it was in play mode.

  • Tip #45 – DrawGizmo attribute

The DrawGizmo attribute allows you to setup your gizmo drawing code some place other then with your MonoBehavior class. The code below shows an example of the DrawGizmo attribute used on a method within a Editor class.

    /// <summary>
    /// Provides a editor for the <see cref="TileMap"/> component
    /// </summary>
    [CustomEditor(typeof(TileMap))]
    public class TileMapEditor : Editor
    {
        /// The RenderMapGizmo method will be called if the map is selected. 
        [DrawGizmo(GizmoType.Selected | GizmoType.Active)]
        static void RenderMapGizmo(TileMap map, GizmoType gizmoType)
        {
            // store map width, height and position
            var mapWidth = map.Columns * map.CellWidth;
            var mapHeight = map.Rows * map.CellHeight;
            var position = map.transform.position;
            var activelayerHeight = map.ActiveLayer * map.Depth;
 
            if (map.drawGridLines)
            {
                // draw layer border
                Gizmos.color = Color.white;
                Gizmos.DrawLine(
                    position + new Vector3(0, activelayerHeight, 0), position + new Vector3(mapWidth, activelayerHeight, 0));
                Gizmos.DrawLine(
                    position + new Vector3(0, activelayerHeight, 0), position + new Vector3(0, activelayerHeight, mapHeight));
                Gizmos.DrawLine(
                    position + new Vector3(mapWidth, activelayerHeight, 0),
                    position + new Vector3(mapWidth, activelayerHeight, mapHeight));
                Gizmos.DrawLine(
                    position + new Vector3(0, activelayerHeight, mapHeight),
                    position + new Vector3(mapWidth, activelayerHeight, mapHeight));
 
                // more draw logic here
        }
    }

The alternative is that you can have a method in your MonoBehavior called OnDrawGizmosSelected

    /// <summary>
    /// Provides a component for tile mapping.
    /// </summary>
    public class TileMap : MonoBehaviour
    {
 
        /// <summary>
        /// When the game object is selected this will draw the gizmos
        /// </summary>
        /// <remarks>Only called when in the Unity editor.</remarks>
        private void OnDrawGizmosSelected()
        {
            // gizmo draw code goes here            
        }
    }

… But since gizmo drawing logic is typically for use within the unity editor the DrawGizmo attribute allows you to place the draw logic in a more appropriate location.

  • Tip #44 – Serializing your MonoBehaviors private fields

If you have private fields that are wrapped with a public property but you want the value of those private fields to be saved with your scene you can use the SerializeField attribute.

  • Tip #43 – With Unity 4 MonoBehaviors can now be namespaced

Did you know with the release of Unity 4 MonoBehaviours can now be inside namespaces! Finally we can better organize our codez!

November 2012

  • Tip# 42 – Mouse Cursors

Did you know you can customize the look of the mouse cursor in your game by calling Cursor.SetCursor?

  • Tip #41 - Implicit operators (automatic type casting)

This is not so much a unity specific tip but more of a C# language tip. if you are trying to port or adapt your game code from one platform to another like for example from XNA over to unity implicit operators may come in handy for automatically casting types and making your code much more readable and easier to port.

Link to MSDN Documentation here

Often times we programmers can fall into bad programming habits and we can forget or overlook language and API features that would otherwise make our lives easier. :P

  • Tip #40 - Billboarding

This code snip is for object billboarding. Just drop the script onto the object and it will automatically face the camera.

 public class CameraBillboard : MonoBehaviour
 {
    // the camera the the game object will be bill boarded to.
    public Camera m_Camera;
    // if true the game object will be positioned just in front of the cameras near clip plane
    public bool PositionInFrontOfCamera;
    // the offset to position the object when PositionInFrontOfCamera is true
    public float Offset = 0.001f;
 
    void Awake()
    {
        // if no camera has been specified just use main camera
        if (m_Camera == null) m_Camera = Camera.main;
    }
 
    void Update()
    {
        // get forward vector of the camera and normalize it
        var vec = m_Camera.transform.forward;
        vec.Normalize();
 
        // set the position of the game object just inside the cameras near clipping plane so it blocks the camera view
        if (this.PositionInFrontOfCamera) this.transform.position = m_Camera.transform.position + (vec * (m_Camera.nearClipPlane + this.Offset));
 
        // orient the game object to look at the camera
        this.transform.LookAt(this.transform.position + m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up);
    }
 }
  • Tip #39 – RequireComponent attribute

You can specify the RequireComponent attribute on a class that inherits from MonoBehavior and Unity will add the specified component if it is not already present when you add the script to the game object.

 [RequireComponent(typeof(RigidBody))]
 public class SomeBehaviorScript : MonoBehaviour
 {
 }
  • Tip #38 – Performance Optimizations

The unity docs have a great list of recommended performance optimizations available here.

October 2012

  • Tip #37 – Optimize procedurally generated meshes

If you are generating meshes procedurally via scripting you can optimize that mesh for drawing by using MeshUtility.Optimize.

  • Tip #36 – Specify DEFINE directives for conditional compilation

You can use editor scripts to specify additional DEFINE directives for conditional compilation by using the EditorUserBuildSettings.activeScriptCompilationDefines property.

  • Tip #35 – Unity editor Drag & Drop support

If you need your editor script to support drag and drop functionality you can use the methods provided in the DragAndDrop class.

  • Tip #34 – Detect when editor changes play mode state

If you need to know when the unity editor is changing play mode states you can hook into the EditorApplication.playmodeStateChanged callback.

  • Tip #33 – Play & Pause your game in the editor with scripting

Using EditorApplication.isPlaying & EditorApplication.isPaused you can control when you want to run or pause your game in the unity editor using scripting.

September 2012

  • Tip #32 – Can you hear me now?

Unity provides access to the users microphone via the Microphone class.

  • Tip #31 – Location awareness

Unity provides support for location awareness such as longitude, latitude, and altitude in the LocationService class.

  • Tip #30 – Social Platforms

Unity provides built in support for various social platforms including support for custom implementations through the Social class.

  • Tip #29 – Gyroscopes

If the device supports it you can use the Gyroscope class to get gyroscopic information about the orientation and acceleration of the device.

August 2012

  • Tip #28 – System Information

You can get detailed system information by using the SystemInfo class. SystemInfo provides information on the graphic device hardware, processor count, operating system, device type and more. Very handy for gathering stats/trends on your users hardware specs.

  • Tip #27 – OpenGL Drawing

The Unity API provides access to a OpenGL style graphics for immediate mode drawing in the class called “GL”.

  • Tip #26 - Frustum AABB collisions

You can perform Frustum and AABB collision detection using something similar to the fallowing code ...

        public object[] GetObjects(Vector3 position, float distance, float fov, Vector3 direction)
        {
            var results = new List<GameObject>();
 
            // search all game objects
            var objects = UnityEngine.Object.FindObjectsOfType(typeof(GameObject));
 
            if (this.camera == null)
            {
                this.cameraGameObject = new GameObject();
                this.camera = this.cameraGameObject.AddComponent<Camera>();
            }
 
            this.cameraGameObject.transform.position = position;
            this.cameraGameObject.transform.forward = direction;
            this.camera.fov = fov;
            this.camera.far = distance;
 
            var planes = GeometryUtility.CalculateFrustumPlanes(this.camera);
 
            foreach (GameObject obj in objects)
            {
                if (obj == this.cameraGameObject)
                {
                    continue;
                }
 
                // if no collider just ignore
                if (obj.collider == null)
                {
                    continue;
                }
 
                if (GeometryUtility.TestPlanesAABB(planes, obj.collider.bounds))
                {
                    results.Add(obj);
                }
            }
 
            return results.ToArray();
        }
  • Tip #25 – Get/Set caret position for TextArea/TextField object

You can get access to more powerful controls by using an undocumented unity api the TextEditor control.

 var te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
 if (te != null) 
 {
     te.MoveCursorToPosition(new Vector2(5555, 5555));
 }

July 2012

  • Tip #24 – Conditional Compilation

If you need to know what symbols Unity uses for conditional compilation check out the docs page Platform Dependent Compilation

  • Tip #23 – Call JavaScript functions from web player

You can use Application.ExternalCall to call JavaScript functions on the page the web player is displayed in.

  • Tip #22 – Command line arguments

Did you know you can pass in command line arguments when running Unity as well as with the standalone players! Read Here.

  • Tip #21 – Detailed Logs

If you need to get more detailed logging information check out the Log Files page.

  • Tip #20 - Importing Animations using multiple model files

If you have a modal with animation(s) you can export the model without any animation. Then export additional copies of the model with animation and with the same name but with @animationname at the end of the file name. Read the section on “Importing Animations using multiple model files” here.

  • Tip #19 – Determine platform

If you need to know what platform your code is running under you can use Application.platform.

  • Tip #18 - Running Editor Script Code on Launch

If you need to run some script code as soon as the editor has launched you can use the InitializeOnLoad attribute.

  • Tip #17 - MenuItem shortcut keys

With scripting you can add menu items that have shortcut keys assigned to them. Read the MenuItem description for more information.

  • Tip #16 - Gizmos

A great way to help debug your game is to use Gizmos to visually represent empty game objects in your scene. BurgZergArcade example

  • Tip #15 - Tags

When working with multiple duplicate game objects you may want use tags to uniquely identify a specific object. You can then use GameObject.FindGameObjectsWithTag to select that specific object in your scripts.

  • Tip #14 - Unity Console Output

If you need to output information to the unity console check out the Debug class.

June 2012

  • Tip #13 - Screen Resolution

If you need to set the screen resolution of your game via scripting check out Screen.SetResolution.

  • Tip #12 - Collider manipulation

Did you know that you can manipulate a game objects collider in the scene view by holding down the 'Shift' key. Holding shift will display the colliders control dots. Just drag the dots to adjust the collider!

  • Tip #11 - Snap to vertex

Hold the 'V' key while the translate tool is active and you can drag an object by it's vertices and snap that vertex to another objects vertex!

  • Tip #10 - Inspector lock

The inspector window contains a small lock icon in the top right of the window. If you select a game object and click the lock the inspector will be locked to that object and will not change when you select other objects. Click again to disable the lock. This allows you to have multiple inspector windows open with locks to different objects.

  • Tip #9 - Adding Undo support to custom editor scripts

To allow the user to undo actions that are performed by a custom editor script you can use Undo.RegisterUndo.

    foreach (var item in Selection.transforms)
    {
        Undo.RegisterUndo(item, "Move " + item.name);
        var pos = item.position;
        pos = new Vector3(activeObject.transform.position.x, pos.y, pos.z);
        item.transform.position = pos;
    }
  • Tip #8 - Execute menu items from editor script

If you want to take advantage of functionality that is provided by menu commands you can use EditorApplication.ExecuteMenuItem in your editor scripts to execute them.

  • Tip #7 - Hide/show objects in the hierarchy window

Are you working on a large project but dislike how many objects you have to weed through in the hierarchy window? You can create editor scripts that change the objects Object.hideFlags property to hide or show the object in the hierarchy window.

    var item = Selection.activeGameObject;
    if ((item.hideFlags & HideFlags.HideInHierarchy) == HideFlags.HideInHierarchy)
    {
        item.hideFlags = item.hideFlags ^ HideFlags.HideInHierarchy;
        item.active = !item.active;
        item.active = !item.active;
    }

Notice that item.active = !item.active is specified twice. Currently in unity 3.5 you need those 2 lines of code for the game object to hide and show properly in the hierarchy window.

NOTICE: GameObject.active is obsolete use GameObject.SetActive instead

  • Tip #6 - Saving editor/player settings across sessions

Wondering how to save player preferences across sessions or save settings for an editor script to use? Check out EditorPrefs & PlayerPrefs.

  • Tip #5 - Custom Preferences Window settings

If you are writing custom editors for unity and you need to have a way of editing the tools settings you can have those settings show up in the Unity Preferences Window "Edit -> Preferences...".

Just create a new editor script that has a public class and add the fallowing lines of code ...

    // Add preferences section named "cbxTools" to the Preferences Window
    [PreferenceItem("cbxTools")]
    public static void PreferencesGUI()
    {
        // Preferences GUI
        cbxToolPreferencesConfig.Instance.DrawGUI();
    }

CustomPreferences.png

  • Tip #4 - Draw editor gui's only in active scene view

If you are creating custom editors for unity but only want the gui for that editor to be drawn in the currently active scene view try this ...

    if (SceneView.mouseOverWindow.GetInstanceID() == SceneView.currentDrawingSceneView.GetInstanceID())
    {
        this.DrawGUI(SceneView.currentDrawingSceneView);
    }

May 2012

  • Tip #3 - Infinite Recursion / Stack Overflow

If your suddenly finding that Unity will instantly terminate while running your code, check if you are invoking any infinite recursion loops. Unity tends to terminate instantly if this occurs in your code.

  • Tip #2 - Game object Icons

You can visually tag your game objects with icons by selecting the object and clicking the Red/Green/Blue colored cube in the upper left of the inspector window.

UnityIcons.png

You can select a colored Label, Dot, or a Texture from your assets.

  • Tip #1 - Prevent config dialog from showing when running your compiled game

When you building your standalone unity game using "File -> Build & Run" and then run the *.exe that was generated you will see a dialog pop up.

To prevent the configuration window from being shown select "File -> Build Settings..." in unity and click the "Player Settings" button at the bottom of the window. The Unity inspector window will contain a number of options. Find the drop down titled "Display Resolution Dialog" and set it to "Disabled". Rebuild your game and run the generated *.exe. No more dialog pop up!

Older Tips

  • 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.

Note: Place your new tip at the top of the list, not at the bottom.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox