在场景中注入游戏对象

Injection of gameobject on scene

所以我有带有 GameState 组件的游戏对象,我有带有 SceneContextGameInstaller[=32 的游戏对象=] 组件就可以了。 GameInstallerGameState 的字段,我也将 GameInstaller 引用到单一安装程序。当我从预制实例化玩家游戏对象时 - 在 GameState 字段上具有 [Inject] 属性,并尝试使用 GameState[= 访问场景中的那个 verry 游戏对象32=] 组件 - 我得到空引用。

我就是这样 binding, and that's how i'm injecting(这是我 enemies/player 的基础 class)

public class GroundEnemy : AbstractUnit

    public override void SetDefaultConfig(UnitData groundEnemyData, Camera camera, Player target)
{
    Debug.Log(gameState + " - gamestate ref in " + this.name);
    this.target = target;
    healthbarCanvas.worldCamera = camera;
    maxHealth = currentHealth = groundEnemyData.Health;
    speed = groundEnemyData.UnitSpeed;
    collisionDamage = groundEnemyData.UnitColisionDmg;
    gameState.OnGameLost.AddListener(this.LostGame);
}

与 gameState.OnGameLost 一致 - 我有一个空引用。 注入 GameState 我正在从 monobeh 派生的基础 class 中制作。

这就是我使用 player/enemy 脚本实例化预制件的方式

public class Spawner : MonoBehaviour

{
[InjectOptional] private Player injectedPlayer;
[SerializeField] private LevelLoader levelLoader;
[SerializeField] private CameraTracker camTracker;    
[SerializeField] private Joystick joystick;
[Inject(Id = "Player")] private UnitData playerEntity;
[Inject(Id = "Fly Enemy")] private UnitData flyEntity;
[Inject(Id = "Ground Enemy")] private UnitData groundEntity;
[Inject(Id = "Boss Enemy")] private UnitData bossEntity;
[Inject] private ArenaData arenaData;
[Inject] private GameState gameState;
[Header("Prefabs")]
[SerializeField] private Player playerPrefab;
[SerializeField] private FlyEnemy flyEnemyPrefab;
[SerializeField] private GroundEnemy groundEnemyPrefab;
[SerializeField] private BossEnemy bossEnemyPrefab;
[HideInInspector] public List<AbstractUnit> AliveEnemies = new List<AbstractUnit>();
private Player cashedPlayer;

private void Start()
{
    InstantiateEntities();
}

private void InstantiateEntities()
{
    if (injectedPlayer != null)
    {
        var player = Instantiate(injectedPlayer, arenaData.PlayerSpawnPos, Quaternion.identity);
        player.HealthbarCanvas = camTracker.PriorCamera;
        ExtraSetup(player);
    }
    else
    {
        var player = Instantiate(playerPrefab, arenaData.PlayerSpawnPos, Quaternion.identity);
        player.SetDefaultConfig(playerEntity, camTracker.PriorCamera);
        ExtraSetup(player);
    }

    if (arenaData.FlySpawnPoints.Count != 0)
    {
        for (int i = 0; i < arenaData.FlySpawnPoints.Count; i++)
        {
            var entity = Instantiate(flyEnemyPrefab, arenaData.FlySpawnPoints[i], Quaternion.identity);
            AliveEnemies.Add(entity);
            entity.SetDefaultConfig(flyEntity, camTracker.PriorCamera, cashedPlayer);
        }
    }

    if (arenaData.GroundSpawnPoints.Count != 0)
    {
        for (int i = 0; i < arenaData.GroundSpawnPoints.Count; i++)
        {
            var entity = Instantiate(groundEnemyPrefab, arenaData.GroundSpawnPoints[i], Quaternion.identity);
            AliveEnemies.Add(entity);
            entity.SetDefaultConfig(groundEntity, camTracker.PriorCamera, cashedPlayer);
        }
    }

    if (arenaData.BossSpawnPos != Vector2.zero)
    {
        var entity = Instantiate(bossEnemyPrefab, arenaData.BossSpawnPos, Quaternion.identity);
        AliveEnemies.Add(entity);
        entity.SetDefaultConfig(bossEntity, camTracker.PriorCamera, cashedPlayer);
    }
}

private void ExtraSetup(Player player) 
{
    camTracker.SetTarget(player.transform);
    joystick.player = player;
    joystick.gameObject.SetActive(true);
    cashedPlayer = player;
}

public void RemoveEnemy(AbstractUnit abstractUnit)
{
    AliveEnemies.Remove(abstractUnit);
    if (AliveEnemies.IsEmpty())
    {
        gameState.OnPlayerWinLvl?.Invoke();
        if (SceneManager.GetActiveScene().buildIndex + 1 != SceneManager.sceneCount)
        {
            StartCoroutine(levelLoader.LoadScene(SceneManager.GetActiveScene().buildIndex + 1, cashedPlayer));
        }
    }
}

}

你需要绑定FromInstance,像这样:

Container.BindInterfacesAndSelfTo<GameState>().FromInstance(_myGameStateReference).AsSingle();