Search
Twitter Feed
Navigation

Entries in C# (6)

Wednesday
Dec052012

A genuine passion for C# .Net development

My latest contract dev contract is coming to an end so I've been looking around for something for next year. The recruitment company that does my payroll stuff mentioned that there was some potential work going at a company and sent me their 'Developer Questionnaire' to fill out.

It's amazing how long these end up taking, but I thought I'd share part of my answer to the following question.

Q: [Give details/examples etc of] A genuine passion for C#.Net development.

A: My wife has been using an iPhone app called Baby Tracker to keep track of my newborn son’s activities. Unfortunately the app has limited display options and no export functionality. I worked out that the app uses an unencrypted SQLite database to store its data. I am currently writing a .Net 4 WPF app using MVVM to display the domain data extracted using Fluent NHibernate in Visual Studio 2012.

Google tells me that lots of other people are trying to get data out of this app. If it turns out to be any good I'll OSS it and see if anyone wants to use it.


 

Saturday
Jun182011

MonoBehaviour Magic – using C# Namespaces and Inheritance in Unity

A discussion on Unity's workhorse class, MonoBehaviour and ways to use it in C#.

 

What is MonoBehaviour?

 

MonoBehaviour is the class from which all scripts attached to objects that exist in a scene derive from. What seems strange to many seasoned (non-Unity) programmers when they first encounter this beast is that all its methods seem to be missing. MonoBehaviour inherits from Behaviour which inherits from Component then from Object. None of these have the methods we're used to seeing, namely Awake(), Start() and OnGUI().

 

These are magic methods, summoned directly from the dark Unity overlord. Or, more likely, via reflection. Unity checks if your class contains any of these methods and will call them when appropriate. In the interests of clarity, from now on I'll call these methods 'Overridable Methods' as the Unity documentation does.

 

So back to the thing that irks purist programmers; Unity is using reflection here, so it skips the usual processes used to allow or exclude other classes from accessing methods. As you can see on the default class created by Unity.

 

    void Awake()
    {
    }

 

This methods is private, so nothing outside the class should be able to call it. But obviously stuff can otherwise nothing would work.

 

One way to think of it is the following.

 

public class MonoBehaviour
{
    public virtual void Awake()
    {
    }
}

public class MyMonoBehaviour : MonoBehaviour
{
    public override void Awake()
    {
    }
}

 

Then imagine the Unity Engine doing something like this

 

foreach (MonoBehaviour baseClass in listOfAllMonobahaviours)
{
     baseClass.Awake();
}

 

If you start of just using the default Unity created classes, it may take you a while to realise how many MonoBehaviour overrideable methods there actually are. At latest count there are 49 'Overridable Functions' that Unity uses. A full list of them is here

 

Now, don't let the fact that Unity magics up these methods for you discourage you from programming in a traditional manner. That is, you can still utilise inheritance in the manner that you might like to. The thing to remember is that the Unity engine is the only thing that will call these methods in an improper way, everything we do manually will work as expected.

 

For example, the classic 'new' vs 'override' issue.

 

public class BaseClass
{
    public void DoSomething()
    {
    }
}

public class DerivedClass : BaseClass
{
    public new void DoSomething()
    {
    }
}

 

Which method gets called depends on what the type is when we call it. I.e.

 

        DerivedClass newClass = new DerivedClass();

        newClass.DoSomething();
        (newClass as BaseClass).DoSomething(); 

 

...will call different methods. But in Unity, it doesn't matter what modifiers you add to your MonoBehaviour overridable methods, Unity will treat them as if you used

 

    public override void Awake()
    {
    }

 

...and call the Awake() method of the highest derived class.

 

