Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pluggable Factories - C# Static Constructor

Discussion in 'Scripting' started by Tinus, Jun 9, 2010.

  1. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Hey all,

    I've recently come across the absolutely wonderful design pattern called 'Pluggable Factory'. This design pattern allows you to decouple a factory from knowledge of subclasses of the thing it produces.

    There's a whole bunch of uses. You can use them to build a minimal-maintenance framework for functionality that you'll be continuously extending with loads of subclasses, like messages, events and rules. As a test I would like to use it for serializing/de-serializing some messages over the network.

    Err, I'll leave the design pattern mumbo jumbo to these articles:
    http://www.gamedev.net/reference/articles/article841.asp
    http://adtmag.com/articles/2000/09/25/industrial-strength-pluggable-factories.aspx

    The trick is that each subclassed factory uses a static initialization routine to register itself with a single 'Maker' before the program's main function starts.

    The problem is that I cannot get static initialization to work in Unity. Static constructors don't seem to get called at all! I know I'm not supposed to use object constructors because they can be called randomly in-editor, but static constructors should be a different story. Since Awake() can be used to replace a constructor I was hoping there was also something similar to replace the static constructor (for class initialization). Is there such a thing? Or is there a hack to get around it?
     
  2. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    What? I've been using static constructors for a while now and they've always worked fine.
     
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    Maybe I'm not getting something. Do you have to have them in a Monobehaviour and attached to a gameobject in-scene to run? I just tried that and it did work, but I expected to be able to use them from a class not in a Monobehaviour.

    Example: A.cs is attached to a game object in scene. Play is pressed in the editor.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class A : MonoBehaviour {
    5.   static A() {
    6.     Debug.Log("Static constructor A");
    7.   }
    8. }
    9.  
    10. public class B {
    11.     static B() {
    12.       Debug.Log("Static constructor B");
    13.     }
    14. }
    15.  
    16. // Result: "Static constructor A"
    17.  
    Static constructor B is never called.
     
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    That's absolutely correct and you are getting the expected outcome. Static constructor B is never being called because you are never accessing class B from anywhere. The static constructor is not fired when the application starts, but it is when the first either static method is called or instance is created.
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    I see. Thank you for the explanation!