enum中使用中文 unity_Unity游戏开发——基于枚举的事件系统

本文介绍了如何在Unity游戏开发中利用枚举(EventType)创建一个简单的事件系统,包括定义消息事件、回调方法和核心事件处理类(EventCenter)。通过示例展示了如何广播和监听消息,以及在脚本销毁时正确移除监听。
摘要由CSDN通过智能技术生成

0.前言

游戏开发中,消息机制是比较常见的模块间解耦工具,在此分享一个平时用的比较顺手的事件系统。

1.使用方法介绍

首先分为三个脚本

EventType:用来定义消息事件

CallBack:用委托定义消息回调方法

EventCenter:用来处理消息广播、添加、移除的核心类

使用的时候要先在EventType里先定义好消息的枚举

public enum EventType

{

Test,

ShowText,

ShowInfo,

}

然后编写一个测试脚本,用来演示消息广播

using UnityEngine;

public class EventTest : MonoBehaviour

{

// Start is called before the first frame update

void Start()

{

EventCenter.Broadcast(EventType.Test);

// 第一个参数填写枚举消息,后面填写消息需要携带的参数

EventCenter.Broadcast(EventType.ShowText, "hello world");

EventCenter.Broadcast(EventType.ShowInfo, "hello world", "Xiaoming", 1);

}

}

再编写一个测试脚本,用来接收消息

using UnityEngine;

public class ListenerTest : MonoBehaviour

{

// Start is called before the first frame update

void Awake()

{

// 监听无参数的消息

EventCenter.AddListener(EventType.Test, Test);

// 监听带有参数的消息,使用泛型传入参数类型

EventCenter.AddListener(EventType.ShowText, ShowText);

// 最多支持接收带有五个参数的消息,当然也可以在CallBack里继续扩展参数数量

EventCenter.AddListener(EventType.ShowInfo, ShowInfo);

}

void Test()

{

Debug.Log("no param");

}

void ShowText(string text)

{

Debug.Log(text);

}

void ShowInfo(string text, string name, int age)

{

Debug.Log(text + name + age);

}

// 最后需要在脚本销毁的时候移除监听

private void OnDestroy()

{

EventCenter.RemoveListener(EventType.Test, Test);

EventCenter.RemoveListener(EventType.ShowText, ShowText);

EventCenter.RemoveListener(EventType.ShowInfo, ShowInfo);

}

}

最后运行的效果

2.完整代码

EventCenter.cs

using System;

using System.Collections;

using System.Collections.Generic;

usi

Unity枚举类型enum)用于定义一组相关的符号常量,以便在程序运行时从编译时已经设定的固定数目的选择做出决定。枚举类型的声明使用enum关键字。在Unity枚举类型通常用于定义一组特定的选项,例如游戏的角色类型、道具类型等。 在给定的引用\[1\],展示了在Unity使用枚举类型的示例代码。在这个示例,定义了一个名为Fruit的枚举类型,包含了三个选项:Apple、Pear和Banana。通过使用Enum.GetNames和Enum.GetValues方法,可以分别获取枚举类型的名称和值,并进行遍历和输出。 在Unity使用枚举类型可以方便地管理和使用一组固定的选项,提高代码的可读性和可维护性。通过枚举类型,可以避免使用魔法数字或字符串,使代码更加清晰和易于理解。 总结起来,Unity枚举类型enum)用于定义一组相关的符号常量,以便在程序运行时从编译时已经设定的固定数目的选择做出决定。它可以提高代码的可读性和可维护性,使代码更加清晰和易于理解。 #### 引用[.reference_title] - *1* [Unity 基础 之 实现枚举enum/Enum)遍历的三种简单方法(foreach/for)](https://blog.csdn.net/u014361280/article/details/115945198)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Unity 基础 之 Enum(enum) 枚举 的简单介绍,和枚举变量同时赋值多个值,并且分解枚举变量包含多个值](https://blog.csdn.net/u014361280/article/details/114587433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值