# 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:
![CreateGameEvent](https://i.imgur.com/MKbDJDu.png)
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.
![ButtonEventRaise](https://i.imgur.com/oBsLpWp.png)
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.
![sample](https://i.imgur.com/ludHm1p.png)
Yeah, it's basic.
Here is what the scene looks like:
![scene](https://i.imgur.com/xDCTJNJ.png)
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.
![damage](https://i.imgur.com/WyEqd3l.png)
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分钟,他谈到要创建一个事件系统,该系
资源详情
资源评论
资源推荐
收起资源包目录
Unity-ScriptableObjects-Game-Events--master.zip (73个子文件)
Unity-ScriptableObjects-Game-Events--master
.gitignore 430B
EventSystem
Editor
GameEventEditor.cs.meta 285B
GameEventEditor.cs 381B
ContextMenu.cs 8KB
EventListener.cs 3KB
GameEvent.cs 897B
EventRaiser.cs 351B
LICENSE 34KB
README.md 5KB
Game Event System Demo
Assets
EventSystem
EventListener.cs.meta 285B
Editor
GameEventEditor.cs.meta 285B
GameEventEditor.cs 381B
EventRaiser.cs.meta 285B
EventListener.cs 3KB
Editor.meta 214B
GameEvent.cs 897B
EventRaiser.cs 351B
GameEvent.cs.meta 285B
Game Events.meta 214B
Test Scene.unity.meta 197B
Scripts
HPTextDisplay.cs.meta 285B
FillImageValue.cs.meta 285B
Player.cs.meta 285B
Player.cs 919B
FillImageValue.cs 793B
KeyboardInput.cs 671B
KeyboardInput.cs.meta 285B
HPTextDisplay.cs 288B
EventSystem.meta 214B
Test Scene.unity 55KB
Scripts.meta 214B
Art.meta 214B
Game Events
MoveLeft.asset.meta 231B
MoveDown.asset 418B
Heal.asset.meta 231B
MoveRight.asset 419B
Hide Player.asset.meta 231B
MoveRight.asset.meta 231B
MoveUp.asset.meta 231B
Hide Player.asset 446B
Show Player.asset.meta 231B
MoveLeft.asset 418B
Show Player.asset 421B
MoveUp.asset 416B
Heal.asset 414B
MoveDown.asset.meta 231B
Damage.asset.meta 231B
Damage.asset 416B
Art
Sprites.meta 214B
Sprites
FlatSquare.png 17KB
player.png.meta 2KB
player.png 11KB
FlatSquare.png.meta 2KB
ProjectSettings
Physics2DSettings.asset 1KB
ProjectSettings.asset 18KB
TagManager.asset 378B
DynamicsManager.asset 1KB
NavMeshAreas.asset 1KB
EditorBuildSettings.asset 138B
InputManager.asset 6KB
QualitySettings.asset 5KB
EditorSettings.asset 632B
GraphicsSettings.asset 2KB
ProjectVersion.txt 28B
AudioManager.asset 357B
NetworkManager.asset 151B
TimeManager.asset 202B
UnityConnectSettings.asset 812B
ClusterInputManager.asset 114B
Game Event System Demo.sln 1KB
UnityPackageManager
manifest.json 26B
Game Event System Demo.Editor.csproj 24KB
Game Event System Demo.csproj 20KB
共 73 条
- 1
XanaHopper
- 粉丝: 41
- 资源: 4725
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1