将游戏对象注入场景

问题描述 投票:-1回答:1

所以我的游戏对象上带有GameState组件,而我的游戏对象上带有SceneContextGameInstaller组件。 GameInstaller具有GameState的字段,我也已将GameInstaller引用给monoinstallers。当我从预制实例化玩家游戏对象时-在[[GameState字段上具有[Inject]属性,并尝试使用GameState组件访问场景中的真实游戏对象-我得到的是空引用。

就是我的binding,也是我的injecting(这是我的敌人/玩家的基类)

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派生的。

这就是我如何使用播放器/敌人脚本实例化预制件的方法>>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)); } } }

}

因此,我的游戏对象上带有GameState组件,我的游戏对象上带有SceneContext和GameInstaller组件。 GameInstaller具有GameState的字段,我也将GameInstaller引用到...
c# unity3d zenject
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.