Manually though, it all works as expected. You can hide and override to your heart's content for these overridable methods, but just accept that whatever you do, Unity doesn't care and will still treat method as having been declared as an override (even if it's private). And note that you can manually call all these methods, and if you do they will be treated exactly as you would expect in non-Unity programming. But it's probably best not to for the sake of clarity.

 

An exception to that rule however might be if you have a derived class and you want to call an overridable method in the base class. I.e. In the Awake() method of the derived class you manually call base.Awake(). Since the base class's implementation of Awake will never be call by Unity (since it will always be overridden) this seems ok.

 

Execution Order

 

However, it's worth noting that the execution order of the overridable methods is random. You cannot guarantee the order in which they are called. If this is a problem for you, I'd recommend just creating your own update method, MyUpdate() and then have a controlling class call them manually.

 

 

public class MyMonoBehaviour : MonoBehaviour
{
    public void MyUpdate()
    {
    }
}


public class MyController : MonoBehaviour
{
    void Update()
    {
        foreach (MyMonoBehaviour myMonoBehaviour in this.listOfAllMyMonobehaviour)
        {
            myMonoBehaviour.MyUpdate();
        }
    }
}

 

Namespaces and Inheritance

 

One of the other things that traditional programmers might notice is the lack of namespaces. Well, that's because Unity likes to pretend that it doesn't use namespaces, but it can.

 

The basic rule is that you can't put any MonoBehaviour derived class inside an explicit namespace, but that shouldn't stop you from using them completely.

 

Now if you do put a MonoBehaviour script inside a namespace you'll get this compiler warning. “The class defined in script file named 'MyScript' does not match the file name!” And none of the MonoBehaviour overridable methods (Start, Update) will be called because Unity will treat it as a normal class.

 

So what, you still shouldn't use namespaces right? Well, the documentation say no, but they are just so useful that I reckon you should anyway.

 

Having to give everything a unique class name for a project of any size is annoying to say the least. Now, for the MonoBehaviour classes we're screwed, but for everything else, go nuts. Now I tend to section off as much common code as I can into static libraries or helper classes, on top of using inheritance to concentrate common code in base classes.

 

For a simple example.

 

Create separate Constant classes for each level. Note: You can stick all these into the one file if you want. The requirements for Class name to match File name is only for MonoBehaviour classes.

 

namespace Level01 
{
	public class Constants
	{
		public const float Speed = 0.1f;
	}
}

namespace Level02
{
	public class Constants
	{
		public const float Speed = 0.3f;
	}
}

 

Create a Player base class and an abstract member (so it can be used within the class).

 

public abstract class PlayerBase : MonoBehaviour
{
	// Common code for Player

	public abstract float Speed {get;}

	void Update()
    	{
       		this.transform.position += new Vector3(0.0f, 0.0f, this.Speed * Time.deltaTime);
    	}
}

 

Then we create separate Player classes specific to each level. Each will import the required namespace and thus get access to the Constants for that level..

 

using Level01;

public class Level01Player : PlayerBase
{
	// Level 01 specific code for Player

	public override float Speed
	{
		get {return Constants.Speed;}
	}
}


using Level02;

public class Level02Player : PlayerBase
{
    // Level 01 specific code for Player

    public override float Speed
    {
        get { return Constants.Speed; }
    }
}

 

Note: Each of these is in a separate file.

 

Maybe a bit of a contrived example, but I'm just trying to show that you shouldn't throw away the traditional programming tools when you start working with Unity.

 

Note that I didn't have to make the base class abstract. Another option would have been to make the Speed property virtual and have the derived classes override it. But since we have no intention of actually using PlayerBase on a Unity object directly it makes sense to ensure it can never be used (Unity will spit it if you attach an abstract class to an object).

 

Performance

 

Finally a little word on performance. After I realised that I didn't have to use MonoBehaviours for everything, I started wondering about their performance overhead. Perhaps I should be avoiding them at all cost? Well it turns out that whatever overhead there is is almost negligible, at least according to my quick and dirty testing.

 

I created a scene with 1000 objects with a MonoBehaviour script on them and a task to be performed in their Update() methods, then another scene with only one MonoBehaviour class and an Update() method that did the same task 1000 time. I ran them both and measure how many times Update was called. The result? Pretty much the same. Now I'm not conclusively saying that there is no performance gain to be had by minimising MonoBehaviours, but in my opinion there are hundreds of performance tweaks you should look at first before considering it.

Thursday
Mar312011

Using protobuf-net serialization in Unity iPhone

If performance is an issue for your serialization in a Unity iPhone project, this post shows you how to use the protobuf-net library.

Some Background

As part of a new game project I'm working on, I had to store level information in a file and read it in for each new level. I had created a simple level editor as a windows WPF C# project and then I would serialize my level model class onto the file system then include it in my Unity project to be loaded in during runtime.

I initially used simple XML serialization, but as the levels increased in complexity it was taking longer and longer (well, 100s of miliseconds, but every bit counts ;-) to deserialize. So, off to the web to find the fastest and easiest serialization. The best candidate was Marc Gravell's protobuf-net, which provides 'Fast, portable, binary serialization for .Net' using Google's protocol buffers technology. And he's not kidding on the 'Fast' part either, see the performance stats here.

So I grabbed the library, chuck it into Unity and start testing. It worked in Unity Windows, worked in Unity Mac but when I deployed it to my iPhone I hit this:

ExecutionEngineException: Attempting to JIT compile method

Now, I'm no expert on the inner working of .Net and it's relationship with iOS running Mono, but according to this thread on the Unity forums, the culprit is the JIT (Just in Time) compilation of classes that have not been seen by the system before. Mono on iOS is an AOT (Ahead of Time) only system, so that's why it craps out. I'm sure smarter people than me could provide a better explanation, but that will do for now.

So, I emailed Marc Gravell for help because he mentioned in this thread that version 2 of protobuf-net would have a 'pre-compile to dll' option, meaning that the serializer/deserializer classes can be pre-made in a dll instead of on the fly (and JIT'ed). I think that's how it works.

Anyway, he sent me an alpha version of a Unity iPhone friendly protobuf-net and that's the one that works. Woohoo.

I also have to say that this is all info that Marc sent me so all credit goes to him here. I wouldn't know a protocol buffer if it came up and bit me in the arse to be honest.

The Solution

First up, download the alpha version of the protobuf-net libraries. The link Marc sent me is this one but go check out the site to see if there is a later version.

This contains two libraries, the 'Light Framework' and 'Full Framework' versions of protobuf-net. The basic procedure is this:

  • Create a library dll (assembly) of the model classes you want to serialize/deserialize. That is, just create a new 'Class Library' Visual Studio project and have it contain only your model. (I don't use MonoDevelop but I'm sure it's a similar process). You will have to reference the 'Light Framework' dll in this project in order to use the  [ProtoContract]/[ProtoMember] attributes, described in the Getting Started guide. Build this project to produce your MyModel.dll.
namespace ProtoTest
{
    // Simple model classes, with some inheritence and generics thrown in. 
    [ProtoContract]
    public class MyModel
    {
        [ProtoMember(1)]
        public int int1 { get; set; }
        [ProtoMember(2)]
        public TestEnum enum1 { get; set; }

        public List intList { get; set; }
        [ProtoMember(3)]
        public List floatList { get; set; }
        [ProtoMember(4)]
        public List stringList { get; set; }

        [ProtoMember(5)]
        public List anotherClassList { get; set; }
    }

    [ProtoContract, ProtoInclude(10, typeof(DerivedClass))]
    public class AnotherClass
    {
        [ProtoMember(1)]
        public string string1 { get; set; }
    }

    [ProtoContract]
    public class DerivedClass : AnotherClass
    {
        [ProtoMember(1)]
        public float float1 { get; set; }
    }

    public enum TestEnum
    {
        run,
        walk,
        skip
    }
}

    Note: Make sure your model project is set to .Net 2.0 in the project properties, otherwise Unity will throw up the following error:

    Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'MyModel'
  • Next you need to create the serilization/deserialization classes. So create a new 'Console Application' Visual Studio project. Now, for this one you need to reference the Full Framework protobuf-net library as well as obviously your newly created MyModel.dll assembly. 
  • Now you need the code to create the libraries.
var model = TypeModel.Create();

model.Add(typeof(AnotherClass), true);
// Note: you don't need to add DerivedClass here, in fact it craps out if you do. 
model.Add(typeof(TestEnum), true); 
model.Add(typeof(MyModel), true);
model.Compile("MySerializer", "MySerializer.dll");
  • This will output 'MySerializer.dll' 
  • Now we have our serialization library that we can use in our Unity project. So now you have to add three assemblies to your Unity iPhone project:
    • MyModel.dll
    • MySerializer.dll
    • Protobuf-net.dll (Light Framework)

And we're good to go.

To serialize the files in my external application, I used the following code. I haven't played around with writing files to the iOS file system so I won't post that code, but I'm sure it's similar once you get the paths correct.

MyModel myNewModel = new MyModel();

MySerializer mySerializer = new MySerializer();

using(var file = File.Create("TestFile001.bytes"))
{
    mySerializer.Serialize(file, myNewModel);
}

In my case I was creating my game level files in an external application, so having the libraries external was actually more convenient. Once I had run my level editor app and created the binary serialized output files, I figured the easiest way to load them in Unity was via TextAsset class. TextAsset can be used to load files from the Resources folder just like any other resource, and despite the name, it is also fine for binary files. 

Note: From the Unity docs on TextAsset

If you're using the text asset to contain binary data, you should make sure the file has the .bytes extension. For any other of the extensions the TextImporter will try to strip nonascii characters if it is unable to parse the file as an utf8 string.

So inside our Unity project scripts, to read in the binary file we just use this.

TextAsset textFile = Resources.Load("TestFile001") as TextAsset;

MySerializer mySerializer = new MySerializer();

MyModel readInMyModel;

using (System.IO.Stream s = new System.IO.MemoryStream(textFile.bytes))
{
    readInMyModel = mySerializer.Deserialize(s, null, typeof(MyModel)) as MyModel;
}

I had a look via Reflector and the second parameter to Deserialize() there is used in case your type variable is null, so I assume you can use either one.

And there you have it. A bit more work than just using a library directly, but if performance is an issue then it is well worth the effort. I haven't done proper metrics yet, but from a quick look it seems at least an order of magnitude faster than the XmlSerializer I was using before.

 

Edit: In response to the comment below about not being able to use Vector3.

    [ProtoContract]
    public class MyVector3
    {
        [ProtoMember(1)]
        public float x { get; set; }

        [ProtoMember(2)]
        public float y { get; set; }

        [ProtoMember(3)]
        public float z { get; set; }

        public MyVector3()
        {
            this.x = 0.0f;
            this.y = 0.0f;
            this.z = 0.0f;
        }

        public MyVector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public static implicit operator Vector3(MyVector3 v) 
        {
            return new Vector3(v.x, v.y, v.z);
        }

        public static implicit operator MyVector3(Vector3 v)
        {
            return new MyVector3(v.x, v.y, v.z);
        }
    }
Tuesday
Mar222011

Unity and accessing common MonoBehaviour manager classes

I've started a few Unity codebases over the last couple of years and one of the first things you want to do is work out how to access your MonoBehaviour 'Manager' classes attached to gameobjects from other scripts.

Now, the manager-type scripts that are attached to our Unity GameObjects are essentially Singletons. I usually call them 'MonoBehaviour Singletons' because they usually exist just once and need to be accessed from all over the place. I'm talking here about things like a class that manages all the playing of sound for a particular scene. You create a script called, for example, SoundManager and attach it to a GameObject in the Unity Editor and it is the single point of sound management throughout your scene. What I'm talking about here is how to access that script from wherever you need it.

So here is our little SoundManager class that we want to access. It just has a single method, PlayClickSound().

public class SoundManager : MonoBehaviour
{
    public void PlayClickSound()
    {
    }
}

Direct Access

Now of course, the 'easiest' way to access that script is via a call to GameObject.FindObjectOfType or, if you have several classes of the same type, GameObject.FindWithTag where you have 'tagged' each gameobject differently.

(GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager).PlayClickSound();
// or
(GameObject.FindWithTag("Sounds").GetComponent(typeof(SoundManager)) as SoundManager).PlayClickSound();

But, calls to these methods are expensive and you absolutely do not want to be calling them often.

Keep a Reference

The first solution that is in the documentation is to call those expensive methods once in the Awake or Start method of each script that needs them, then store the reference for all further calls.

public class TestClass : MonoBehaviour
{
    SoundManager mySoundManager;
    bool needToPlaySound;

    void Awake()
    {
        this.mySoundManager = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
    }

    void Update()
    {
        if (this.needToPlaySound)
        {
            this.mySoundManager.PlayClickSound();
        }
    }
}

From a performance perspective, that's pretty much all you'd need to do (unless for example you are doing this from a 'bullet' script and creating many bullets a second) and I'm sure you all know that, but I'm looking at it from a maintainability and ease-of-use perspective. With even a small project, the code above is going to be copied many times.

Singleton Manager

In my opinion, the lesser the code the better. So, what's next? Create a static class that holds this code that we can access from anywhere. Since this class is controlling our access to the MonoBehaviour 'Singleton' manager classes, I've called it the SingletonManager

public class SingletonManager
{
    public static SoundManager GetSoundManager()
    {
        return GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
    }
}

Then in our classes we can access it like this:

public class TestClass : MonoBehaviour
{
    SoundManager mySoundManager;
    bool needToPlaySound;

    void Awake()
    {
        this.mySoundManager = SingletonManager.GetSoundManager();
    }

    void Update()
    {
        if (this.needToPlaySound)
        {
            this.mySoundManager.PlayClickSound();
        }
    }
}

This is better in my option, but still not perfect. There is still a lot of code repeated in each of our accessing classes. The less code in our classes, the easier it is to see what is happening and the easier the maintain the code.

Singleton Singleton Manager

So, another option is to make our SingletonManager a classic singleton and have it store the references to all the MonoBehaviour singletons.

public class SingletonManager
{
    private static SingletonManager instance;
    public static SingletonManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new SingletonManager();
            }
            return instance;
        }
    }

    private SoundManager soundManager;
    public SoundManager SoundManager
    {
        get
        {
            if (this.soundManager == null)
            {
                this.soundManager = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
            }
            return this.soundManager;
        }
    }
}

