Unity-Ash Upgrades

Unity-Ash Upgrades

A while back I decided to scratch an itch and see if Richard Lord’s Ash Entity Framework could be ported to Unity. Well I was pleasantly surprised that it did port quite easily (with some help from David Arno’s .NET port) and worked well enough that I could also port Richard’s Asteroids game over to Unity too.

Since then I have had a few people contact me interested in using the port for their own projects so I decided to give it a little more love and polish.

Part 1 – Separation

First things first was to split the Ash library from the Asteroids example project so you dont have to include the entire Asteroids port in every single one of your games (madness). So now the Unity Ash project can be found here:

https://github.com/mikecann/Unity-Ash

..and the Asteroids project (which uses the Untity-Ash project) lives here:

https://github.com/mikecann/UnityAshteroids

Now all that you need to do to is to clone (or include as submodule) Unity-Ash into your game’s Assets folder and you should be good to go, simples!

Part 2 – GetComponents

Next up was performance. I knew that the performance of Unity-Ash was the biggest stumbling block and unfortunately it seemed like a fairly fundamental one thanks to some holes in the Unity API (more on that later). However, I knew there were a few low-hanging-performance-fruits I could pick that should speed up things a bit.

To experiment with things I setup a separate Unity project for the performance tests and included Unity-Ash from GitHub.

2014-12-10_09-30-51

The key performance bottleneck in Unity-Ash is the way the code checks for component additions and removals. Dynamic component addition and removal is key to how Ash functions and in AS3 because it was all custom code we could just fire an event when a component is added or removed from an Entity. Because Unity-Ash tries to piggyback on top of Unity’s existing Entity (GameObject) / Component architecture we dont actually have access to the GameObject source and thus cant trigger events when a component is added or removed.

The solution I came up with is to add a component to each GameObject which acts like the Entity. It is responsible for checking to see if components have been added or removed, to do this it gets a list of all the components in the GameObject each frame and compares against its internal cache to see if any have been added or removed. This solution works well, however you can probably see the performance hit involved with getting a list of components from Unity for each GameObject, each frame will likely be quite high.

Because this listing of components was so crucial to performance I decided to write some tests to see what is the fastest way of doing that:

public class GetComponentTests : MonoBehaviour
{
	public int iterations = 10000000;
	public int repeats = 3;

	public GameObject noComponents;
	public GameObject manyComponents;
	public List<Component> components;

	public void TestAll()
	{
		components = new List<Component>();
		TestHelpers.Execute(iterations, repeats, "Empty Function", TestEmpty);
		TestHelpers.Execute(iterations, repeats, "Get Components With No Components", NoComponentsGetComponents);
		TestHelpers.Execute(iterations, repeats, "Get Components With Many Components", ManyComponentsGetComponents);
		TestHelpers.Execute(iterations, repeats, "Get Components With No Components List", NoComponentsGetComponentsList);
		TestHelpers.Execute(iterations, repeats, "Get Components With Many Components List", ManyComponentsGetComponentsList);

		Debug.Log("components " + components.Count);
	}        

	public void TestEmpty()
	{
	}

	public void NoComponentsGetComponents()
	{
		noComponents.GetComponents<Component>();
	}

	public void ManyComponentsGetComponents()
	{
		manyComponents.GetComponents<Component>();
	}

	public void NoComponentsGetComponentsList()
	{
		noComponents.GetComponents<Component>(components);
	}

	public void ManyComponentsGetComponentsList()
	{
		manyComponents.GetComponents<Component>(components);
	}
}

I run each of these 10 million times then repeat 3 times and average:

1) “TestEmpty” is my control test test to get a sense of the cost of simply calling an empty function. That averaged to 0ms elapsed
2) “NoComponentsGetComponents” I test getting a list of components when the GameObject is empty (other than Transform), that averaged to 22ms.
3) “ManyComponentsGetComponents” I test getting a list of components when there are 20 components (plus Transform) to see what the cost of getting additional components would be, that averaged to 35ms.
4) “NoComponentsGetComponentsList” is the same as 2) except I avoid an internal memory allocation my passing in a List to populate the return with, that averaged to 5ms.
5) “ManyComponentsGetComponentsList” is the same as 3) except I pass in the List again, that averaged to 15ms.

