Tip of the day entries for 2012

December 2012
Did you know your editor scripts can acquire a preview texture for an asset? Check out the members of the AssetPreview class.
 * Tip #47 - AssetPreview

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 #46 – ExecuteInEditMode 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.
 * Tip #45 – DrawGizmo attribute

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

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

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 #44 – Serializing your MonoBehaviors private fields

Did you know with the release of Unity 4 MonoBehaviours can now be inside namespaces! Finally we can better organize our codez!
 * Tip #43 – With Unity 4 MonoBehaviors can now be namespaced

November 2012
Did you know you can customize the look of the mouse cursor in your game by calling Cursor.SetCursor?
 * Tip# 42 – Mouse Cursors

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.
 * Tip #41 - Implicit operators (automatic type casting)

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

This code snip is for object billboarding. Just drop the script onto the object and it will automatically face the camera.
 * Tip #40 - Billboarding

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.
 * Tip #39 – RequireComponent attribute

The unity docs have a great list of recommended performance optimizations available here.
 * Tip #38 – Performance Optimizations

October 2012
If you are generating meshes procedurally via scripting you can optimize that mesh for drawing by using MeshUtility.Optimize.
 * Tip #37 – Optimize procedurally generated meshes

You can use editor scripts to specify additional DEFINE directives for conditional compilation by using the EditorUserBuildSettings.activeScriptCompilationDefines property.
 * Tip #36 – Specify DEFINE directives for conditional compilation

If you need your editor script to support drag and drop functionality you can use the methods provided in the DragAndDrop class.
 * Tip #35 – Unity editor Drag & Drop support

If you need to know when the unity editor is changing play mode states you can hook into the EditorApplication.playmodeStateChanged callback.
 * Tip #34 – Detect when editor changes play mode state

Using EditorApplication.isPlaying & EditorApplication.isPaused you can control when you want to run or pause your game in the unity editor using scripting.
 * Tip #33 – Play & Pause your game in the editor with scripting

September 2012
Unity provides access to the users microphone via the Microphone class.
 * Tip #32 – Can you hear me now?

Unity provides support for location awareness such as longitude, latitude, and altitude in the LocationService class.
 * Tip #31 – Location awareness

Unity provides built in support for various social platforms including support for custom implementations through the Social class.
 * Tip #30 – Social Platforms

If the device supports it you can use the Gyroscope class to get gyroscopic information about the orientation and acceleration of the device.
 * Tip #29 – Gyroscopes

August 2012
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 #28 – System Information

The Unity API provides access to a OpenGL style graphics for immediate mode drawing in the class called “GL”.
 * Tip #27 – OpenGL Drawing

You can perform Frustum and AABB collision detection using something similar to the fallowing code ...
 * Tip #26 - Frustum AABB collisions

You can get access to more powerful controls by using an undocumented unity api the TextEditor control.
 * Tip #25 – Get/Set caret position for TextArea/TextField object

July 2012
If you need to know what symbols Unity uses for conditional compilation check out the docs page Platform Dependent Compilation
 * Tip #24 – Conditional Compilation

You can use Application.ExternalCall to call JavaScript functions on the page the web player is displayed in.
 * Tip #23 – Call JavaScript functions from web player

Did you know you can pass in command line arguments when running Unity as well as with the standalone players! Read Here.
 * Tip #22 – Command line arguments

If you need to get more detailed logging information check out the Log Files page.
 * Tip #21 – Detailed Logs

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 #20 - Importing Animations using multiple model files

If you need to know what platform your code is running under you can use Application.platform.
 * Tip #19 – Determine platform

If you need to run some script code as soon as the editor has launched you can use the InitializeOnLoad attribute.
 * Tip #18 - Running Editor Script Code on Launch

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

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

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 #15 - Tags

If you need to output information to the unity console check out the Debug class.
 * Tip #14 - Unity Console Output

June 2012
If you need to set the screen resolution of your game via scripting check out Screen.SetResolution.
 * Tip #13 - Screen Resolution

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 #12 - Collider manipulation

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 #11 - Snap to vertex

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 #10 - Inspector lock

To allow the user to undo actions that are performed by a custom editor script you can use Undo.RegisterUndo.
 * Tip #9 - Adding Undo support to custom editor scripts

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 #8 - Execute menu items from editor script

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.
 * Tip #7 - Hide/show objects in the hierarchy window

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.

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...".
 * Tip #5 - Custom Preferences Window settings

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




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

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.



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

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.
 * Tip #1 - Prevent config dialog from showing when running your compiled game

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!