Then we can access it like this:

public class TestClass : MonoBehaviour
{
    bool needToPlaySound;
    
    void Update()
    {
        if (this.needToPlaySound)
        {
            SingletonManager.Instance.SoundManager.PlayClickSound();
        }
    }
}

Note: I'm not touching on thread-safety here for simplicity but it shouldn't matter to most people. If you're doing anything with threads you'd better Google "threadsafe singleton"

This way we have concentrated the code accessing the MonoBehaviour singletons in one place. The Awake method can be used for more essential code needed there, and in many cases can be removed all together.

Interfaces - Use 'em

Now one final problem is that these MonoBehaviour singletons we are exposing with the SingletonManager are (obviously) extending the MonoBehaviour class, which is a huge class with many methods on it. This means that you have way too much access to that class and its attached GameObject that you need.

For example, you can type SingletonManager.Instance.SoundManager.transform.rotation = ... and suddenly you're able to change the rotation of the SoundManager's GameObject from anywhere in the code. From a good coding viewpoint this is not a good thing. The SoundManager should only let external classes have access to a very limited number of methods. That's where interfaces can save you.

So, instead of having the SingletonManager expose the SoundManager class directly, we create an interface ISoundManager and expose that.

public interface ISoundManager
{
    void PlayClickSound();
}