So in summary, I learned its much faster if I pass in a list to GetComponents, which makes sense as I avoid the memory allocation associated with returning a new array each time. As a result I folded this performance improvement into Unity-Ash.

Part 3 – Unity vs Ash

These tests are all well and good but 10 million iterations of GetComponents in standalone is probably an unlikely use case in the standard game so in addition I wanted to test what the actual performance hit you would get if you used Unity-Ash in a game instead of the standard Unity way.

To test this I set up another scene that creates and updates 5,000 asteroids on the screen.

upout

I time how long they create then I count how many frames I get in 10 seconds of updating them. Each asteroid has a 2D rigid body and a very simple controller script that wraps the Asteroids position on the screen:

2014-12-10_10-15-54

public class AsteroidController : MonoBehaviour
{
	private Bounds bounds;

	void Awake()
	{
		var size = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
		bounds = new Bounds(Vector3.zero, new Vector3(size.x * 2, size.y * 2));
	}

	void Update()
	{
		if (transform.position.x < bounds.min.x)
		{
			transform.position = new Vector3(transform.position.x + bounds.size.x, transform.position.y, transform.position.z);
		}
		if (transform.position.x > bounds.max.x)
		{
			transform.position = new Vector3(transform.position.x - bounds.size.x, transform.position.y, transform.position.z);
		}
		if (transform.position.y < bounds.min.y)
		{
			transform.position = new Vector3(transform.position.x, transform.position.y + bounds.size.y, transform.position.z);
		}
		if (transform.position.y > bounds.max.y)
		{
			transform.position = new Vector3(transform.position.x, transform.position.y - bounds.size.y, transform.position.z);
		}
	}
}

This was the base test and it took about 440ms to create 5000 asteroids and processed 83 frames over 10 seconds (8.3FPS).

Now I had my base I decided to setup the same scenario but using Unity-Ash to drive the updates. So now the Asteroid looks like this:

2014-12-10_10-19-42

Notice that there is no controller for the Asteroid, now all the update logic happens in a system:

public class MovementSystem : SystemBase
{
	private Bounds bounds;
	private NodeList nodes;

	public MovementSystem()
	{
		var size = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
		bounds = new Bounds(Vector3.zero, new Vector3(size.x * 2, size.y * 2));
	}

	override public void AddToGame(IGame game)
	{
		nodes = game.GetNodeList<MovementNode>();
	}

	override public void Update(float time)
	{
		var cam = Camera.main;
		for (var node = (MovementNode)nodes.Head; node != null; node = (MovementNode)node.Next)
		{
			var transform = node.Transform;
			var rigidbody = node.Rigidbody;

			if (transform.position.x < bounds.min.x)
			{
				transform.position = new Vector3(transform.position.x + bounds.size.x, transform.position.y, transform.position.z);
			}
			if (transform.position.x > bounds.max.x)
			{
				transform.position = new Vector3(transform.position.x - bounds.size.x, transform.position.y, transform.position.z);
			}
			if (transform.position.y < bounds.min.y)
			{
				transform.position = new Vector3(transform.position.x, transform.position.y + bounds.size.y, transform.position.z);
			}
			if (transform.position.y > bounds.max.y)
			{
				transform.position = new Vector3(transform.position.x, transform.position.y - bounds.size.y, transform.position.z);
			}
		}
	}

	override public void RemoveFromGame(IGame game)
	{
		nodes = null;
	}
}

The update logic is functionally exactly the same as the AsteroidController except its now contained within a system.

This time it took 595ms to create the Asteroids and we processed 33 frames over 10 seconds (3.3FPS).

So immediately you can see there is a large performance hit in using Ash-Unity over regular Unity (8.3FPS compared to 3.3FPS). To confirm that it was the extra overhead of checking for new components on each asteroid rather than the internal updating mechanism of the Systems in Ash I decided to turn off checking for component updates..

