Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Game events and shared data realized using scriptable objects

License

NotificationsYou must be signed in to change notification settings

chark/unity-scriptable-objects

Repository files navigation

Unity 2019.4+Actions Status

⚠️We are deprecating/splitting up this repository. No new features will be added except for bug fixes. Further development will continue on:

This package provides utilities for implementing game architecture which is oriented aroundScriptableObject assets and game events. Most of these ideas are based onUnite2017.

Features

  • Game events - allows transferring of data between scripts usingScriptableObject event assets.
  • Game event listeners - listener components which allow subscribing to various events.
  • Mutable objects - eases up sharing of mutable data viaScriptableObject assets.

Installation

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"  }}

Samples

Example usage of game events and mutable objects can be found inAssets/Samples directory. These samples can also be imported viaUnity Package Manager.

Documentation

Game events

Example usage of Game Event assetsExample of a setup Game Event Listener component

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 abool argument.
  • IntGameEvent - event with anint argument.
  • FloatGameEvent - event with afloat argument.
  • StringGameEvent - event with astring argument.
  • Vector2GameEvent - event with aVector2 argument.
  • Vector3GameEvent - event with aVector3 argument.
  • TransformGameEvent - event with aTransform argument.
  • GameObjectGameEvent - event with aGameObject argument.

Mutable objects

Example usage of Mutable Objects

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 abool value.
  • MutableInt - encapsulates anint value.
  • MutableFloat - encapsulates afloat value.
  • MutableString - encapsulates astring value.
  • MutableVector2 - encapsulates aVector2 value.
  • MutableVector3 - encapsulates aVector3 value.

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.

Custom game events

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;}}

Custom mutable objects

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;}}

[8]ページ先頭

©2009-2025 Movatter.jp