# Unity: ScriptableObjects Game Events System
Based on a great talk by Ryan Hipple, here is my improved version of his Game Event system for Unity
**Quick Note:**
This project doesn't use any namespaces.
Conflicts with your own code may happen if you have any classes named the same.
There are 3 classes:
GameEvent
EventListener
EventAndResponse
## Why use this
At Unite Austin 2017, Ryan Hipple talked about the advantage of using scriptable objects in Unity.
The video is here (HIS TALK):
https://youtu.be/raQ3iHhE_Kk?t=1964
Around 32 minutes in, he talks about creating an event system that allows you and your team to quickly work on features that can act independently. His example: Imagine a designer was working on a player HP bar. He would need to bring in the player into his test scene, maybe some sort of manager, and who knows what else just to do this one thing. With his system using Game Events, you can create a player "take damage" event and have the UI for the HP bar just listen to this event. The best part: You don't even need the player in the scene. Watch his video. He explains it better.
I started using his system but found it got pretty complex adding lots of Event Listener to a single game object so I changed up the code to make it list based. You add just 1 Event Listener component to your GameObject and you can add as many events as you want and as many responses to each event as you want.
Here is MY video on it:
https://www.youtube.com/watch?v=1ZK63Mp6yTY
In my example, I set up an Input field. I have a simple script that once finished inputting it checks the length of the string.
The script has 2 Game Events.
1 for if it has enough characters and 1 if there isn't enough.
In the if statement of the script, simply Raise the correct Game Event. Anything that is listening to the event will react.
Add an Event Listener.
Listen for each game event and just add what you want to happen if either is raised. This is really designer-friendly as adding new events is as easy as: "Create > Game Event" in your project window.
## Installation
Just download the files and drag the EventSystem folder into your Asset folder (or any subfolder of your creation) in Unity.
## Usage and Sample Project
Start by creating a game event:

You will need something to raise the event.
Here is an example with a button and a script that raises the event when the button is pressed.

You can just add a public GameEvent variable to your code and do
```c#
eventName.Raise();
```
wherever you need.
Next, you need to add an Event Listener to the object that needs to "listen" for an event to be raised.
[!eventRaised](https://i.imgur.com/GyaLgKh.png)
Add an element to the list (it will default to 0).
Set the Game Event to listen for by dragging the game event into the slot or clicking on the Game Event variable and finding it in your project.
You can listen out for different types of events. You can pass in strings, ints, floats, and bools by setting them in the game event or by setting them before calling the "eventName.Raise".
Example:
```c#
eventName.sentInt = 5;
eventName.Raise();
```
This can then be used with the event type "Respone For Sent Int" as long as your function takes in an int as a parameter.
That's it really.
Check out the sample.
Download Game Event System Demo to see this in action.
Our example game development scenario includes a player inside a game world, some UI, and a game object for keeping keyboard input separate.

Yeah, it's basic.
Here is what the scene looks like:

I kept Input as it's own object to show how modular this system really is.
You don't NEED to have the player on screen to send events the keys were pressed for raising the moving events.
You can use WASD to move. You can even hide and show the player using the buttons on screen.
You will notice the HP bar does nothing. This is intentional.
If you go into the "Game Events" folder you will see 2 events for Damage and Heal.

Using the custom inspector we can raise the event right from viewing the actual event asset. This is great for debugging and quicking checking if an event is firing.
You will also notice the sent int is 1. This is the damage to the HP.
The HP bar has a simple script for filling the bar by having a current value and max value and converting that to a value the fill image can understand.
Try damage and heal the player from the editor's events.
If you found any of that confusing, watch MY video above or reach out to me on Twitter: https://twitter.com/stephenmcvicker
## Remember
The goal is to keep everything modular/data-driven and designer-friendly.
You will need to create some helper scripts at some points.
This is designed so you can add features or subtract them without breaking anything.
Try to keep your event listeners to parent objects of the objects you want to affect. If your game object is disabled it won't raise the event so use your parents!
On your event raised you can add a listener to display/update UI, spawn in an object or effect, play an animation or even play a sound. There is a lot you can do with this system if you just experiment and play with it.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Unity:ScriptableObjects游戏事件系统 根据Ryan Hipple的精彩演讲,这是我为Unity开发的Game Event系统的改进版本 快速说明:该项目不使用任何名称空间。 如果您有任何名称相同的类,则可能会与您自己的代码发生冲突。 有3个类:GameEvent EventListener EventAndResponse 为什么用这个 在Unite Austin 2017上,Ryan Hipple谈到了在Unity中使用可脚本化对象的优势。 视频在这里(他说): ://youtu.be/raQ3iHhE_Kk?t 1964大约32分钟,他谈到要创建一个事件系统,该系
资源详情
资源评论
资源推荐
收起资源包目录























































































共 73 条
- 1

























XanaHopper
- 粉丝: 45
- 资源: 4725
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于Arduino设计的直流电压表毕业项目,原理图、源码-电路方案
- 基于深度学习和单目摄像头测距的前车碰撞预警系统源码详解(附设计文档),基于深度学习和单目摄像头测距的前车碰撞预警系统源码详解(GPU与CPU版本),前车碰撞预警-FCW,基于深度学习和单目摄像头测距
- RedPanda.C .3.2.win64.MinGW64-11.4.zip
- unixbench自动化脚本
- 毕业设计基于Python的Django-html基于知识图谱电影推荐问答系统源码(完整前后端+mysql+说明文档+LW+PPT).zip
- 青海统计年鉴2000-2020年
- 价值19800的影视视频微信小程序源码-自带支付通道带采集+搭建教程
- mingw-w64-x86-64-gmp-6.3.0-2-any.pkg.zip
- lua基础编程,包括lua的基本类型,语法,table,面向对象,协程,和c api
- 扫地机器人路径规划中遗传算法的应用与MATLAB实现-遗传算法-路径规划-扫地机器人-优化搜索-matlab
- 软考初级程序员 C程序设计(一)ppt
- spec2017工具cfg配置文件
- ltp20240524工具
- 西门子PLC STEP7编程软件梯形图与昆仑通态触摸屏电锅炉峰谷电供热系统全方案,CAD原理图全套包装解析,西门子PLC与昆仑通态触摸屏联控电锅炉供热系统:峰电供热与谷电蓄热水箱全自动蓄能程序CAD原
- HFI脉振方波高频注入与增强滑膜esmo代码:含原厂文档,TI与ST移植方式,学习资料大放送,HFI脉振方波高频注入与增强滑膜esmo代码:含原厂文档,可移植使用,支持TI与ST方式,HFI脉振方波高
- 2025年2月28日PPTX
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论1