2014-12-10_10-40-02

Sure enough the frame rate went back up (in fact higher!) than the Unity update rate of 8.3FPS to 8.6FPS.

So with this in mind I decided to add an option to the Entity component that lets you choose how often it checks for new components:

2014-12-10_10-42-02

The reasoning is that its actually quite rare that you change the components after the initial creation of the object (at least that’s what I found) and thus by reducing the number of times the Entity component checks for updates we should be able to increase the performance.

Sure enough, when set to “Every Other Frame” the framerate goes up to 4.5FPS then set to “Every 10 Frames” it goes up to 7.4 FPS. The final setting “If Component Count Changes” attempts to skip the iteration of the components by comparing the number of components against the previous frame, this results in 7.5 FPS.

So the short of the long is that you can get the same as or higher FPS using Unity-Ash so long as you know beforehand how often you are going to be added or removing components. If you have entities that is likely never to have components added or removed then set it to update “Never” and enjoy great FPS with the power of using Ash.

Just as an addendum to this, I have submitted a feature request to Unity to add in events to GameObject that we can listen to that are fired when components are added or removed, this would obviously remove 99.9% of these performance issues from Unity Ash so I would love your votes:

http://feedback.unity3d.com/suggestions/componentadded-slash-removed-events-on-gameobject

As a final step in the upgrade process I looked at what more radical changes we could make to the framework by taking advantage of Generics and some other features that C# gives us that were lacking in AS3, you can check those out towards the bottom of the readme: https://github.com/mikecann/Unity-Ash

Well that’s it for now, I hope you enjoy the improvements, please to let me know if you think its worth carrying on development, I would love to work on it some more but I have games to write!

Working with Parse.com in Unity – Part 3 – Tests, Typescript and Common Code

Working with Parse.com in Unity – Part 3 – Tests, Typescript and Common Code

This is part of a three-post series on working with Parse.com in Unity. For more info please see the other posts in the series:

Part 1 – Intro and App Structure
Part 2 – Services, Helpers and Looming
Part 3 – Tests, Typescript and Common Code

In the last post I covered how to use Parse.com in Unity itself, in this post I want to talk about to to go about writing backend code.

Note all the code talked about can be found in the Parse Unity Sample Project on Github.

Environment

I briefly talked about App structure in my first post. I like to use use Visual Studio with Typescript and C# for my Backend as they all play nicely together an produce a hassle free way of coding up the backend.

I like to have the Parse command line app running in develop mode (parse develop “Parse Unity Sample”) at the same time so I can see whats happening on the server when I call it, and it allows for very quick iterations:

2014-11-11_08-39-16

Typescript

I have talked a lot in the past about my love for Typescript and so I love to use it whenever I can. Parse lets you run Javascript code on the server so I use Typescript that compiles to Javascript. To get it to work I first create a Typescript project that has been setup with CommonJS as the module system:

2014-11-11_08-40-46

I then make sure all the code is contained withing the /cloud folder (so that the require() works):

2014-11-11_08-45-22

It works well, particularly when combined with my (not yet finished) Typescript definition for Parse which provides type safety for as much as possible:

2014-11-11_08-47-02

For example, the code that is run before a User is saved looks like:

// Force this TS file to become a module
export var x = 2;

Parse.Cloud.beforeSave("_User", (request, response) => {

    // Must have a player name
    if (request.object.get("playerName") == null || request.object.get("playerName") == "")
        return response.error("Must supply a player name when creating a new user");

    // Email and username must equal
    if (request.object.get("email") != request.object.get("username"))
        return response.error("Username and email address must be equal");

    // All done
    response.success();
});

Testing

Because there is no way to run Parse cloud code offline, all tests must run on code that runs on the Parse servers. At first this sounded really nasty to me and almost put me off using Parse all together but once I realised that I could just create another App for testing and structure my test in such a way that I could isolate each test, I grew to like it, I actually really enjoy writing these tests now.

I like to use NUnit with the Parse .NET SDK for the testing because it lets us use some more advanced C# features such as async / await which the Unity SDK hasn’t got access too, and more importantly it returns the server error messages (unlike Unity) which we can test against.

To get started just create a Test class in your Typescript project (surprisingly Typescript projects work well C# within them in Visual Studio) and start testing:

namespace ParseUnitySampleBackend.Tests
{
    [TestFixture]
    public class SaveUserTests
    {
        [SetUp]
        public async void Init()
        {
            TestingHelpers.InitParse();
            ParseUser.LogOut();
        }

        [Test]
        [ExpectedException(ExpectedMessage = "Must supply a player name when creating a new user")]
        public async void RequiresPlayerName()
        {
            var user = new GameUser()
            {
                Username = TestingHelpers.GetRandomUserEmail(),
                Password = "a"
            };
            await user.SignUpAsync();
        }
		
	...

I included some simple helpers that I like to use for testing which setup Parse before each test.

If you use Visual Studio’s Test Explorer with the parse command line you can get really good feedback on what is happening on the server:

2014-11-11_08-59-15

Common Code

Because I write my tests in C# and my Unity code is in C# I would like to share my common code between the two projects. Unfortunately simply splitting the project out into a library project then including it as a reference in the testing project doesn’t work because Unity requires a different compiler (Unity 3.5 subset on mono) and thus when you try to add that as a reference you get errors related to invalid assemblies:

2014-11-11_09-03-05

The solution I found is to use a little known trick of linking source. To do this select the “Models” folder from the common project and while holding Control and Shift drag it into the Backend project, you should note that the cursor changes to a little shortcut icon and when in the backend project the file icons now have a shortcut icon to indicate they are linked:

2014-11-11_09-05-26

This means that files are linked to the Common project so they are included in compilation and any changes you make to those files in either the Common project or Backend project will be reflected in the other.

This is perfect as it now lets us compile the same files using a different compiler and version of Parse.

Conclusion

Well thats it for my three part post on how to get started using Parse in Unity. I hope it helps some people, please do leave me a comment or email me: mike.cann@gmail.com if you have any questions.

Happy coding!

Working with Parse.com in Unity – Part 2 – Services, Helpers and Looming

Working with Parse.com in Unity – Part 2 – Services, Helpers and Looming

This is part of a three-post series on working with Parse.com in Unity. For more info please see the other posts in the series:

Part 1 – Intro and App Structure
Part 2 – Services, Helpers and Looming
Part 3 – Tests, Typescript and Common Code

In the last post I covered the basic app structure, in this post I want to cover some of the specifics of the front end, the Unity part.

Note all the code talked about can be found in the Parse Unity Sample Project on Github.

Services

I like to split my code up so that all the work that involves dealing with remote services are contained within “Service” classes. The service classes are responsible for taking game data and turning it into a form that the remote service can deal with, then translating the result and passing it back to the game.

In the sample project I have just one service, the UserService, which is responsible for logging a user in or signing them up or logging them out, basically all functionality that that concerns the “GameUser” class.

Tasks

Lets take a look at the Login method in the UserService class:

public Task<GameUser> Login(string email, string password)
{
	Debug.Log("Logging in..");

	return ParseUser.LogInAsync(email, password)
		.OnMainThread()
		.Then(t => Task.FromResult((GameUser)t.Result));
}

We can see that it takes in an email and password then logs in for us and returns a Task.

Parse uses Tasks throughout its Unity SDK as a way of handling asynchronicity. You will probably familiar with Tasks if you have done any work with aync / await in C# 5 or greater.

Unity runs on an old version of C# it doesn’t have any concept of Tasks baked in so Parse includes the System.Threading.Tasks namespace. Although Unity does not have any support for async / await you can still use the Tasks API in a Javascript-like Promise way.

Tasks allow us to provide a callback method that will be called when the Task returns, which allows the game to continue on running while we wait for the server to respond. For example, this is how we call Login:

private void Login()
{
	isLoading = true;
	userService.Login(usernameInp.text, passwordInp.text)
		.Then(OnLoggedIn, OnLoginError);                
}

private void OnLoginError(Exception e)
{
	isLoading = false;
	menus.ErrorPopup.Open("Error logging in!");
}

private void OnLoggedIn(GameUser user)
{
	isLoading = false;        
	menus.States.SetState("Logged In State");
}

The Then function after login is being called on the Task and is the first of my helper libraries that I have included in the sample project which imports my Unity Parse Helpers library.

In that library I have included a number of extension methods for Task that allow you to chain functions together for example from the SignupState:

private void Signup()
{
	isLoading = true;

	// Signup
	userService.Signup(usernameInp.text, passwordInp.text, playernameInp.text)

		// Then Login
		.Then(t => userService.Login(usernameInp.text, passwordInp.text))

		// Then we are done
		.Then(OnLoggedIn, OnError);                     
}

This makes chaining asynchronous logic together in Unity a breeze and you don’t ever have to deal with the headache of using yeild!

Threading

The first major headache I came across when using Parse in Unity is to do with threading. Note from my earlier example of Login in UserService:

public Task<GameUser> Login(string email, string password)
{
	Debug.Log("Logging in..");

	return ParseUser.LogInAsync(email, password)
		.OnMainThread()
		.Then(t => Task.FromResult((GameUser)t.Result));
}

The function “OnMainThread” is a helper extension I wrote to deal with the threading issue. If we were to remove that function then when we try to login Unity would throw an error something like:

2014-11-11_08-07-43

This is because below the covers Parse is making web calls on a separate thread and when it returns the result is passed back to us without returning to be main thread and pretty much everything in Unity needs to happen on the main thread so when we try to access a variable that lives on the main thread Unity crashes.

There are a few ways around this issue, Parse documents that you can use coroutines to deal with this, but I would really rather not have to use coroutines so instead I used another common solution, a “Loomer”.

A Loomer basically just takes a function and waits until the game is running on the main thread before executing the function. So behind the scenes my “OnMainThread” extension function is calling the loomer which ensures that any Tasks that follow it will execute on the main thread:

public static Task<T> OnMainThread<T>(this Task<T> task)
{ 
	var tcs = new TaskCompletionSource<T>();
	var loom = Loom.Instance;

	Action<Task<T>> a = t =>
	{
		loom.QueueOnMainThread(() =>
		{
			if (t.IsFaulted) tcs.SetException(t.Exception);
			else if (t.IsCanceled) tcs.SetCanceled();
			else { tcs.SetResult(t.Result); }
		});    
	};

	task.ContinueWith(a);
	return tcs.Task;
}

So whenever you call Parse functions in your services just make sure you call “OnMainThread()” and you should be good to go!

On a separate note, I have already previously mentioned that if you are going to write extension methods for Tasks you must make sure that you sepparate your action from the ContinueWith() else you will get an internal compiler error in Unity!

Errors

Unfortunately there is an issue in Unity with Parse that means that we cannot get proper error messages back from the server. For example if a user mistypes their email address when logging in all we get is a HTTP error code like “404 – Not found”, this is obviously not particularly helpful. Parse does actually return a better error than which can be seen if you use the Rest or .Net API but whatever reason in Unity that error message gets lost.

This is probably the single biggest drawback with using Parse.com with Unity but it is apparently a fundemental issue with Unity. I have started a thread on the Parse Google Group to see if we can get an update on this but im not holding any hope. I did hear a rumor that the new version of the Unity compiler (shipping in Unity 5+) will finally include improvements to network stack.

Generally you can work around it as you can guess what the issue is if an error occurs and handle it in your business logic. Its still really annoying however particularly when it comes to debugging and testing, hence why I have devoted another post to that very subject..

To Be Continued…

And thats basically it for dealing with Parse.com in Unity, I strongly reccomend checking out the source if you need more information on anything I have covered, hopefully it makes more sense.

In the next part ill talk about how to structure your backend so that you can easily test your cloud code.