unity-atoms/README.md

87 lines
5.3 KiB
Markdown
Raw Normal View History

2018-10-30 15:05:06 -04:00
# ⚛️ Unity Atoms
*Tiny modular pieces utilizing the power of Scriptable Objects*
2018-11-16 06:49:57 -05:00
Read [this](https://medium.com/@adamramberg/unity-atoms-tiny-modular-pieces-utilizing-the-power-of-scriptable-objects-e8add1b95201) article on Medium for a great introduction to Unity Atoms.
2018-10-30 15:55:37 -04:00
## Influences
Unity Atoms is derrived from and a continuation of Ryan Hipple's [talk](https://www.youtube.com/watch?v=raQ3iHhE_Kk&t=2787s) from Unite 2017. The original source code can be found [here](https://github.com/roboryantron/Unite2017).
2018-10-30 15:05:06 -04:00
[This](https://www.youtube.com/watch?v=6vmRwLYWNRo&t=738s) talk by Richard Fine is a forerunner to Ryan Hipple's talk during Unite 2016.
2018-10-30 15:05:06 -04:00
2018-10-30 15:55:37 -04:00
## Motivation
The general approach to building scripts in Unity often generates a code base that is monolithic. This results in that your code is cumbersome to test, non-modular and hard to debug and understand.
Unity Atoms is an open source library that aims to make your game code:
- 📦 Modular *- avoid scripts and systems directly dependent on each other*
- ✏️ Editable *- Scriptable Objects makes it possible to make changes to your game at runtime*
- 🐞 Debuggable *- modular code is easier to debug than tightly coupled code*
2018-10-30 15:55:37 -04:00
## Introduction
Before you start looking into this library you should watch the video above ☝️ and read [this](https://unity3d.com/how-to/architect-with-scriptable-objects) article on how to architect your game with Scriptable Objects.
Squashed commit of the following: commit 847eff037204d841546c0da772d2f341f9cf1e25 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 22:09:38 2019 +0100 #17 - Serializable not needed for ScriptableObject commit 593b275e6394b9d589de8a7a20375145dfc4aa84 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 21:59:33 2019 +0100 18 - IGameEvent<T1, T2> RegisterListener commit 40443ce9bd4b1c339aaf19cfcb119f2336608dae Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:35:04 2019 +0100 Remove some more warnings commit bd453110ac51a6ebe3d54368fcb039bdbe52e278 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:33:30 2019 +0100 Update README commit 90977b853a047c84efb5311768a09f4e8a1165b2 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:31:45 2019 +0100 Initialize to null to get rid off warnings commit 38b7f5c4ede195aa7198f567801c9cbeedc9b6f6 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:15:53 2019 +0100 More fixes to enable local unity project commit 404e1cbf88ed4431c61d3ece074e838e74ac5141 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:12:37 2019 +0100 Remove duplicated asmdef commit 5734300684e8a16553f213157cad1b4722b7cb7f Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:09:46 2019 +0100 Change files to include commit d1e42b119a6bc1577b1792459fa298e063652337 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:05:56 2019 +0100 Added root package json commit 1709a0347147d74460f653182bbaf8d15eb6154e Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 15:30:44 2019 +0100 #16 - Add test and examples Unity project commit a3ea1a133bf6727e011ba85c64569db45302e487 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 13:12:02 2019 +0100 #13 - Make usage of UPM (package manager) commit 492a30e905f6cf3f5899cb7080ef2bda73110f00 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 11:28:56 2019 +0100 Added extensions + code formatting fixes commit 709949a1016c236cfd363cf25392fedfd8d083ca Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Fri Mar 8 10:16:45 2019 +0100 More AtomicTags changes (#15) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git * Further AtomicTag optimization commit ae6584c879f182e727fe0a8d0aff4b0715829914 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 10:08:36 2019 +0100 Editor config commit 197d7067608600e4e2d13dc42db909ee8f8c75df Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 09:23:12 2019 +0100 Added editor config file commit 53d6adc07bbc2967c12c17227a1c31d9f1cfba77 Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Tue Mar 5 22:57:47 2019 +0100 More efficient AtomicTags (#12) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git commit 81209d83b5195300d4c2d54411cff3c7983f0d97 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Wed Dec 12 20:54:17 2018 +0100 Added MonoHooks + ColliderType + bug fixes commit c6b240cebbdc410341fb05204235842df5da9d73 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sat Dec 1 00:23:10 2018 +0100 Experimenting with adding UI state management commit dfd70a8944acbeabe5feba1cf6cff2be6802c470 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 23:10:21 2018 +0100 Issue #6 - AtomicTags commit 8907763227f4d4c2a32c5684e7caa4d4a082eb16 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 22:42:29 2018 +0100 First commit of v1.0.0
2019-03-17 18:43:20 -04:00
## Installation 2018.3+
Go to your projects `Packages/manifest.json` and add this:
"dependencies": {
...
"com.mambojambostudios.unity-atoms": "https://github.com/AdamRamberg/unity-atoms.git",
Squashed commit of the following: commit 847eff037204d841546c0da772d2f341f9cf1e25 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 22:09:38 2019 +0100 #17 - Serializable not needed for ScriptableObject commit 593b275e6394b9d589de8a7a20375145dfc4aa84 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 21:59:33 2019 +0100 18 - IGameEvent<T1, T2> RegisterListener commit 40443ce9bd4b1c339aaf19cfcb119f2336608dae Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:35:04 2019 +0100 Remove some more warnings commit bd453110ac51a6ebe3d54368fcb039bdbe52e278 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:33:30 2019 +0100 Update README commit 90977b853a047c84efb5311768a09f4e8a1165b2 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:31:45 2019 +0100 Initialize to null to get rid off warnings commit 38b7f5c4ede195aa7198f567801c9cbeedc9b6f6 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:15:53 2019 +0100 More fixes to enable local unity project commit 404e1cbf88ed4431c61d3ece074e838e74ac5141 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:12:37 2019 +0100 Remove duplicated asmdef commit 5734300684e8a16553f213157cad1b4722b7cb7f Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:09:46 2019 +0100 Change files to include commit d1e42b119a6bc1577b1792459fa298e063652337 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:05:56 2019 +0100 Added root package json commit 1709a0347147d74460f653182bbaf8d15eb6154e Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 15:30:44 2019 +0100 #16 - Add test and examples Unity project commit a3ea1a133bf6727e011ba85c64569db45302e487 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 13:12:02 2019 +0100 #13 - Make usage of UPM (package manager) commit 492a30e905f6cf3f5899cb7080ef2bda73110f00 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 11:28:56 2019 +0100 Added extensions + code formatting fixes commit 709949a1016c236cfd363cf25392fedfd8d083ca Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Fri Mar 8 10:16:45 2019 +0100 More AtomicTags changes (#15) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git * Further AtomicTag optimization commit ae6584c879f182e727fe0a8d0aff4b0715829914 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 10:08:36 2019 +0100 Editor config commit 197d7067608600e4e2d13dc42db909ee8f8c75df Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 09:23:12 2019 +0100 Added editor config file commit 53d6adc07bbc2967c12c17227a1c31d9f1cfba77 Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Tue Mar 5 22:57:47 2019 +0100 More efficient AtomicTags (#12) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git commit 81209d83b5195300d4c2d54411cff3c7983f0d97 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Wed Dec 12 20:54:17 2018 +0100 Added MonoHooks + ColliderType + bug fixes commit c6b240cebbdc410341fb05204235842df5da9d73 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sat Dec 1 00:23:10 2018 +0100 Experimenting with adding UI state management commit dfd70a8944acbeabe5feba1cf6cff2be6802c470 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 23:10:21 2018 +0100 Issue #6 - AtomicTags commit 8907763227f4d4c2a32c5684e7caa4d4a082eb16 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 22:42:29 2018 +0100 First commit of v1.0.0
2019-03-17 18:43:20 -04:00
...
}
This will make the Unity packagemager install it.
2018-10-30 15:55:37 -04:00
## Usage
2018-10-30 16:23:40 -04:00
Unity Atoms is an event based system that encourages the game to be as data-driven as possible. The 4 most fundamental pieces (atoms) of Unity Atoms are:
2018-10-30 15:55:37 -04:00
- Variables
- (Game) Events
- (Game Event) Listeners
- Responses
### Variables
2018-10-30 16:23:40 -04:00
Variables are data / variables stored as Scriptable Objects. Because Variables are stored as Scriptable Objects they are not part of any scene, but could be instead be seen as part of a global shared game state. Variables are also designed to make it easy to inject (via the Unity Inspector) to your MonoBehaviours.
It is possible to attach an event to a Variable that gets raised when its updated. This makes it possible to write more data-driven code.
It is also possible to attach another event to a Variable that also gets raised when a Variable is changed, but that contains both the old and the new value of the Variable.
Unity Atoms also offer some variations / additions to Variables such as Contants, References and Lists.
2018-10-30 15:55:37 -04:00
#### Constants
2018-10-30 16:23:40 -04:00
Exactly the same as Variables, but can not be changed via script and therefore does not contain the change events that Variables does. The idea is to use Constants for for example tags instead of hard coding tags in your scripts.
2018-10-30 15:55:37 -04:00
#### References
2018-10-30 16:23:40 -04:00
References can be toggled between "use as constant" or "use variable" via the Unity Inspector. When a reference is "used as constant" then it functions exactly like a regular serialized variable in a MonoBehaviour script. However, when it is set to "use variable" it functions exactly like a Variable.
2018-10-30 15:55:37 -04:00
#### Lists
2018-10-30 16:23:40 -04:00
A list is an array of values that is stored as a Scriptable Object. There is the possibility to add Game Events for when the following happens to the list:
- An item is added to the list.
- An item is removed from the list.
- The list is cleared.
2018-10-30 15:55:37 -04:00
### Game Events
2018-10-30 16:23:40 -04:00
A game event is a thing that happens in the game that others could listen / subscribe to. Game events are also Scriptable Objects that lives outside of a specific scene. It is possible to raise a Game Event from the Unity Inspector for debug purposes.
2018-10-30 15:55:37 -04:00
2018-10-30 16:23:40 -04:00
### Game Event Listeners
A listener listens / observes / subscribes to an event and raises / invokes zero to many responses to that event. Game Event Listeners are Monobehaviours and lives in a scene. See below for more information on the type of responses there are.
2018-10-30 15:55:37 -04:00
### Responses
2018-10-30 16:23:40 -04:00
A responses is raised by a listener in response to an event. Responses can live both in the scene as [UnityEvents](https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html) or outside the scene as a Scriptable Object in the shape of a Game Action or a Game Function.
2018-10-30 15:55:37 -04:00
#### Game Actions
2018-10-30 16:23:40 -04:00
A Game Action is a C# function as a Scriptable Object. A Game Function can be used as a response in a Game Event Listener.
2018-10-30 15:55:37 -04:00
#### Game Functions
2018-10-30 16:23:40 -04:00
A Game Function is basically the same as a Game Action, but while a Game Actions does not return something a Game Function does.
2018-10-30 15:55:37 -04:00
### Mono Hooks
2018-10-30 16:23:40 -04:00
Mono Hooks is a way to make it possible to have Unity lifecycle methods as events. The main reason for this is to make this pattern consistent and possible to use in ALL of your code.
2018-10-30 15:55:37 -04:00
## Further Notes
When you start thinking about this pattern you will realize that everything can be explained using the atoms above. The native Unity lifecycle methods can be thought of as variation of the pattern above, where events gets raised and passes a long data (eg. OnTriggerEnter2D) and you write a response to that event.
## Examples
2018-11-05 14:22:02 -05:00
Examples will soon be added to this project.
## Contribution
Squashed commit of the following: commit 847eff037204d841546c0da772d2f341f9cf1e25 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 22:09:38 2019 +0100 #17 - Serializable not needed for ScriptableObject commit 593b275e6394b9d589de8a7a20375145dfc4aa84 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 21:59:33 2019 +0100 18 - IGameEvent<T1, T2> RegisterListener commit 40443ce9bd4b1c339aaf19cfcb119f2336608dae Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:35:04 2019 +0100 Remove some more warnings commit bd453110ac51a6ebe3d54368fcb039bdbe52e278 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:33:30 2019 +0100 Update README commit 90977b853a047c84efb5311768a09f4e8a1165b2 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:31:45 2019 +0100 Initialize to null to get rid off warnings commit 38b7f5c4ede195aa7198f567801c9cbeedc9b6f6 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:15:53 2019 +0100 More fixes to enable local unity project commit 404e1cbf88ed4431c61d3ece074e838e74ac5141 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:12:37 2019 +0100 Remove duplicated asmdef commit 5734300684e8a16553f213157cad1b4722b7cb7f Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:09:46 2019 +0100 Change files to include commit d1e42b119a6bc1577b1792459fa298e063652337 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:05:56 2019 +0100 Added root package json commit 1709a0347147d74460f653182bbaf8d15eb6154e Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 15:30:44 2019 +0100 #16 - Add test and examples Unity project commit a3ea1a133bf6727e011ba85c64569db45302e487 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 13:12:02 2019 +0100 #13 - Make usage of UPM (package manager) commit 492a30e905f6cf3f5899cb7080ef2bda73110f00 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 11:28:56 2019 +0100 Added extensions + code formatting fixes commit 709949a1016c236cfd363cf25392fedfd8d083ca Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Fri Mar 8 10:16:45 2019 +0100 More AtomicTags changes (#15) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git * Further AtomicTag optimization commit ae6584c879f182e727fe0a8d0aff4b0715829914 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 10:08:36 2019 +0100 Editor config commit 197d7067608600e4e2d13dc42db909ee8f8c75df Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 09:23:12 2019 +0100 Added editor config file commit 53d6adc07bbc2967c12c17227a1c31d9f1cfba77 Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Tue Mar 5 22:57:47 2019 +0100 More efficient AtomicTags (#12) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git commit 81209d83b5195300d4c2d54411cff3c7983f0d97 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Wed Dec 12 20:54:17 2018 +0100 Added MonoHooks + ColliderType + bug fixes commit c6b240cebbdc410341fb05204235842df5da9d73 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sat Dec 1 00:23:10 2018 +0100 Experimenting with adding UI state management commit dfd70a8944acbeabe5feba1cf6cff2be6802c470 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 23:10:21 2018 +0100 Issue #6 - AtomicTags commit 8907763227f4d4c2a32c5684e7caa4d4a082eb16 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 22:42:29 2018 +0100 First commit of v1.0.0
2019-03-17 18:43:20 -04:00
Would ❤️ if you would like to contribute to the project. Post me a message if you want to become a contributer.