Unity/Asset 분석

[RTS Engine] 현재 Scene에 Spawn된 Unit Instance접근

lipnus 2022. 1. 8. 17:34
반응형

FactionManager.cs

    public class FactionManager : MonoBehaviour {
		// ...
        
		//The lists below hold all different types of units.
		private List<Unit> units = new List<Unit>(); //list containing all the units that this faction owns.
        
        // ...

FactionManager에 다 있음.

CustomEvent에서 Unit이 Create되거나, Destroy될때 리스너를 받아서 자체적으로 업데이트.

 

 

GameDataManager.cs

private bool InitSinglePlayerGame()
  {
  // ...

  //This where we will set the NPC settings using the info from the single player manager:
  //First check if we have enough faction slots available:
  if (LobbyManager.instance.LobbyFactions.Count <= factions.Count)
  {
    defeatCondition = LobbyManager.instance.UIMgr.defeatConditionMenu.GetValue(); //set defeat condition
    speedModifier = LobbyManager.instance.UIMgr.speedModifierMenu.GetValue(); //set speed modifier

    //loop through the factions slots of this map:
    for (int i = 0; i < LobbyManager.instance.LobbyFactions.Count; i++)
    {
      factions[i].Init(
      LobbyManager.instance.LobbyFactions[i].GetFactionName(),
      LobbyManager.instance.LobbyFactions[i].GetFactionType(),
      LobbyManager.instance.LobbyFactions[i].GetFactionColor(),
      LobbyManager.instance.LobbyFactions[i].PlayerControlled,
      LobbyManager.instance.GetCurrentMap().GetInitialPopulation(),
      LobbyManager.instance.LobbyFactions[i].GetNPCType(),
      gameObject.AddComponent<FactionManager>(), i, this);
    }
  
  	// ...

 

gameObject.AddComponent<FactionManager>(), i, this);

 

1.

FactionManager는 Faction당 1개씩 Scene에 AddComponent를 통해 생성된다.

Scene을 Play했을 때 GameManager 게임오브젝트를 보면 밑에 추가되어 있음.

 

2.

FactonManager는 FactionSlot안에 들어있음.

GameManager를 통해 가져오면 되고, 이를 통해 모든 unit들의 instance에 접근가능.

 

 

 

 

Example

IEnumerable<Unit> units = gameMgr.GetFaction(0).FactionMgr.GetUnits();
        
        foreach(Unit unit in units) {
            Debug.Log($"유닛: {unit.name}");
        }

 

하나의 유닛이 있으면 이렇게 표시.

반응형