【Unity】一个消息框架例子

【Unity】一个消息框架例子_第1张图片

框架实现:

定义消息类:

public class Msg1

{

public byte Type;

public int Command;

public object Obj;

public Msg1() { }

public Msg1(byte Type_, int Command_, object Obj_)

{

this.Type = Type_;

this.Command = Command_;

this.Obj = Obj_;

}

}

public class Msg1Type

{

public static byte Type_Audio = 1;

public static byte Type_UI = 2;

}

public class Msg1Command

{

public static int Command_PlayAudio = 101;

public static int Command_AddScore = 201;

}

定义MonoBase类:

public abstract class MonoBase1: MonoBehaviour

{

//发送消息

public void SendMsg(byte Type, int Command, object Obj)

{

Msg1 msg1 = new Msg1(Type, Command, Obj);

SendMsg(msg1);

}

public void SendMsg(Msg1 msg)

{

MessageCenter1.SendMsg(msg);

}

//接收消息

public virtual void ReceiveMsg(Msg1 msg1) { }

//分发消息

public virtual void DistributeMsg(Msg1 msg1) { }

}

定义消息中心类:

public class MessageCenter1

{

public static List managerList = new List();

public static void AddManagerToList(MonoBase1 monoBase1)

{

if (!managerList.Contains(monoBase1)) managerList.Add(monoBase1);

}

//给所有的管理类发送消息

public static void SendMsg(byte Type, int Command, object Obj)

{

Msg1 msg1 = new Msg1(Type, Command, Obj);

SendMsg(msg1);

}

public static void SendMsg(Msg1 msg1)

{

foreach (MonoBase1 monoBase in managerList) monoBase.ReceiveMsg(msg1);

}

}

定义管理器基类:

public abstract class ManagerBase1 : MonoBase1 where T: MonoBase1

{

private static T instance;

public static T Instance { get => instance; }

public List receiveList = new List();

protected byte msgType;

private void Awake()

{

instance = this as T;

SetMsgType();

MessageCenter1.AddManagerToList(this);

}

//将MonoBase添加至receiveList

public void AddToReceiveList(MonoBase1 monoBase1)

{

if (!receiveList.Contains(monoBase1)) receiveList.Add(monoBase1);

}

//设置可接受的消息类型

public abstract void SetMsgType();

//接收消息

public override void ReceiveMsg(Msg1 msg1)

{

if (msg1.Type == msgType)

{

DistributeMsg(msg1);

}

}

//分发消息

public override void DistributeMsg(Msg1 msg1)

{

foreach(MonoBase1 monoBase1 in receiveList)

{

monoBase1.ReceiveMsg(msg1);

}

}

}

使用框架实例

Player触碰到Coin,Coin发送消息使得分数显示增加

Player挂载:

public class PlayerControllerMsg1: MonoBehaviour

{

private void Update()

{

float vertical = Input.GetAxis("Vertical");

float horizontal = Input.GetAxis("Horizontal");

Vector3 dir = new Vector3(horizontal, 0, vertical);

if(dir != Vector3.zero)

{

transform.rotation = Quaternion.LookRotation(dir);

transform.Translate(Vector3.forward * 3 * Time.deltaTime);

}

}

}

Coin挂载:

public class CoinController: MonoBase1

{

private void OnTriggerEnter(Collider other)

{

if(other.tag == "Player")

{

SendMsg(Msg1Type.Type_UI, Msg1Command.Command_AddScore, 1);

Destroy(gameObject);

}

}

}

Canvas挂载:

public class UIManagerOfMsg1 : ManagerBase1

{

public override void SetMsgType(){ this.msgType = MsgType.Type_UI; }

}

分数面板挂载:

public class ScorePanel: MonoBase1

{

public Text text;

private void Start() { UIManagerOfMsg1.Instance.AddToReceiveList(this); }

public override void ReceiveMsg(Msg1 msg1)

{

if(msg1 != null && msg1.Command == Msg1Command.Command_AddScore)

{

int score = int.Parse(text.text) + 1;

text.text = score + "";

}

}

}

你可能感兴趣的:(Unity,unity,游戏引擎,消息框架,C#,游戏开发,框架)