接口的作用就是所有订阅接口的代码当达成一定条件是都会执行此代码;
例如,玩家死亡之后,敌人全部都应有胜利的动画,就可以用此方法;
首先创建一个接口代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IEndGameObserver
{
void EndNotify();
}
因为他是接口所以是interface而不是一般的class
只需要在里面协商需要调用的方法即可
上面的using System.Collections;
using System.Collections.Generic;
using UnityEngine;也是不需要的可以删除;
在其他需要的敌人代码中需要添加此接口和对应的接口函数
如
public class EnemyController : MonoBehaviour,IEndGameObserver
{
public void EndNotify()
{
anim.SetBool("Win", true);
isChase = false;
isWalk = false;
attackTarget = null;
}
}
必须要添加接口中的函数,
然后在GameMnager中添加所有的接口
GameManger是我创建的一个单例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMan