Unity Helpers – Utilities and Extensions for Unity

Unity Helpers – Utilities and Extensions for Unity

During the development of my up and coming game I have encountered some snags when developing in Unity so I wrote a number of utilities and extension methods to help out.

One such annoyance is the inability to use interfaces in GetComponent() and GetComponents(), so I wrote some extension methods to help with that:

using UnityHelpers;

var obj = new GameObject();
obj.AddComponent<MyComponent>();

obj.Has<MyComponent>(); // Returns true or false
obj.Has<IMyComponent>(); // Can also handle interfaces

obj.Get<MyComponent>(); // Returns the first component
obj.Get<IMyComponent>(); // Returns the first component that implements the interface

obj.GetAll<MyComponent>(); // Gets all the components
obj.GetAll<IMyComponent>(); // Gets all the components that implement the interface

Another utility is for adding children to GameObjects:

using UnityHelpers;

var obj = new GameObject();

obj.AddChild("Mike"); // Creates a new GameObject named "Mike" and adds it as a child

var player = obj.AddChild<Player>("Dave"); // Creates a new GameObject named "Dave" and adds the component "Player" returning it

obj.AddChild(typeof(Player), typeof(Friendly), typeof(AI)); // Creates a new GameObject and adds a number of components

There are many other utils and extensions, and more to come.

Checkout the source for more info: https://github.com/mikecann/Unity-Helpers/tree/master/Scripts

I have rigorously unit and battle tested these utils and added them to Github so I can use them in furthur projects. I hope they can be of some use to others too!

Fixing Unity’s Internal Compiler Error

Fixing Unity’s Internal Compiler Error

As mentioned in my last post, I am working on a Unity game that takes advantage of Parse for Asyncronous multiplayer. Well one nice feature of parse is that it uses Tasks to handle its asynchronicity.

Tasks are very much like JS promises (except they are type-safe) and return when the operation has completed. For example:

obj.SaveAsync().ContinueWith(task =>
{
    if (task.IsCanceled)
    {
        // the save was cancelled.
    }
    else if (task.IsFaulted)
    {
        AggregateException exception = task.Exception;
    }
    else
    {
        // the object was saved successfully.
    }
});

You can chain tasks together like so:

var query = new ParseQuery<ParseObject>("Student")
    .OrderByDescending("gpa");
 
query.FindAsync().ContinueWith(t =>
{
    var students = t.Result;
    IEnumerator<ParseObject> enumerator = students.GetEnumerator();
    enumerator.MoveNext();
    var student = enumerator.Current;
    student["valedictorian"] = true;
    return student.SaveAsync();
}).Unwrap().ContinueWith(t =>
{
    return query.FindAsync();
}).Unwrap().ContinueWith(t =>
{
    var students = t.Result;
    IEnumerator<ParseObject> enumerator = students.GetEnumerator();
    enumerator.MoveNext();
    enumerator.MoveNext();
    var student = enumerator.Current;
    student["salutatorian"] = true;
    return student.SaveAsync();
}).Unwrap().ContinueWith(t =>
{
    // Everything is done!
});

The problem is that if you want to catch errors you must manually check the task return for errors inside each handler. This seemed wasteful to me as all I wanted was a global error handler for that particular task chain, such as can be achieved with Javascript Promises.

Fortunately someone else also had noticed this problem and solved it:

Task.Factory.StartNew(StartBusyIndicator)
	.Then(task => GetWebResponseAsync(url))
	.Then(task => Console.WriteLine(task.Result.Headers))
	.Finally(ExceptionHandler, StopBusyIndicator);

The only problem is that when I tried to implement his C# library Unity started throwing the dreaded Internal Compiler Error:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.ArgumentNullException: Argument cannot be null.

It took me a while to work out what was going on. I managed to simplify the entire problem down to this simple example:

public static class TaskHelpers
{
	public static void Then<TIn>(this Task<TIn> task, Action<Task<TIn>> next)
	{
		task.ContinueWith(t =>
		{		
		});    
	}
}

I posted about this on the Unity forum on Parse’s Forum and even on Parse’s bug tracking system but no one was interested, Parse even told me its not a Parse issue.

It took me quite a while to work out what was going on, but I eventually worked out that if I separated the handler from the Continue With call it would work:

public static class TaskHelpers
{
	public static void Continue<TIn>(this Task<TIn> task, Action<Task<TIn>> next)
	{
		Action<Task> a = t => { };
		task.ContinueWith(a);    
	}
}

Huzzah! It compiles. So I guess this is a lesson learnt. With the Unity Mono compiler, if you are getting Internal Compiler errors then perhaps try separating out the lambdas into variables.

Parse.com Type-Safe Extensions for Unity

Parse.com Type-Safe Extensions for Unity

As mentioned in my previous post I have been working on a multiplayer game built in Unity for a little while now. I ummed and ahhed over the technology choice for the backend for a little while before deciding to go with Parse.com‘s library. The reason being that it looked simple to implement (they have a Unity SDK), they take care of all the backend headaches for me and the pricing model looked fair.

The Parse Unity library is okay, not as fully fleshed as some of the other languages they support but good enough for what I needed apart from one critical point, the lack of type-safety in the queries such as:

new ParseQuery<Armor>().WhereLessThanOrEqualTo("cost", 13);

This annoyed me so much that I decided to write some extension methods that take advantage of Lambda functions to provide the type-safety. The above now becomes:

new ParseQuery<Armor>().WhereLessThanOrEqualTo(a => a.Cost, 13);

The library can also handle chains such as

new ParseQuery<Player>().Include(p => p.Stats.Heath.Remaining); // becomes "stats.health.remaining"

The library also handles interfaces by introducing a new attribute “ParseFieldType”.

[ParseClassName("Father")]
public class Father : ParseObject, IFather
{
	[ParseFieldName("daughter")]
	[ParseFieldType(typeof(Child))]
	public IChild Daughter { get; set; }
}

new ParseQuery<Father>().Include(f => f.Daughter.Name); // becomes "daughter.name" and works because ParseFieldType redirects the chain to Child rather than IChild

It can even handle lists

[ParseClassName("Father")]
public class Father : ParseObject, IFather
{
	[ParseFieldName("children")]
	[ParseFieldType(typeof(Child))]
	public List<IChild> Children { get; set; }
}

new ParseQuery<Father>().Include(f => f.Children[0].Name); // becomes "children.name"

To get started go grab the .dll from my Github project for the library: https://github.com/mikecann/Unity-Parse-Helpers