Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity and Live Recompiling

Discussion in 'Editor & General Support' started by nafonso, Aug 19, 2011.

  1. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    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
    Code (csharp):
    1.  
    2. public MyClass : MonoBehaviour
    3. {
    4.     private static MyClass m_singleton = null;
    5.  
    6.     private SomeClass m_someVar;
    7.  
    8.     public static MyClass Singleton
    9.     {
    10.         get
    11.         {
    12.             if( m_singleton == null )
    13.                 m_singleton = (MyClass)FindObjectOfType( typeof(MyClass) );
    14.             return m_singleton;
    15.         }
    16.     }
    17. }
    18.  
    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:
    Code (csharp):
    1.  
    2. public class MyClass
    3. {
    4.     private static ArrayList m_myList;
    5.  
    6.     static MyClass()
    7.     {
    8.         m_myList = new ArrayList();
    9.         // do whatever you want with it
    10.     }
    11. }
    12.  
    13. 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.
    14.  
    15.  
    16. [B]#2 - Some classes aren't serialized:[/B]
    17. An example of this is a Hashtable (or its generic counterpart Dictionary). Each time you recompile, these variables - static or not - are lost.
    18. But there's ways to avoid this, for example this one:
    19. [code]
    20.    ....
    21.    public static MyClass Singleton
    22.    {
    23.        get
    24.        {
    25.            if( m_singleton == null )
    26.            {
    27.                m_myDictionary = new Dictionary<int,SomeClass>();
    28.                foreach( SomeClass s in FindObjectsOfType(typeof(SomeClass)) )
    29.                    m_myDictionary.Add( s.GetId(), s );
    30.                m_singleton = (MyClass)FindObjectOfType( typeof(MyClass) );
    31.            }
    32.            return m_singleton;
    33.        }
    34.    }
    35.    ...
    36.  

    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:
    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class MyClass
    4. {
    5.     public int x;
    6.     public int y;
    7. }
    8.  
    but with structs, it doesn't work:
    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public struct MyStruct
    4. {
    5.     public int x;
    6.     public int y;
    7. }
    8.  
    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
     
  2. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    455
    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
     
    Last edited: Feb 10, 2012
  3. Mark-Currie

    Mark-Currie

    Joined:
    Sep 6, 2012
    Posts:
    53
    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
     
  4. RebelBinary

    RebelBinary

    Joined:
    Aug 12, 2015
    Posts:
    11