Unity/Asset 분석

[RTS Engine] CustomEvents

lipnus 2021. 7. 15. 20:20
반응형

GameManager에 들어있음

여기서 이런저런 이벤트들의 Callback들이 여기로 다 모임

 

CustomEvents.cs

public delegate void UnitEventHandler(Unit Unit);
public static event UnitEventHandler UnitCreated = delegate {};
public static event UnitEventHandler UnitDead = delegate {};
public static event UnitEventHandler UnitSelected = delegate {};
public static event UnitEventHandler UnitDeselected = delegate {};
public static event UnitEventHandler UnitStartMoving = delegate {};
public static event UnitEventHandler UnitStopMoving = delegate { };
public static event UnitEventHandler UnitWanderToggled = delegate { };
public static event UnitEventHandler UnitInstanceUpgraded = delegate { };

public static void OnUnitDead (Unit unit) //called when a unit is dead
{
	UnitDead (unit);
}

 

 

 

Custom Events

The RTS Engine allows to customize your RTS game furthermore using custom delegate events. To make use of those events, make sure that you have the Custom Events component in the map (you can find it attached to the “GameManager” object when creating a

soumidelrio.com

 

DemoSceneManager

public class UnitCustomEvents : MonoBehaviour
    {
        //This component uses the custom delegate events to monitor unit related custom events and modify the unit's behavior in the demo scene

        //listen to custom events
        private void OnEnable()
        {
            CustomEvents.UnitDead += OnUnitDead;
        }

        private void OnDisable()
        {
            CustomEvents.UnitDead -= OnUnitDead;
        }

        //called each time a unit is dead
        private void OnUnitDead (Unit unit)
        {
            if(unit.gameObject.GetComponent<UnitRagdollEffect>())
                unit.gameObject.GetComponent<UnitRagdollEffect>().Trigger(); //trigger the ragdoll effect

            unit.GetSelection().ToggleSelection(false, false); //disable the unit's selection since it's dead.
        }
    }

 

 

① 여러 Callback들이 CustomEvent로 온다.

② CustomEvent에서 Callback를 받으면, 해당 콜백에 맞는 event를 호출한다

③ 거기에다가 등록을 시켜놓으면 Callback이 왔을 때 해당 이벤트가 실행되게 된다.

 

위 코드의 경우, 유닛이 죽었을 때 OnUnitDead(Unit unit) 매소드가 실행되도록 구현

반응형