- Notifications
You must be signed in to change notification settings - Fork1
Game events and shared data realized using scriptable objects
License
chark/unity-scriptable-objects
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package provides utilities for implementing game architecture which is oriented aroundScriptableObject assets and game events. Most of these ideas are based onUnite2017.
- Game events - allows transferring of data between scripts using
ScriptableObjectevent assets. - Game event listeners - listener components which allow subscribing to various events.
- Mutable objects - eases up sharing of mutable data via
ScriptableObjectassets.
This package can be installed by using theUnity Package Manager. To install this package, add the following tomanifest.json:
{"dependencies": {"com.chark.unity-scriptable-objects":"https://github.com/chark/unity-scriptable-objects.git#upm" }}Example usage of game events and mutable objects can be found inAssets/Samples directory. These samples can also be imported viaUnity Package Manager.
Game events are scriptable objects (Right Click -> Create -> Game Events -> ...) which can be subscribed to via listener components (Add Component -> Game Events -> ...). Events allow to decouple scripts and instead rely on intermediateScriptableObject assets for communication.
Available game events:
GameEvent- simple event which doesn't accept any arguments.BoolGameEvent- event with aboolargument.IntGameEvent- event with anintargument.FloatGameEvent- event with afloatargument.StringGameEvent- event with astringargument.Vector2GameEvent- event with aVector2argument.Vector3GameEvent- event with aVector3argument.TransformGameEvent- event with aTransformargument.GameObjectGameEvent- event with aGameObjectargument.
Mutable objects are used for storing and editing data onScriptableObject assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations whereScriptableObject data needs to be reset when theactive scene changes.
Available mutable objects:
MutableBool- encapsulates aboolvalue.MutableInt- encapsulates anintvalue.MutableFloat- encapsulates afloatvalue.MutableString- encapsulates astringvalue.MutableVector2- encapsulates aVector2value.MutableVector3- encapsulates aVector3value.
Each mutable object has aResetType property. This allows specifying when data in the mutable object should be reset. The following modes are available:
None- do not reset (default).ActiveSceneChange- when the active (focused) scene changes.SceneUnloaded- when the current scene gets unloaded.SceneLoaded- when the scene is loaded.
In some situations, built-in game events might not suffice. For example if a custom type needs to be passed as an argument to the event. In this case, a custom game event can be created which would carry all the necessary data.
To create a custom game event, first create a regularUnityEvent:
[Serializable]publicclassCustomEvent:UnityEvent<Custom>{}
After that is ready, create a game event by extendingGameEvents.Generic.ArgumentGameEvent:
[CreateAssetMenu(fileName="CustomEvent",menuName="Game Events/Custom Event")]publicclassCustomGameEvent:ArgumentGameEvent<Custom>{}
Finally, create a game event listener by extendingGameEvents.Generic.ArgumentGameEventListener:
[AddComponentMenu("Game Events/Custom Game Event Listener")]publicclassCustomGameEventListener:ArgumentGameEventListener<CustomGameEvent,CustomEvent,Custom>{}
, add a custom editor so that the event could be raised, and the listeners which reference the event get displayed in the inspector.
[CustomEditor(typeof(CustomGameEvent))]publicclassGameObjectGameEventEditor:ArgumentGameEventEditor<CustomGameEvent,Custom>{protectedoverrideCustomDrawArgumentField(Customvalue){varfieldValue=EditorGUILayout.ObjectField(value,typeof(Custom),true);returnfieldValueasCustom;}}
In some cases, littering the script code with loads ofMutableObject references can be inconvenient. To avoid this, a single object can be used which encompasses multiple fields.
To create a custom mutable object, extendMutableObjects.Generic.MutableObject and overrideResetValues() method, e.g:
[CreateAssetMenu(fileName="MutableCustom",menuName="Mutable Objects/Mutable Custom")]publicclassMutableCustom:MutableObject{[SerializeField]privateinthealth=default;[SerializeField]privateintarmor=default;[SerializeField]privateintxp=default;publicintHealth{get;set;}publicintArmor{get;set;}publicintXp{get;set;}// This will set property values when mutable object is enabled or if the values change in the// inspector.publicoverridevoidResetValues(){Health=health;Armor=armor;Xp=xp;}}
About
Game events and shared data realized using scriptable objects
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.


