add game event scriptable object
This commit is contained in:
		
							parent
							
								
									9ef1d12a40
								
							
						
					
					
						commit
						d945aea322
					
				
							
								
								
									
										8
									
								
								game/Assets/Scripts/Util/Events.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								game/Assets/Scripts/Util/Events.meta
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: 63db837647a10954ea169d14e0ceedce | ||||||
|  | folderAsset: yes | ||||||
|  | DefaultImporter: | ||||||
|  |   externalObjects: {} | ||||||
|  |   userData:  | ||||||
|  |   assetBundleName:  | ||||||
|  |   assetBundleVariant:  | ||||||
							
								
								
									
										80
									
								
								game/Assets/Scripts/Util/Events/GameEvent.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								game/Assets/Scripts/Util/Events/GameEvent.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using UnityEngine; | ||||||
|  | #if UNITY_EDITOR | ||||||
|  | using UnityEditor; | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | namespace Ktyl.Util | ||||||
|  | { | ||||||
|  |     [CreateAssetMenu(menuName = "ktyl/Events/Game Event")] | ||||||
|  |     public class GameEvent : ScriptableObject | ||||||
|  |     { | ||||||
|  |         [SerializeField] private bool _logRaised; | ||||||
|  | 
 | ||||||
|  |         protected readonly List<GameEventListener> _listeners = new List<GameEventListener>(); | ||||||
|  | 
 | ||||||
|  |         public virtual void Raise() | ||||||
|  |         { | ||||||
|  |             if (_logRaised) | ||||||
|  |             { | ||||||
|  |                 Debug.Log($"raised {this}", this); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             for (int i = 0; i < _listeners.Count; i++) | ||||||
|  |             { | ||||||
|  |                 _listeners[i].OnEventRaised(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public void Register(GameEventListener listener) | ||||||
|  |         { | ||||||
|  |             if (_listeners.Contains(listener)) | ||||||
|  |             { | ||||||
|  |                 Debug.LogError($"{listener} already registered", this); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             _listeners.Add(listener); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public void Unregister(GameEventListener listener) | ||||||
|  |         { | ||||||
|  |             if (!_listeners.Contains(listener)) | ||||||
|  |             { | ||||||
|  |                 Debug.LogError($"{listener} not already registered"); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             _listeners.Remove(listener); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     #region Editor | ||||||
|  |     #if UNITY_EDITOR | ||||||
|  | 
 | ||||||
|  |     [CustomEditor(typeof(GameEvent), true)] | ||||||
|  |     public class GameEventEditor : Editor | ||||||
|  |     { | ||||||
|  |         private GameEvent _event; | ||||||
|  | 
 | ||||||
|  |         private void OnEnable() | ||||||
|  |         { | ||||||
|  |             _event = target as GameEvent; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public override void OnInspectorGUI() | ||||||
|  |         { | ||||||
|  |             base.OnInspectorGUI(); | ||||||
|  | 
 | ||||||
|  |             if (GUILayout.Button("Raise")) | ||||||
|  |             { | ||||||
|  |                 _event.Raise(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     #endif   | ||||||
|  |     #endregion | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								game/Assets/Scripts/Util/Events/GameEvent.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								game/Assets/Scripts/Util/Events/GameEvent.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: 3a977303773797047b37664649362484 | ||||||
|  | MonoImporter: | ||||||
|  |   externalObjects: {} | ||||||
|  |   serializedVersion: 2 | ||||||
|  |   defaultReferences: [] | ||||||
|  |   executionOrder: 0 | ||||||
|  |   icon: {instanceID: 0} | ||||||
|  |   userData:  | ||||||
|  |   assetBundleName:  | ||||||
|  |   assetBundleVariant:  | ||||||
							
								
								
									
										29
									
								
								game/Assets/Scripts/Util/Events/GameEventListener.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								game/Assets/Scripts/Util/Events/GameEventListener.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using UnityEngine; | ||||||
|  | using UnityEngine.Events; | ||||||
|  | 
 | ||||||
|  | namespace Ktyl.Util | ||||||
|  | { | ||||||
|  |     public class GameEventListener : MonoBehaviour | ||||||
|  |     { | ||||||
|  |         [SerializeField] private GameEvent _event; | ||||||
|  |         [SerializeField] private UnityEvent _response; | ||||||
|  | 
 | ||||||
|  |         private void OnEnable() | ||||||
|  |         { | ||||||
|  |             _event.Register(this); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private void OnDisable() | ||||||
|  |         { | ||||||
|  |             _event.Register(this); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public void OnEventRaised() | ||||||
|  |         { | ||||||
|  |             _response.Invoke(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								game/Assets/Scripts/Util/Events/GameEventListener.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								game/Assets/Scripts/Util/Events/GameEventListener.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: 0a5de09a27d949d4db67034f55c57e6b | ||||||
|  | MonoImporter: | ||||||
|  |   externalObjects: {} | ||||||
|  |   serializedVersion: 2 | ||||||
|  |   defaultReferences: [] | ||||||
|  |   executionOrder: 0 | ||||||
|  |   icon: {instanceID: 0} | ||||||
|  |   userData:  | ||||||
|  |   assetBundleName:  | ||||||
|  |   assetBundleVariant:  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user