Ejemplo n.º 1
0
    void LoadSingletonClasses()
    {
        LoadedData.singletonList = new Dictionary <Type, ISingleton>();

        Type[] types = new Type[0];
        Type   t     = typeof(ISingleton);

        types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => t.IsAssignableFrom(p)).ToArray();


        for (int i = 0; i < types.Length; i++)
        {
            ConstructorInfo info = types[i].GetConstructor(new Type[0]);
            ISingleton      inst = null;

            if (info != null)
            {
                inst = info.Invoke(new object[0]) as ISingleton;

                MonoBehaviour singleton = new GameObject(inst.GetType().FullName).AddComponent(inst.GetType()) as MonoBehaviour;
                DontDestroyOnLoad(singleton.gameObject);

                ISingleton castedSingleton = singleton as ISingleton;
                castedSingleton.RunOnCreated();

                LoadedData.singletonList.Add(types[i], castedSingleton);
            }
        }
    }