├── .gitattributes ├── .gitignore ├── EventManager.cs ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /EventManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | 4 | /// 5 | /// 事件类型 6 | /// (根据需要取名称,不得重复) 7 | /// 8 | public enum EventType 9 | { 10 | StartGame, 11 | ClickBlock 12 | } 13 | 14 | 15 | /// 16 | /// 事件管理器 17 | /// 18 | public class EventManager { 19 | 20 | /// 21 | /// 事件监听池 22 | /// 23 | private static Dictionary eventTypeListeners = new Dictionary(); 24 | 25 | /// 26 | /// 添加事件 27 | /// 28 | /// 事件类型 29 | /// 监听函数 30 | public static void addEventListener(EventType type,DelegateEvent.EventHandler listenerFunc) 31 | { 32 | DelegateEvent delegateEvent; 33 | if(eventTypeListeners.ContainsKey(type)) 34 | { 35 | delegateEvent = eventTypeListeners[type]; 36 | }else 37 | { 38 | delegateEvent = new DelegateEvent(); 39 | eventTypeListeners[type] = delegateEvent; 40 | } 41 | delegateEvent.addListener(listenerFunc); 42 | } 43 | 44 | /// 45 | /// 删除事件 46 | /// 47 | /// 事件类型 48 | /// 监听函数 49 | public static void removeEventListener(EventType type,DelegateEvent.EventHandler listenerFunc) 50 | { 51 | if (listenerFunc == null) 52 | { 53 | return; 54 | } 55 | if(!eventTypeListeners.ContainsKey(type)) 56 | { 57 | return; 58 | } 59 | DelegateEvent delegateEvent = eventTypeListeners[type]; 60 | delegateEvent.removeListener(listenerFunc); 61 | } 62 | 63 | /// 64 | /// 触发某一类型的事件 并传递数据 65 | /// 66 | /// 事件类型 67 | /// 事件的数据(可为null) 68 | public static void dispatchEvent(EventType type,object data) 69 | { 70 | if(!eventTypeListeners.ContainsKey(type)) 71 | { 72 | return; 73 | } 74 | //创建事件数据 75 | EventData eventData = new EventData(); 76 | eventData.type = type; 77 | eventData.data = data; 78 | 79 | DelegateEvent delegateEvent = eventTypeListeners[type]; 80 | delegateEvent.Handle(eventData); 81 | } 82 | 83 | } 84 | 85 | /// 86 | /// 事件类 87 | /// 88 | public class DelegateEvent 89 | { 90 | /// 91 | /// 定义委托函数 92 | /// 93 | /// 94 | public delegate void EventHandler(EventData data); 95 | /// 96 | /// 定义基于委托函数的事件 97 | /// 98 | public event EventHandler eventHandle; 99 | 100 | /// 101 | /// 触发监听事件 102 | /// 103 | /// 104 | public void Handle(EventData data) 105 | { 106 | if(eventHandle!=null) 107 | eventHandle(data); 108 | } 109 | 110 | /// 111 | /// 删除监听函数 112 | /// 113 | /// 114 | public void removeListener(EventHandler removeHandle) 115 | { 116 | if (eventHandle != null) 117 | eventHandle -= removeHandle; 118 | } 119 | 120 | /// 121 | /// 添加监听函数 122 | /// 123 | /// 124 | public void addListener(EventHandler addHandle) 125 | { 126 | eventHandle += addHandle; 127 | } 128 | } 129 | 130 | /// 131 | /// 事件数据 132 | /// 133 | public class EventData 134 | { 135 | /// 136 | /// 事件类型 137 | /// 138 | public EventType type; 139 | /// 140 | /// 事件传递的数据 141 | /// 142 | public object data; 143 | } 144 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 http://playlive.github.io/ 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityEventManager 2 | unity C# 事件管理器 3 | 4 | 添加事件 5 | 6 | void Start() 7 | { 8 | EventManager.addEventListener(EventType.ClickBlock, eventClickBlock); 9 | } 10 | 11 | public void eventClickBlock(EventData data) 12 | { 13 | Debug.Log("clickBlock"); 14 | } 15 | 16 | 删除事件 17 | 18 | void Start() 19 | { 20 | EventManager.removeEventListener(EventType.ClickBlock, eventClickBlock); 21 | } 22 | 23 | public void eventClickBlock(EventData data) 24 | { 25 | Debug.Log("clickBlock"); 26 | } 27 | 28 | 触发事件 29 | 30 | void OnMouseDown() 31 | { 32 | //不带参数 33 | EventManager.dispatchEvent(EventType.ClickBlock, null); 34 | //带参数(参数为object类型) 35 | EventManager.dispatchEvent(EventType.ClickBlock, "测试参数"); 36 | } 37 | 38 | # Authors 39 | * PlayLive [@放飞吧小熊](http://www.weibo.com/u/1677154562/) 40 | * @2015 [PlayLive](http://playlive.github.io/) 41 | --------------------------------------------------------------------------------