Пример #1
0
        public override void Initialize()
        {
            // use Binding to bind tags to a strong enum type
            _sceneTags[SceneTags.Monster] = "Monster";

            // bind IPaperdoll interface to a Playerpaperdoll implementation
            Injector.BindTransient <IPaperdoll, PlayerPaperdoll>();
            // create a new Player and bind it as an injectable singleton to Player implementation
            Injector.BindSingleton <Player>(
                Factory.Factory.Create <Player>(CreateDefaultPlayer, SceneObjects));

            // 3 different ways to create player:
            // 1. If you've got a GameObject in the scene with an attached PlayerWorldView component to it we can construct it at runtime:
            Factory.Factory.ConstructMonoBehaviour <PlayerWorldView>("PlayerWorldView_AddedInEditor", SceneObjects);
            // 2. If you've got a GameObject in the scene without an attached PlayerWorldView component to it we can add it at runtime:
            Factory.Factory.AddComponent <PlayerWorldView>("PlayerWorldView_EmptyContainer", SceneObjects);
            // 3. We can make a new GameObject in code here at runtime and add the component to it (note this could easily be a prefab):
            var playerGameObject = new GameObject("PlayerWorldView_FromCode");

            Factory.Factory.AddComponent <PlayerWorldView>(playerGameObject, SceneObjects);
            // Important note: Since the Player object is a singleton each PlayerWorldView has a reference to the exact same
            // instance of the PlayerData.  We've successful encapsulated Player from it's Scene View representation.
            // We could easily give this exact same Player to another system, like HUD, Combat, or a Web Service via Injection
            // in the same manner.

            // re-bind IPaperdoll interface to MonsterPaperdoll now
            Injector.BindTransient <IPaperdoll, MonsterPaperdoll>();
            // set default constructor for MonsterPaperdolls
            Factory.Factory.SetConstructor <MonsterPaperdoll>(CreateDefaultMonsterPaperdoll);
            // tell Injector to make new instances of Monster
            Injector.BindTransient <Monster>();

            // iterate monsters added in the scene by our bound tag
            var monsters = GameObject.FindGameObjectsWithTag(_sceneTags[SceneTags.Monster]);

            for (var index = 0; index < monsters.Length; index++)
            {
                var monster = monsters[index];
                // construct the MonsterWorldView component pre-attached to the monsters (they pre-exist in the scene)
                Factory.Factory.ConstructMonoBehaviour <MonsterWorldView>(monster, SceneObjects, monster.name + "_" + index);
            }
            // Important note: Even though we only use method 1 here for constructing the MonsterWorldView we could easily be
            // instantiating monsters here from a prefab at runtime and constructing those instead.
        }
Пример #2
0
        void Awake()
        {
            CDebug.DisplayMethod = CDebug.ConsoleLogMethod.Selected;
            //CDebug.LogThis(typeof(Injector));

            if (Instance != this && Instance != null)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance  = this;
                _commands = new CommandQueue();

                // create framework manager objects
                _frameworkObjects = new SceneObjectData();
                _frameworkObjects.PushObjectAsSingleton(new SceneManager());

                Injector.BindTemplate("Framework_UseDiskAccess", _readWriteSettingsConfiguration);
                Injector.BindSingleton <EngineOptions>(Factory.Factory.Create <EngineOptions>(_frameworkObjects));

                // give App a getter.  Get options via:
                // Framework.App.Options.<Option>
                // Remember to apply options when necessary
                // true/false parameter tells App if it can use JSON to write and read default configurations from a HD.
                App = _frameworkObjects.PushObjectAsSingleton(Factory.Factory.Create <App>());

                _frameworkObjects.InitializeSceneObjects();

                Globals = new SceneObjectData();
                Globals.InitializeSceneObjects();

                // ProcessCommands() executes all commands in a co-routine
                StartCoroutine(ProcessCommands());

                DontDestroyOnLoad(gameObject);
            }

            // when Awake() is invoked we've loaded a new scene so we create the new view
            SceneManager.CreateSceneView();
        }