Unity3D Joystick Script

Here we are again. 3 cups of coffee later and another script conversion. I’ve taken the time to convert the Penelope Joystick.cs JavaScript script into C-Sharp. Why would someone do this? I did it because I am getting tired of anything that is not strongly typed in Unity3D. If you are writing your engine in JavaScript, you’re probably making variables with or without typos that don’t have the scope you thought they did and worse, you might have a 6-8 hour hot date with your code when you can’t figure out why it’s crashing due to some strange “type assumptions”. Don’t get me wrong, hot dates are a blast, especially when they aren’t with your computer.

In my usual way, here is the Joystick script converted over into C-Sharp.

May 17, 2011: Oh one more thing, I’m posting this early, call it jumping the gun, I haven’t tested it yet. I’ll be testing in the next 24 hours.

View Code CSHARP
using UnityEngine;
 * File: MPJoystick.cs
 * Author: Chris Danielson of (monkeyprism.com)
// USED TO BE: Joystick.js taken from Penelope iPhone Tutorial
// Joystick creates a movable joystick (via GUITexture) that 
// handles touch input, taps, and phases. Dead zones can control
// where the joystick input gets picked up and can be normalized.
// Optionally, you can enable the touchPad property from the editor
// to treat this Joystick as a TouchPad. A TouchPad allows the finger
// to touch down at any point and it tracks the movement relatively 
// without moving the graphic
public class MPJoystick : MonoBehaviour
	class Boundary {
		public Vector2 min = Vector2.zero;
		public Vector2 max = Vector2.zero;
	private static MPJoystick[] joysticks;					// A static collection of all joysticks
	private static bool enumeratedJoysticks = false;
	private static float tapTimeDelta = 0.3f;				// Time allowed between taps
	public bool touchPad;
	public Vector2 position = Vector2.zero;
	public Rect touchZone;
	public Vector2 deadZone = Vector2.zero;						// Control when position is output
	public bool normalize = false; 							// Normalize output after the dead-zone?
	public int tapCount;	
	private int lastFingerId = -1;								// Finger last used for this joystick
	private float tapTimeWindow;							// How much time there is left for a tap to occur
	private Vector2 fingerDownPos;
	//private float fingerDownTime;
	//private float firstDeltaTime = 0.5f;
	private GUITexture gui;
	private Rect defaultRect;								// Default position / extents of the joystick graphic
	private Boundary guiBoundary = new Boundary();			// Boundary for joystick graphic
	private Vector2 guiTouchOffset;						// Offset to apply to touch input
	private Vector2 guiCenter;							// Center of joystick
	void Start() {
		gui = (GUITexture)GetComponent(typeof(GUITexture));
		defaultRect = gui.pixelInset;
		defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5;
    	defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5;
		transform.position = Vector3.zero;
		if (touchPad) {
			// If a texture has been assigned, then use the rect ferom the gui as our touchZone
			if ( gui.texture )
				touchZone = defaultRect;
		} else {	
			guiTouchOffset.x = defaultRect.width * 0.5f;
			guiTouchOffset.y = defaultRect.height * 0.5f;
			// Cache the center of the GUI, since it doesn't change
			guiCenter.x = defaultRect.x + guiTouchOffset.x;
			guiCenter.y = defaultRect.y + guiTouchOffset.y;
			// Let's build the GUI boundary, so we can clamp joystick movement
			guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
			guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
			guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
			guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
	public Vector2 getGUICenter() {
		return guiCenter;
	void Disable() {
		gameObject.active = false;
		//enumeratedJoysticks = false;	
	private void ResetJoystick() {
		gui.pixelInset = defaultRect;
		lastFingerId = -1;
		position = Vector2.zero;
		fingerDownPos = Vector2.zero;	
	private bool IsFingerDown() {
		return (lastFingerId != -1);
	public void LatchedFinger(int fingerId) {
		// If another joystick has latched this finger, then we must release it
		if ( lastFingerId == fingerId )
	void Update() {
		if (!enumeratedJoysticks) {
			// Collect all joysticks in the game, so we can relay finger latching messages
			joysticks = (MPJoystick[])FindObjectsOfType(typeof(MPJoystick));	
			enumeratedJoysticks = true;
		int count = Input.touchCount;
		if ( tapTimeWindow > 0 )
			tapTimeWindow -= Time.deltaTime;
			tapCount = 0;
		if ( count == 0 )
			for(int i = 0; i < count; i++) {
				Touch touch = Input.GetTouch(i);			
				Vector2 guiTouchPos = touch.position - guiTouchOffset;
				bool shouldLatchFinger = false;
				if (touchPad) {
					if (touchZone.Contains(touch.position))
						shouldLatchFinger = true;
				else if (gui.HitTest(touch.position)) {
					shouldLatchFinger = true;
				// Latch the finger if this is a new touch
				if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId )) {
					if (touchPad) {
						//gui.color.a = 0.15;
						lastFingerId = touch.fingerId;
						//fingerDownPos = touch.position;
						//fingerDownTime = Time.time;
					lastFingerId = touch.fingerId;
					// Accumulate taps if it is within the time window
					if ( tapTimeWindow > 0 )
					else {
						tapCount = 1;
						tapTimeWindow = tapTimeDelta;
					// Tell other joysticks we've latched this finger
					//for (  j : Joystick in joysticks )
					foreach (MPJoystick j in joysticks) {
						if (j != this) 
							j.LatchedFinger( touch.fingerId );
				if ( lastFingerId == touch.fingerId ) {	
					// Override the tap count with what the iPhone SDK reports if it is greater
					// This is a workaround, since the iPhone SDK does not currently track taps
					// for multiple touches
					if ( touch.tapCount > tapCount )
						tapCount = touch.tapCount;
					if ( touchPad ) {
						// For a touchpad, let's just set the position directly based on distance from initial touchdown
						position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
						position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
					} else {					
						// Change the location of the joystick graphic to match where the touch is
						Rect r = gui.pixelInset;
						r.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
						r.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );		
						gui.pixelInset = r;
					if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
		if (!touchPad) {
			// Get a value between -1 and 1 based on the joystick graphic location
			position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
			position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
		// Adjust for dead zone	
		var absoluteX = Mathf.Abs( position.x );
		var absoluteY = Mathf.Abs( position.y );
		if (absoluteX < deadZone.x) {
			// Report the joystick as being at the center if it is within the dead zone
			position.x = 0;
		else if (normalize) {
			// Rescale the output after taking the dead zone into account
			position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
		if (absoluteY < deadZone.y) {
			// Report the joystick as being at the center if it is within the dead zone
			position.y = 0;
		else if (normalize) {
			// Rescale the output after taking the dead zone into account
			position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );

, , , , ,


Using Apple Property List Files with Unity3D

Persistent storage and data retrieval can be troubling without a reference in your environment to the SQLite library. In the case of Unity3D, there is no direct SQLite library wrappers offered out of the box. One can write a device specific SQLite wrapper (or purchase one) that bridges the gap into the Unity3D JavaScript and CSharp world or one can decide on a simpler method for storing and managing data. Enter Apple’s .plist or property list files. These files can be saved in binary and also in a standard text format. For our situation, we’re going to manage these files that are stored in standard text format. It is arguable that using a custom plist parser is going to be slower than using SQLite, but with a small enough subset of data I believe that plist should be an adequate solution.

The unity community has a free version of a PropertyListSerializer.js file. I copied this code outright and converted it into the strongly typed C-Sharp environment. My reasoning was simple, I found that using JavaScript wasn’t going to work with my current project for a few reasons so I decided to convert this file over into C-Sharp code. I did attempt to upload my changes to that site, but it continued to complain that I was getting blocked by the spam check functions.


Edit: May 13, 2011 After testing on the actual iOS device I discovered that DTD validation is not implemented in Unity. I’ve now disabled the XmlDocument forced validation.

View Code CSHARP
 * Converted By: Chris Danielson of http://MonkeyPrism.com
 * Converted Date: May 8, 2011
 * Original JavaScript Auther: capnbishop
 * Conversion of a Unity 3D Community script from JavaScript to type controlled C# Mono.Net.
 * Please note, that the original object was named PropertyListSerializer.  I renamed it to prevent any conflicts with the original script.
 * The PropertyListSerializer.js (CD: renamed now to PListManager) script is used to load and save an XML property list file to and from a hierarchical hashtable 
 * (in which the root hashtable can contain child hashtables and arrays). This can provide a convenient and dynamic means of serializing 
 * a complex hierarchy of game data into XML files.
 * When loading, the resulting hashtable can include 8 different types of values: string, integer, real, date, data, boolean, dictionary, and array. 
 * Data elements are loaded as strings. Dictionaries are loaded as hashtables. Arrays are loaded as arrays. Each value is loaded with an 
 * associating key, except for elements of an array. Thus, each child hashtable and array also have associating keys, and can be combined to 
 * create a complex hierarchy of key value pairs and arrays.
 * When saving, the resulting XML file will contain the same hierarchy of data. All data will end up being stored as a string, but with an 
 * associated value type. Strings, integers, and decimals values are stored as such. Dates are stored in ISO 8601 format. Hashtables are stored 
 * as a plist key/value dictionary, and arrays as a series of keyless values.
 * The loader passes a lot of values by reference, and performs a considerable amount of recursion. Primitive values had to be passed by reference. 
 * Unity's JavaScript only passes objects by reference, and cannot explicitly pass a primitive by reference. As such, we've had to create a 
 * special ValueObject, which is just an abstract object that holds a single value. This object is then passed by reference, and the primitive 
 * value is set to its val property.
 * This plist loader conforms to Apple's plist DOCTYPE definition: http://www.apple.com/DTDs/PropertyList-1.0.dtd
 * Original JavaScript URL:  http://www.unifycommunity.com/wiki/index.php?title=PropertyListSerializer
 * Example Saving:
 		Hashtable playerData = new Hashtable();
		playerData.Add("TestObject", 1.5f);
		ArrayList guns = new ArrayList();
	    playerData.Add("Guns", guns);
		Hashtable grenades = new Hashtable();
		grenades.Add("FragmentationCount", 1);
		grenades.Add("IncendiaryCount", 1);
		playerData.Add("Grenades", grenades);
		//save outside the current project (same folder as Assets and Library)
		String xmlFile = Application.dataPath + "/../ExampleSaveFile.plist"; 
		PListManager.SavePlistToFile(xmlFile, playerData);
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Text;
using System.Globalization;
using UnityEngine;
public class PListManager {
	public PListManager() { }
	private const string SUPPORTED_VERSION = "1.0";
	public static bool ParsePListFile(string xmlFile, ref Hashtable plist) {                       
		if (!File.Exists(xmlFile)) { 
			Debug.LogError("File doesn't exist: " + xmlFile); 
			return false; 
		StreamReader sr = new StreamReader(xmlFile);
		string txt = sr.ReadToEnd();
		XmlDocument xml = new XmlDocument();
                xml.XmlResolver = null; //Disable schema/DTD validation, it's not implemented for Unity.
		XmlNode plistNode = xml.LastChild;
		if (!plistNode.Name.Equals("plist")) { 
			Debug.LogError("plist file missing <plist> nodes." + xmlFile);
			return false;
		string plistVers = plistNode.Attributes["version"].Value;
		if (plistVers == null || !plistVers.Equals(SUPPORTED_VERSION)) { 
			Debug.LogError("This is an unsupported plist version: " + plistVers + ". Required version:a " + SUPPORTED_VERSION); 
			return false;
		XmlNode dictNode = plistNode.FirstChild;
    	if (!dictNode.Name.Equals("dict")) { 
			Debug.LogError("Missing root dict from plist file: " + xmlFile); 
			return false; 
		return LoadDictFromPlistNode(dictNode, ref plist);
	private static bool LoadDictFromPlistNode(XmlNode node, ref Hashtable dict) {
		if (node == null) { 
			Debug.LogError("Attempted to load a null plist dict node.");
			return false;
		if (!node.Name.Equals("dict")) { 
			Debug.LogError("Attempted to load an dict from a non-array node type: " + node + ", " + node.Name); 
			return false;
		if (dict == null) { 
			dict = new Hashtable();
		int cnodeCount = node.ChildNodes.Count;
		for (int i = 0; i+1 < cnodeCount; i+=2) {
        	// Select the key and value child nodes
			XmlNode keynode = node.ChildNodes.Item(i);
			XmlNode valuenode = node.ChildNodes.Item(i+1);
			// If this node isn't a 'key'
        	if (keynode.Name.Equals("key")) {
				// Establish our variables to hold the key and value.
				string key = keynode.InnerText;
				ValueObject value = new ValueObject();
	            // Load the value node.
	            // If the value node loaded successfully, add the key/value pair to the dict hashtable.
	            if (LoadValueFromPlistNode(valuenode, ref value)) {
	                // This could be one of several different possible data types, including another dict.
	                // AddKeyValueToDict() handles this by replacing existing key values that overlap, and doing so recursively for dict values.
	                // If this not successful, post a message stating so and return false.
	                if (!AddKeyValueToDict(ref dict, key, value)) {
						Debug.LogError("Failed to add key value to dict when loading plist from dict"); 
						return false;
	            } else { 
					Debug.LogError("Did not load plist value correctly for key in node: " + key + ", " + node);
					return false;
	        } else { 
				Debug.LogError("The plist being loaded may be corrupt.");
				return false;
		} //end for
		return true;
	private static bool LoadValueFromPlistNode(XmlNode node, ref ValueObject value) {
		if (node == null) { 
			Debug.LogError("Attempted to load a null plist value node."); 
			return false;
		if (node.Name.Equals("string")) { value.val = node.InnerText; }
	    else if (node.Name.Equals("integer")) { value.val = int.Parse(node.InnerText); }
	    else if (node.Name.Equals("real")) { value.val = float.Parse(node.InnerText); }
	    else if (node.Name.Equals("date")) { value.val = DateTime.Parse(node.InnerText, null, DateTimeStyles.None); } // Date objects are in ISO 8601 format
	    else if (node.Name.Equals("data")) { value.val = node.InnerText; } // Data objects are just loaded as a string
	    else if (node.Name.Equals("true")) { value.val = true; } // Boollean values are empty objects, simply identified with a name being "true" or "false"
	    else if (node.Name.Equals("false")) { value.val = false; }
	    // The value can be an array or dict type.  In this case, we need to recursively call the appropriate loader functions for dict and arrays.
	    // These functions will in turn return a boolean value for their success, so we can just return that.
	    // The val value also has to be instantiated, since it's being passed by reference.
	    else if (node.Name.Equals("dict")) { 
			value.val = new Hashtable();
			Hashtable htRef = (Hashtable)value.val;
			return LoadDictFromPlistNode(node, ref htRef);
	    else if (node.Name.Equals("array")) {
			value.val = new ArrayList();
			ArrayList alRef = (ArrayList)value.val;
			return LoadArrayFromPlistNode(node, ref alRef);
		} else { 
			Debug.LogError("Attempted to load a value from a non value type node: " + node + ", " + node.Name);
			return false;
	    return true;
	private static bool LoadArrayFromPlistNode(XmlNode node, ref ArrayList array ) {
	    // If we were passed a null node object, then post an error stating so and return false
	    if (node == null) { 
			Debug.LogError("Attempted to load a null plist array node.");
			return false;
	    // If we were passed a non array node, then post an error stating so and return false
	    if (!node.Name.Equals("array")) { 
			Debug.LogError("Attempted to load an array from a non-array node type: " + node + ", " + node.Name); 
			return false;
	    // We can be passed an empty array object.  If so, initialize it
	    if (array == null) { array = new ArrayList(); }
	    // Itterate through the child nodes for this array object
	    int nodeCount = node.ChildNodes.Count;
	    for (int i = 0; i < nodeCount; i++) {
	        // Establish variables to hold the child node of the array, and it's value
	        XmlNode cnode = node.ChildNodes.Item(i);
	        ValueObject element = new ValueObject();
	        // Attempt to load the value from the current array node.
	        // If successful, add it as an element of the array.  If not, post and error stating so and return false.
	        if (LoadValueFromPlistNode(cnode, ref element)) { 
			} else { 
				return false; 
	    // If we made it through the array without errors, return true
	    return true;
	private static bool AddKeyValueToDict(ref Hashtable dict, string key, ValueObject value) {
	    // Make sure that we have values that we can work with.
	    if (dict == null || key == null || key.Length < 1 || value == null) { 
			Debug.LogError("Attempted to AddKeyValueToDict() with null objects.");
			return false;
	    // If the hashtable doesn't already contain the key, they we can just go ahead and add it.
	    if (!dict.ContainsKey(key)) { 
			dict.Add(key, value.val);
			return true;
	    // At this point, the dict contains already contains the key we're trying to add.
	    // If the value for this key is of a different type between the dict and the new value, then we have a type mismatch.
	    // Post an error stating so, but go ahead and overwrite the existing key value.
		if (value.val.GetType() != dict[key].GetType()) {
	        Debug.LogWarning("Value type mismatch for overlapping key (will replace old value with new one): " + value.val + ", " + dict[key] + ", " + key);
	        dict[key] = value.val;
    	// If the value for this key is a hashtable, then we need to recursively add the key values of each hashtable.
    	else if (value.val.GetType() == typeof(Hashtable)) {
	        // Itterate through the elements of the value's hashtable.
			Hashtable htTmp = (Hashtable)value.val;
	        foreach (object element in htTmp) {
	            // Recursively attempt to add/repalce the elements of the value hashtable to the dict's value hashtable.
	            // If this fails, post a message stating so and return false.
				Hashtable htRef = (Hashtable)dict[key];
	            if (!AddKeyValueToDict(ref htRef, (string)element, new ValueObject(htTmp[element]))) {
	                Debug.LogError("Failed to add key value to dict: " + element + ", " + htTmp[element] + ", " + dict[key]);
	                return false;
	    // If the value is an array, then there's really no way we can tell which elements to overwrite, because this is done based on the congruent keys.
	    // Thus, we'll just add the elements of the array to the existing array.
	    else if (value.val.GetType() == typeof(ArrayList)) {
			ArrayList alTmp = (ArrayList)value.val;
			ArrayList alAddTmp = (ArrayList)dict[key];
			foreach (object element in alTmp) {
	    // If the key value is not an array or a hashtable, then it's a primitive value that we can easily write over.
	    else { 
			dict[key] = value.val;
    	return true;
	public static bool SavePlistToFile (String xmlFile, Hashtable plist) {
	    // If the hashtable is null, then there's apparently an issue; fail out.
	    if (plist == null) { 
			Debug.LogError("Passed a null plist hashtable to SavePlistToFile.");
			return false;
	    // Create the base xml document that we will use to write the data
	    XmlDocument xml = new XmlDocument();
	    xml.XmlResolver = null; //Disable schema/DTD validation, it's not implemented for Unity.
	    // Create the root XML declaration
	    // This, and the DOCTYPE, below, are standard parts of a XML property list file
	    XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
	    // Create the DOCTYPE
	    XmlDocumentType doctype = xml.CreateDocumentType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
	    // Create the root plist node, with a version number attribute.
	    // Every plist file has this as the root element.  We're using version 1.0 of the plist scheme
	    XmlNode plistNode = xml.CreateNode(XmlNodeType.Element, "plist", null);
	    XmlAttribute plistVers = (XmlAttribute)xml.CreateNode(XmlNodeType.Attribute, "version", null);
	    plistVers.Value = "1.0";
	    // Now that we've created the base for the XML file, we can add all of our information to it.
	    // Pass the plist data and the root dict node to SaveDictToPlistNode, which will write the plist data to the dict node.
	    // This function will itterate through the hashtable hierarchy and call itself recursively for child hashtables.
	    if (!SaveDictToPlistNode(plistNode, plist)) {
	        // If for some reason we failed, post an error and return false.
	        Debug.LogError("Failed to save plist data to root dict node: " + plist);
	        return false;
	    } else { // We were successful
	        // Create a StreamWriter and write the XML file to disk.
	        // (do not append and UTF-8 are default, but we're defining it explicitly just in case)
	        StreamWriter sw = new StreamWriter(xmlFile, false, System.Text.Encoding.UTF8);
	    // We're done here.  If there were any failures, they would have returned false.
	    // Return true to indicate success.
	    return true;
	private static bool SaveDictToPlistNode(XmlNode node, Hashtable dict) {
	    // If we were passed a null object, return false
	    if (node == null) {
			Debug.LogError("Attempted to save a null plist dict node.");
			return false;
	    XmlNode dictNode = node.OwnerDocument.CreateNode(XmlNodeType.Element, "dict", null);
	    // We could be passed an null hashtable.  This isn't necessarily an error.
	    if (dict == null) { 
			Debug.LogWarning("Attemped to save a null dict: " + dict); 
			return true;
	    // Iterate through the keys in the hashtable
	    //for (var key in dict.Keys) {
		foreach (object key in dict.Keys) {
	        // Since plists are key value pairs, save the key to the plist as a new XML element
	        XmlElement keyNode = node.OwnerDocument.CreateElement("key");
	        keyNode.InnerText = (string)key;
	        // The name of the value element is based on the datatype of the value.  We need to serialize it accordingly.  Pass the XML node and the hash value to SaveValueToPlistNode to handle this.
	        if (!SaveValueToPlistNode(dictNode, dict[key])) {
	            // If SaveValueToPlistNode() returns false, that means there was an error.  Return false to indicate this up the line.
	            Debug.LogError("Failed to save value to plist node: " + key);
	            return false;
	    // If we got this far then all is well.  Return true to indicate success.
	    return true;
	private static bool SaveValueToPlistNode(XmlNode node, object value) {
	    // The node passed will be the parent node to the new value node.
	    XmlNode valNode;
	    System.Type type = value.GetType();
	    // Identify the data type for the value and serialize it accordingly
	    if (type == typeof(String)) { 
			valNode = node.OwnerDocument.CreateElement("string"); 
	    else if (type == typeof(Int16) || 
		         type == typeof(Int32) ||
		         type == typeof(Int64)) { valNode = node.OwnerDocument.CreateElement("integer"); }
	    else if (type == typeof(Single) || 
		         type == typeof(Double) ||
		         type == typeof(Decimal)) { valNode = node.OwnerDocument.CreateElement("real"); }
	    else if (type == typeof(DateTime)) {
	        // Dates need to be stored in ISO 8601 format
	        valNode = node.OwnerDocument.CreateElement("date");
			DateTime dt = (DateTime)value;
	        valNode.InnerText = dt.ToUniversalTime().ToString("o");
	        return true;
	    else if (type == typeof(bool)) {
	        // Boolean values are empty elements, simply being stored as an elemement with a name of true or false
	        if ((bool)value == true) { valNode = node.OwnerDocument.CreateElement("true"); }
	        else { valNode = node.OwnerDocument.CreateElement("false"); }
	        return true;
	    // Hashtables and arrays require special functions to save their values in an itterative and recursive manner.
	    // The functions will return true/false to indicate success/failure, so pass those on.
	    else if (type == typeof(Hashtable))    { 
			return SaveDictToPlistNode(node, (Hashtable)value); 
	    else if (type == typeof(ArrayList)) { return SaveArrayToPlistNode(node, (ArrayList)value); }
	    // Anything that doesn't fit the defined data types will just be stored as "data", which is effectively a string.
	    else { 
			valNode = node.OwnerDocument.CreateElement("data");
	    // Some of the values (strings, numbers, data) basically get stored as a string.  The rest will store their values in their special format and return true for success.  If we made it this far, then the value in valNode must be stored as a string.
	    if (valNode != null) valNode.InnerText = value.ToString();
	    // We're done.  Return true for success.
	    return true;
	private static bool SaveArrayToPlistNode (XmlNode node, ArrayList array) {
	    // Create the value node as an "array" element.
	    XmlElement arrayNode = node.OwnerDocument.CreateElement("array");
	    // Each element in the array can be any data type.  Itterate through the array and send each element to SaveValueToPlistNode(), where it can be stored accordingly based on its data type.
	    foreach (object element in array) {
	        // If SaveValueToPlistNode() returns false, then there was a problem.  Return false in that case.
	        if (!SaveValueToPlistNode(arrayNode, element)) { return false; }
    	return true;
} //end PListManager class
class ValueObject { 
	public object val;
	public ValueObject() {}
	public ValueObject(object aVal) { val = aVal; }

, , , , , ,


Unity3D File Based Assetbundles on the iPhone

Let me start off by saying that Unity3D is a really great game engine. Let me also say that I wish the documentation was as great as the engine is. Sadly, it’s difficult to figure out some of the more powerful features of Unity3D without significant trial and error.

Please note, that I have read that the usage of Assetbundles on the iPhone can be memory intensive and may or may not fit your needs per your project. I am just putting this information here so that others can see how to do this. Pro Tip: What this means to you? Profile the heck out of your code, pre-assetbundles and after integrating assetbundles.

1. You know or are familiar Unity3D 3.3.
2. You can code in C#.
3. You’re an ace at creating prefabs in Unity.
4. You’re using Unity3D 3.3 Pro and iOS Pro.

OK, if you’re still feeling good, let’s bounce into this how-to.

Create the Project

Open up a new project in Unity3D and set it up for “iPhone” iOS. Feel free to name it whatever passes your way.

In the project hierarchy, let’s go ahead and create a simple directory structure as follows:

/Example Project/
/Example Project/iphone/
/Example Project/iphone/objects/
/Example Project/iphone/materials/

Hit save after doing this and save your default scene and name it whatever you want.  In my case, I named it “test”.

Though, this is a near complete looking example (image to the right), the basic structure should look like the image to the right here.

Add at least one prefab to your project. I have added two prefabs here, “box” and “ABCD”. Both are just tests and contain a cube prefab with a simple texture applied. Make sure that the materials referenced by your prefabs have the following attributes. 1. They are named the same as the prefab. (See the image above!). 2. They are stored in the materials folder underneath the iphone folder.

UnityEditor Coding Time

When working on assetbundles, the first thing you have to do is generate them. Unfortunately, Unity3D does not have anything built in for generating these files except for a UnityEditor API. The UnityEditor script can only be run from inside the Unity3D editor tool. It typically appears as a menu item after the script has been successfully compiled. So if you can code, then you’re in good shape.

Let’s create the UnityEditor script. Create a new C-Sharp script in a /Plugins/ folder (create it if it doesn’t already exist) at the root of your project and name it MPCreateAssetBundle. Copy and paste the following into it. Please note: some of this code was borrowed from Unity’s own Character Customization tutorial on their website.

View Code CSHARP
using System;
using System.IO;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object=UnityEngine.Object;
public class MPCreateAssetBundle : MonoBehaviour {
	private static String[] mpAssetDir = new String[]{"iphone", "ipad", "macosx", "pc"};// , "universal"}; <-- not needed, use the /Resources/ folder.
	[MenuItem("Custom/Monkey Prism/Create All Assetbundles #&i")]
	public static void Execute() {
		Debug.Log("Creating Assetbundles");
		bool blnFound = false;
		String currentDir = null;
		foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)) {
			blnFound = true;
			if (o.name.Contains("@")) continue; //animations!
			String assetPath = AssetDatabase.GetAssetPath(o);
			if (!File.Exists(assetPath)) {
				currentDir = assetPath;
				continue; //Files only.
			//Only check those directories that we have specified in the mpAssetDir
			String currentBuildType = null;
			foreach (String s in mpAssetDir) {
				if (assetPath.Contains("/"+s+"/")) {
					currentBuildType = s;
			if (currentBuildType == null) continue; //if the directory is not found to be one from the mpAssetDir bail out.
			string assetBundleName = null, genericName = null;
			List<Object> toinclude = new List<Object>();
			//Generate pre-fabs for everything in the finished pre-fab directory.
			if (o.GetType() == typeof(GameObject)) {
				Debug.Log("GameObject " + currentDir);
				String d = CharacterRoot((GameObject)o);
				d += "materials/";
				List<Material> materials = CollectAll<Material>(d);
				Debug.Log("materials count=" + materials.Count);
				genericName = o.name.ToLower();
				assetBundleName = currentBuildType + "-prefab-" + genericName;
				//Package up the prefabs in the iPhone directory.
				//Do we need to add in a material?  I think so.
				foreach (Material m in materials) {
					Debug.Log("Material Name=" + m.name);
                    if (m.name.Contains(genericName)) {
						Debug.Log("Added a new material!");
				} //end foreach
			if (assetBundleName == null) continue;
			// Create a directory to store the generated assetbundles.
            if (!Directory.Exists(AssetbundlePath))
            // Delete existing assetbundles for current object
            string[] existingAssetbundles = Directory.GetFiles(AssetbundlePath);
            foreach (string bundle in existingAssetbundles) {
                if (bundle.EndsWith(".assetbundle") && bundle.Contains("/assetbundles/" + assetBundleName))
			//Directories expected.
			Debug.Log("currentBuildType = " + currentBuildType);
			//path = AssetbundlePath + bundleName + ".assetbundle";
			if (toinclude.Count > 0) {
				String path = AssetbundlePath + assetBundleName + ".assetbundle";
				if (currentBuildType.Equals(mpAssetDir[0]) || currentBuildType.Equals(mpAssetDir[1])) //iPhone & iPad
					BuildPipeline.BuildAssetBundle(null, toinclude.ToArray(), path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone);
				else //TODO: might need to condition further and might want to use an enum with the conditional.
					BuildPipeline.BuildAssetBundle(null, toinclude.ToArray(), path, BuildAssetBundleOptions.CollectDependencies);
		} //end foreach
		if (!blnFound) {
			Debug.Log("no objects were found for building assets with.");	
	public static string AssetbundlePath
        get { return "assetbundles" + Path.DirectorySeparatorChar; }
	// This method loads all files at a certain path and
    // returns a list of specific assets.
    public static List<T> CollectAll<T>(string path) where T : Object
        List<T> l = new List<T>();
        string[] files = Directory.GetFiles(path);
        foreach (string file in files)
            if (file.Contains(".meta")) continue;
            T asset = (T) AssetDatabase.LoadAssetAtPath(file, typeof(T));
            if (asset == null) throw new Exception("Asset is not " + typeof(T) + ": " + file);
        return l;
	// Returns the path to the directory that holds the specified FBX.
    static string CharacterRoot(GameObject character)
        string root = AssetDatabase.GetAssetPath(character);
        return root.Substring(0, root.LastIndexOf('/') + 1);

Once this is completed, you’ll want to do two things.
1. Select (by moving your mouse over and left-clicking on it once) the iphone folder under “Example Project”.
2. Run the script as shown in the image:

So let’s back up for a second and look at what we just did. We created a project, created some folders within the project, placed prefabs into the iphone folder and created a UnityEditor script. Upon running this script, a new directory named “assetbundles” would be created.

This directory is not going to be visible to you until you open finder (on Mac) or explorer (on Windows) as it is located on the file system. (see the image above). Inside this folder you should see your prefab(s) named now as: iphone-prefab-<my prefab name>.assetbundle. If you are seeing this, congratulations, you’ve now created your own assetbundles uniquely per prefab.

Generic GameObject and a Simple Script

If you’ve come this far, great work! We’re nearly done.
Let’s create a new GameObject under the Hierarchy.

Now we’re going to write a simple script to instantiate the assetbundles we assembled earlier. Create a new C-Sharp script in the /Plugins/ folder and name it “Example”. Copy and paste the following code into it:

View Code CSHARP
using System;
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
	private WWW www;
	private AssetBundleRequest gameObjectRequest;	
	// Use this for initialization
	IEnumerator Start () {
        //TODO: change the name of the file below!!!
		String strPath = "file://" + Application.dataPath + "/../assetbundles/iphone-prefab-box.assetbundle";
		www = new WWW(strPath);
		yield return www;
        /* TODO: change the name "Box" to match the name of your prefab that you compiled into the assetbundle on step 1. */
		AssetBundleRequest request = www.assetBundle.LoadAsync("Box", typeof(GameObject));
		yield return request;
		Instantiate(request.asset, new Vector3(3f, 0f, 0f), Quaternion.identity);
		Instantiate(request.asset, new Vector3(0f, 0f, 0f), Quaternion.identity);
		Instantiate(request.asset, new Vector3(-3f, 0f, 0f), Quaternion.identity);
        void Update() { }

NOTE: Make sure to change the TODO line in the code to match your file name! If you don’t do this, it might not work!

We’re nearly done! Drag your Example.cs script onto the GameObject you created earlier. Here is how my project looks:

Build Time

We’re now going to build this codebase so that you can run it on your iOS device for testing. I’m going to skip the painful steps of creating a code signing certificate and all that jazz. We’re instead going to focus on how to get the actual assetbundle onto your iOS device in an area that will be accessible to the “Application.dataPath” call in your code!

1. Build the Unity project for iOS.
2. XCode should automatically open.
3. Within XCode you are going to need to tell it about your assetbundles folder! Open up finder or explorer and locate the assetbundles folder that was generated by the UnityEditor script we discussed earlier.
4. Drag and drop the assetbundles folder into XCode at the root of the iOS project. (You should see a prompt as follows)

5. Click “Finish”.

This will then look something like this:

6. Build the code using XCode and run it on your iOS device.

If everything works properly, you should see one of the prefabs instantiate on your iOS screen. That is all there is to it! I think you’ll find that this isn’t as bad as one might imagine, but it does require some serious fishing around in code examples and heavily crawling the Unity forums. The only way I figured this out was by trial and error, heavy amounts of Debug.Log calls and 3 pots of coffee. I’m now going to go and grind my teeth on my level editor.

Edit: I have one of my original forum posts here as well regarding this issue. Unity3D Forum

, , , , , , ,


Unity3D Conditional Device Compilation By Scenes

Recently I posted at the Unity3D forum in regard to how one can create a single project and compile multiple device and platform builds within this context. My goal here is simply to reduce having multiple projects for a single Unity3d application.

On my second cup of coffee here and hope I make sense with my questions here.

After watching the fantastic Smuggle Truck design/build discussions here:
I was perplexed by how they created the iPad/iPhone/Mac OS conditional compilation build all from a single project. Only those assets referenced for that build are baked into the final project that is then sent to XCode.

I’m aware of the preprocessor flags (http://unity3d.com/support/documenta…mpilation.html). I also understand how to use them.

I guess the hurdle(s) I haven’t figured out yet are the following.

1. How does one condition the build between UNITY_IPHONE and UNITY_IPAD? Note: there isn’t any UNITY_IPAD define that I’m aware of. Within the Unity editor it is not possible to pass in custom preprocessor defines globally. They are scoped per-script. I see the command line option of “-executeMethod …” (http://unity3d.com/support/documenta…Arguments.html) Perhaps that is the magic sauce?

2. How did they conditionally include only unique assets to that particular build? For example, Mac OS X textures would be a higher resolution than those for the iPad and iPhone. Perhaps, this will be answered in question one.

Anyone have any ideas or other forum links that I may have missed?
Note, I have seen this: http://forum.unity3d.com/threads/171…al+Compilation

Well here is a basic solution that is created by scene. By scene means that you have to create a different scene for each device but give them the same name. As seen in the first picture here.

Let me just state for the record that this solution would work perfectly but it is less than ideal. I am posting this to merely explain how to conditionally gather assets into a final deployment as well as to isolate resources per build. I believe the ideal solution would be to use asset bundles. I will cover that in a new blog entry in a few days/weeks as I figure it out as well.

I am going to provide a script at the bottom of this article and a zip file containing the magic mixture to easily integrate this beast into your project. Integrating this script is easy. Simply uncompress the zip file and place the “MonkeyPrism” folder into the root of your project.

Once you have the C-Sharp editor object installed you should be able to run the basic build process using the menu system or shortcut keys as documented within the C-Sharp code.

Download the file here.

Everyone: This script requires Unity Pro and it is setup to build for (iPhone, iPad, Mac OS Universal and Windows).
PC Users: Take note that I used the “/” in my script.

View Code CSHARP
using System;
using UnityEditor;
using UnityEngine;
/// <summary>
/// A C# build object demonstrating a dynamic build process for Unity 3D.
/// This script requires you to organize your scenes by device and platform as follows:
///		SCENES_LOC / {iphone, ipad, macos, pc} / {your scenes}
///		i.e.	Assets/Scenes/iphone/scene0.unity
///				Assets/Scenes/iphone/scene1.unity
/// Please note: this is not perfect and honestly, I'm looking into an asset bundle
/// solution as well here.  I just wanted to figure out if there was a better solution.
/// Version 1.0
/// April 29, 2011
/// License:  Free to use and modify.
/// </summary>
public class MonkeyPrismBuild {
	//TODO: place your core scenes location here.  (relative to the already loaded project)
	private const string SCENES_LOC = "Assets/Scenes/";
	//TODO: place your scenes into this array.
	private static string[] myScenes = { 
		"home.unity" }; /*, "levelselect.unity", 
		"game.unity", "credits.unity" };*/
		iPhone = 0, iPad, MacOS, PC
	 * Make sure uniqueBuildNames maps with the enum above and take note of the GetScenesByBuildType method.
	 * Also: make sure that these names match with directories under your SCENES_LOC.
	private static string[] uniqueBuildNames = {"iphone","ipad","macos","pc"};
	[MenuItem ("Tools/Build/iPhone #&i")] //press shift+alt+i
	public static void PerformBuildIPHONE() {
		Debug.Log("Building iPhone version");
	[MenuItem ("Tools/Build/iPad #&p")] //press shift+alt+p
	public static void PerformBuildIPAD() {
		Debug.Log("Building iPad version");
	[MenuItem ("Tools/Build/Mac OS #&m")] //press shift+alt+m
	public static void PerformBuildMACOS() {
		Debug.Log("Building Universal Mac OS version");
	[MenuItem ("Tools/Build/PC #&c")] //press shift+alt+c
	public static void PerformBuildPC() {
		Debug.Log("Building Windows PC version");
	private static string[] GetScenesByBuildType(MP_BUILD_TYPE type, ref string buildDir) {
		int len = myScenes.Length;
		string[] scenes = new string[len];
		//string buildDir = null;
		switch (type) {
			case MP_BUILD_TYPE.iPad:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.iPad];
			case MP_BUILD_TYPE.iPhone:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.iPhone];
			case MP_BUILD_TYPE.MacOS:
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.MacOS];
				buildDir = uniqueBuildNames[(int)MP_BUILD_TYPE.PC];
		} //end switch;
		if (buildDir == null || buildDir.Length < 1) 
			throw new Exception("Invalid MP_BUILD_TYPE::BuildType Specified.  Please visit the MPBuild.cs script to resolve.");
		for (int i=0; i < len; i++) {
			scenes[i] = SCENES_LOC + buildDir + "/" + myScenes[i];
		return scenes;
	private static void RunBuild(MP_BUILD_TYPE type) {
		string buildDir = null;
		string[] scenes = GetScenesByBuildType(type, ref buildDir);
		switch (type) {
			case MP_BUILD_TYPE.iPad:
			case MP_BUILD_TYPE.iPhone:
				BuildPipeline.BuildPlayer(scenes, buildDir, BuildTarget.iPhone, BuildOptions.None);
			case MP_BUILD_TYPE.MacOS:
				BuildPipeline.BuildPlayer(scenes, buildDir, BuildTarget.StandaloneOSXUniversal, BuildOptions.None);
				BuildPipeline.BuildPlayer(scenes, buildDir, BuildTarget.StandaloneWindows, BuildOptions.None);
		} //end switch;


No Comments

Unity 3D JavaScript and C# Scoping/Compiling Issue

This is a quick write-up regarding something as simple as getting JavaScript to interoperate properly with C# scripts in your Unity projects.

I ran into such an issue after purchasing EZ Gui via the Unity Asset Store.  Immediately upon purchasing you are prompted to “import” the project into your codebase.  I did this without issue.  Everything worked fine and dandy until my JavaScript code required referencing a SpriteText object.  This is when the following issue began happening:

Unity BCE0018: The name ‘SpriteText’ does not denote a valid type (‘not found’).

My folder structure was simply the following:

/scripts/myCameraScript.js  <– trying to reference the SpriteText


var test : SpriteText;  //this line was throwing the error.


/EZ GUI/ <– default after purchasing and importing from the Unity Asset store.

I sat and read Unity compiling and scoping rules over and over located here.  On point 4, the statement reads “All scripts that are compiled in this step have access to all scripts in the first group (“Standard Assets”, “Pro Standard Assets” or “Plugins”).”

The solution is simple here. Take the EZ Gui code located in the “Plugins” folder and move it to the root.

/EZ Gui/Plugins/ <– move this to:


Done. It works. Super simple.

Bonus Play, Rule of thumb (kind of no brainers really):

  1. Place all your plugins in the /Plugins/ folder.
  2. Place all your C# Code into the /Standard Assets/ folder.
  3. Place all your JavaScript code into /Scripts/ or any folder that is NOT named (“Standard Assets”, “Pro Standard Assets” or “Plugins”)

This will lead to success and less headaches.

, , , , ,

No Comments