Then our SoundManager will use the interface like this:

public class SoundManager : MonoBehaviour, ISoundManager
{
    public void PlayClickSound()
    {
    }
}

And the SingletonManager will look like this:

public class SingletonManager
{
    private static SingletonManager instance;
    public static SingletonManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new SingletonManager();
            }
            return instance;
        }
    }

    private SoundManager soundManager;
    public ISoundManager SoundManager
    {
        get
        {
            if (this.soundManager == null)
            {
                this.soundManager = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
            }
            return this.soundManager;
        }
    }
}

Much better. Now, when something screwy is happening to our SoundManager, we know exactly what the external classes are capable of doing to it. Plus it essentially forces the coder to think about what relationship our MonoBehaviour singleton classes have with each other. If you need to do something more you have to add it to the interface, rather than hack away at whatever properties you want.

I highly recommend using interfaces in this way. You will thank yourself as your codebase grows.

Friday
May212010

Updated Unity Bézier curve - Now in easy to use Prefab form

So a while ago I implemented a Bézier curve for use in fly-throughs and anywhere where you need something to follow a nice curve. It worked kinda well enough for a simple fly-through but in the project I'm currently working on I needed much more control over the curve, so I've redone it and now it's much more usable. 

The previous one used a kind of quazi Bézier curve with the control points being the waypoints around the point you're calculating. This one uses proper control points, and they are all nicely editable. 

 Here is the prefab once you drag it into the scene. It comes with two points. To create more just duplicate one of the points. The green points represent the waypoints that make up the curve, and the purple points are the left and right control points for each waypoint. 

