Unity and Live Recompiling

Hi,

Unity is supposed to be this great tool in which you just edit stuff while the game is running, and although this is a great workflow for tweaking (ie you mess around with stuff exposed on the editor), it is not great for programmers, because each time you recompile everything goes haywire.

If you are thinking “What do you mean about that? My game works perfectly after I recompile, I don’t need to restart the game!”, then you are doing things wrong! :-]

So what are the problems?

#1 - Static variables:
Each time we recompile, all static variables are gone. This is a minor issue, because we can get around this by not having static variables (eg having instance variables in singleton) or by having static constructors.

Example without static variables

public MyClass : MonoBehaviour
{
    private static MyClass m_singleton = null;

    private SomeClass m_someVar;

    public static MyClass Singleton
    {
        get
        {
            if( m_singleton == null )
                m_singleton = (MyClass)FindObjectOfType( typeof(MyClass) );
            return m_singleton;
        }
    }
}

By using code like this we can get around having static variables, since every time the game is recompiled m_singleton will be null, but m_someVar won’t, and the check on the property Singleton will make sure that m_singleton is properly filled in. You can even improve this a bit by checking if you are in UNITY_EDITOR to avoid doing the if when deployed (since you don’t recompile when running the game).

Example with static constructors:

public class MyClass 
{
    private static ArrayList m_myList;

    static MyClass()
    {
        m_myList = new ArrayList();
        // do whatever you want with it
    }
}

Every time you access this class, the static constructor is called and you can fill in whatever static variable that will be cleared on recompile.


[B]#2 - Some classes aren't serialized:[/B]
An example of this is a Hashtable (or its generic counterpart Dictionary). Each time you recompile, these variables - static or not - are lost.
But there's ways to avoid this, for example this one:
[code]
    ....
    public static MyClass Singleton
    {
        get
        {
            if( m_singleton == null )
            {
                m_myDictionary = new Dictionary<int,SomeClass>();
                foreach( SomeClass s in FindObjectsOfType(typeof(SomeClass)) )
                    m_myDictionary.Add( s.GetId(), s );
                m_singleton = (MyClass)FindObjectOfType( typeof(MyClass) );
            }
            return m_singleton;
        }
    }
    ...

So far, for all these issues we had a way to get around them, some more complex than others, but you can get around them. However, this brings us to problem 3, the real big one we can’t get around!

#3 - Structs are not serialized
And here is where we get stopped! There’s no way to serialize structs in Unity. With regular classes you can do this:

[System.Serializable]
public class MyClass
{
    public int x;
    public int y;
}

but with structs, it doesn’t work:

[System.Serializable]
public struct MyStruct
{
    public int x;
    public int y;
}

If we recompile while the game is running, the values will be reseted, so the x and y of the struct will have their values set to 0.

Another issue that they have is that they won’t show up by default on the inspectors, contrary to classes, forcing you to do custom editors.

So I’d like to ask for better workflow for programmers! :-] Especially the last point, having structs serializable, since there’s no way to get around this (and no, changing it from struct to class is NOT a solution!).

Regards,
Afonso

I’m currently looking in this issue as well : trying to make my game going on after a script recompilation.
It seems it’s possible to serialize the struct, if it’s contained in a class, as shown here : http://bugshake.com/?p=239 .

It’s a hell of work before to be able to enjoy that sweet edit continue feature… :-S

I don’t think anyone is really maintaining their projects to support Live Recompile, especially projects with big teams. For Editor Extensions, you have to support it, but for actual Unity apps, it’s not really worth the effort. It’s too tedious to maintain. Hopefully this changes in a future version of Unity.

If you want to disable it, here’s one way:
http://unityconsole.com/blog/disable-unity-live-recompile

I maintain my project to use LiveRecompile and I wrote a guide on how to do it