With the added control, you can make any curve you want now. 

 To use this prefab, the only Interface you should have to worry about is IBezierCurveManager. This is the interface to the BezierCurveManager (duh) and it only has two methods. 

  • Vector3 GetPositionAtTime(float time);
  • Vector3 GetPositionAtDistance(float distance, float time);

Plus the options on the BezierCurveManager script editable from the Editor. 

  • DrawGizmos
  • DrawControlPoints
  • IsFullLoop
  • SecondsForFullLoop
  • EnableDistanceCalculations

GetPositionAtTime - Gets a position along the curve at a time, with 0 being the start of the curve and SecondsForFullLoop being the end (or start again if IsFullLoop is true). 

GetPositionAtDistance - The same as GetPositionAtTime but with a distance offset. This is useful for having one object follow another. Because the speed along the curve is not constant usually (unless your waypoints are evenly spaced. In order to use this method you must have EnableDistanceCalculations set to true before starting. 

Now, it's a little hacky how I get the Distance calculations for use in EnableDistanceCalculations. I basically do a run around the loop first and manually calculate the distance. I'm sure there is a nicer mathematical way to do it, but this will do for now. But what it means is that there is a little pause on startup where it calculates it all. If you don't want this and don't want to call EnableDistanceCalculations then just set EnableDistanceCalculations to false. 

So, here is the Prefab Package. Hope it all works. 

And here is an Example Project using it. Here we have a red ball going really fast along the curve, a bigger red ball going more slowly along it, and two purple balls a fixed distance ahead and behind it. 

The example project has a source file Sphere.cs which shows simply how to find the IBezierCurveManager object and call GetPositionAtTime on it. 

A note on speed: 

This is crazy fast, so if you only have a few objects using it you should not have a problem with speed. The GetPositionAtDistance call does do more than the normal one, but it's just running through arrays which seem to be stupidly quick. I'm sure there are optimisations to be found, but I didn't see the point at the moment. I've tested it on my 3GS iPhone with negligible effect. 

Enjoy!