New mono hooks, general game functions and refactoring of lists

This commit is contained in:
Adam Ramberg 2018-11-04 10:00:06 +01:00
parent 82c3bbc2a7
commit 203fc1e032
37 changed files with 362 additions and 69 deletions

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3d14e5cd785d945609f7f7a4b0c76a63
guid: 055205c364e382049bcbe7b60bdada76
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f7bf490017eec4167b9746660c081299
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityAtoms.Utils;
namespace UnityAtoms
{
public class CreateCollider2DListOnAwake : CreateListOnAwake<Collider2D, Collider2DList, Collider2DEvent, Collider2DListener, Collider2DAction, Collider2DListAction, Collider2DListGameObjectAction, UnityCollider2DEvent> { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 09c7646f6a6db4daf8ebcdb3cac4da20
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,68 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityAtoms.Utils;
namespace UnityAtoms
{
public class CreateListOnAwake<T, L, E, TEL, TELR, GA1, GA2, UER> : MonoBehaviour
where L : ScriptableObjectList<T, E> where E : GameEvent<T>
where TEL : GameEventListener<T, TELR, E, UER>
where TELR : GameAction<T>
where GA1 : GameAction<L> where GA2 : GameAction<L, GameObject>
where UER : UnityEvent<T>
{
[SerializeField]
private bool CreateAddedEvent = true;
[SerializeField]
private bool CreateRemovedEvent = true;
[SerializeField]
private bool CreateClearedEvent = false;
[SerializeField]
private TEL AddedListener;
[SerializeField]
private TEL RemovedListener;
[SerializeField]
private VoidListener ClearedListener;
[SerializeField]
private GA1 OnListCreate;
[SerializeField]
private GA2 OnListCreateWithGO;
void Awake()
{
var list = DynamicAtoms.CreateList<T, L, E>(CreateAddedEvent, CreateRemovedEvent, CreateClearedEvent);
if (list.Added != null)
{
if (AddedListener != null)
{
AddedListener.GameEvent = list.Added;
AddedListener.GameEvent.RegisterListener(AddedListener);
}
}
if (list.Removed != null)
{
if (RemovedListener != null)
{
RemovedListener.GameEvent = list.Removed;
RemovedListener.GameEvent.RegisterListener(RemovedListener);
}
}
if (list.Cleared != null)
{
if (ClearedListener != null)
{
ClearedListener.GameEvent = list.Cleared;
ClearedListener.GameEvent.RegisterListener(ClearedListener);
}
}
if (OnListCreate != null) { OnListCreate.Do(list); }
if (OnListCreateWithGO != null) { OnListCreateWithGO.Do(list, gameObject); }
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8221d8181949e42a39b92731d7ad8025
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 233fa789c93e44857a7192edddaa550b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -29,7 +29,7 @@ namespace UnityAtoms
void Awake()
{
var variable = VariableUtils.RuntimeCreate<T, V, E1, E2>(CreateChangedEvent, CreateChangedWithHistoryEvent);
var variable = VariableUtils.CreateVariable<T, V, E1, E2>(CreateChangedEvent, CreateChangedWithHistoryEvent);
if (variable.Changed != null)
{

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 98fd892ed06d24ac1a149e917bff9e18
guid: 9409dd866fe734c02aeb4bd09615b418
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b0a2bb436fed1413fbdffbbdf7714a72
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Game Actions/Change Scene")]
public class ChangeScene : VoidAction
{
[SerializeField]
private string SceneName;
public override void Do()
{
SceneManager.LoadScene(SceneName);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6b22e4f9eae7b4f53acf41919e98cf8e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,6 @@
using UnityEngine;
namespace UnityAtoms
{
public abstract class Collider2DListAction : GameAction<Collider2DList> { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3a31a3e519a634982a763c62a47580e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,6 @@
using UnityEngine;
namespace UnityAtoms
{
public abstract class Collider2DListGameObjectAction : GameAction<Collider2DList, GameObject> { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fcfe4e64689e9416282270d1d7748fcd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,6 @@
using UnityEngine;
namespace UnityAtoms
{
public abstract class GameObjectFunction : GameFunction<GameObject> { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ee7154d1879f247f1837154583723781
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,13 +2,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Bool List")]
public class BoolList : ScriptableObjectList<bool>
{
public BoolEvent Added;
protected override GameEvent<bool> _Added { get { return Added; } }
public BoolEvent Removed;
protected override GameEvent<bool> _Removed { get { return Removed; } }
}
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Bool")]
public class BoolList : ScriptableObjectList<bool, BoolEvent> { }
}

View File

@ -0,0 +1,7 @@
using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Collider2D")]
public class Collider2DList : ScriptableObjectList<Collider2D, Collider2DEvent> { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 115f51619a9046d48ab91c3d6a5be40e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,4 +2,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Color")]
public class ColorList : ScriptableObjectList<Color, ColorEvent> { }
}

View File

@ -2,13 +2,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Float List")]
public class FloatList : ScriptableObjectList<float>
{
public FloatEvent Added;
protected override GameEvent<float> _Added { get { return Added; } }
public FloatEvent Removed;
protected override GameEvent<float> _Removed { get { return Removed; } }
}
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Float")]
public class FloatList : ScriptableObjectList<float, FloatEvent> { }
}

View File

@ -3,13 +3,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/GameObject List")]
public class GameObjectList : ScriptableObjectList<GameObject>
{
public GameObjectEvent Added;
protected override GameEvent<GameObject> _Added { get { return Added; } }
public GameObjectEvent Removed;
protected override GameEvent<GameObject> _Removed { get { return Removed; } }
}
[CreateAssetMenu(menuName = "UnityAtoms/Lists/GameObject")]
public class GameObjectList : ScriptableObjectList<GameObject, GameObjectEvent> { }
}

View File

@ -2,13 +2,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Int List")]
public class IntList : ScriptableObjectList<bool>
{
public BoolEvent Added;
protected override GameEvent<bool> _Added { get { return Added; } }
public BoolEvent Removed;
protected override GameEvent<bool> _Removed { get { return Removed; } }
}
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Int")]
public class IntList : ScriptableObjectList<int, IntEvent> { }
}

View File

@ -5,14 +5,12 @@ using UnityEngine;
namespace UnityAtoms
{
[Serializable]
public abstract class ScriptableObjectList<T> : ScriptableObject
public abstract class ScriptableObjectList<T, E> : ScriptableObject where E : GameEvent<T>
{
[SerializeField]
private List<T> list = new List<T>();
protected abstract GameEvent<T> _Added { get; }
protected abstract GameEvent<T> _Removed { get; }
[SerializeField]
public E Added;
public E Removed;
public VoidEvent Cleared;
public int Count { get { return list.Count; } }
@ -22,9 +20,9 @@ namespace UnityAtoms
if (!list.Contains(item))
{
list.Add(item);
if (null != _Added)
if (null != Added)
{
_Added.Raise(item);
Added.Raise(item);
}
}
}
@ -34,9 +32,9 @@ namespace UnityAtoms
if (list.Contains(item))
{
list.Remove(item);
if (null != _Removed)
if (null != Removed)
{
_Removed.Raise(item);
Removed.Raise(item);
}
}
}
@ -50,6 +48,11 @@ namespace UnityAtoms
}
}
public bool Contains(T item)
{
return list.Contains(item);
}
public T Get(int i)
{
return list[i];
@ -72,5 +75,6 @@ namespace UnityAtoms
list[index] = value;
}
}
}
}

View File

@ -2,13 +2,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Vector2 List")]
public class Vector2List : ScriptableObjectList<Vector2>
{
public Vector2Event Added;
protected override GameEvent<Vector2> _Added { get { return Added; } }
public Vector2Event Removed;
protected override GameEvent<Vector2> _Removed { get { return Removed; } }
}
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Vector2")]
public class Vector2List : ScriptableObjectList<Vector2, Vector2Event> { }
}

View File

@ -2,13 +2,6 @@ using UnityEngine;
namespace UnityAtoms
{
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Vector3 List")]
public class Vector3List : ScriptableObjectList<Vector3>
{
public Vector3Event Added;
protected override GameEvent<Vector3> _Added { get { return Added; } }
public Vector3Event Removed;
protected override GameEvent<Vector3> _Removed { get { return Removed; } }
}
[CreateAssetMenu(menuName = "UnityAtoms/Lists/Vector3")]
public class Vector3List : ScriptableObjectList<Vector3, Vector3Event> { }
}

View File

@ -0,0 +1,12 @@
using UnityEngine;
namespace UnityAtoms
{
public class OnTriggerExit2DHook : Collider2DHook
{
private void OnTriggerExit2D(Collider2D other)
{
OnHook(other);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f86cc0c7cafd74176a58a8e9793ce49e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 10c7c5ab964b74e279d7708c776389d9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms.Utils
{
public static class DynamicAtoms
{
public static V CreateVariable<T, V, E1, E2>(bool createChangedEvent = true, bool createChangedWithHistoryEvent = false) where V : ScriptableObjectVariable<T, E1, E2> where E1 : GameEvent<T> where E2 : GameEvent<T, T>
{
var sov = ScriptableObject.CreateInstance<V>();
sov.Changed = createChangedEvent ? ScriptableObject.CreateInstance<E1>() : null;
sov.ChangedWithHistory = createChangedWithHistoryEvent ? ScriptableObject.CreateInstance<E2>() : null;
return sov;
}
public static L CreateList<T, L, E>(bool createAddedEvent = false, bool createRemovedEvent = false, bool createClearedEvent = false) where L : ScriptableObjectList<T, E> where E : GameEvent<T>
{
var sol = ScriptableObject.CreateInstance<L>();
sol.Added = createAddedEvent ? ScriptableObject.CreateInstance<E>() : null;
sol.Removed = createRemovedEvent ? ScriptableObject.CreateInstance<E>() : null;
sol.Cleared = createClearedEvent ? ScriptableObject.CreateInstance<VoidEvent>() : null;
return sol;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a19359b163c8a441387edc925b6fb63b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms.Utils
{
public static class Vector2Utils
{
// Find out if two lines intersect
public static bool IsIntersectingAlternative(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
float denominator = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);
if (denominator != 0)
{
float u_a = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denominator;
float u_b = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denominator;
if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1)
{
return true;
}
}
return false;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ed5a68977728e4e2b9f545bb75b36bd9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,7 +4,7 @@ namespace UnityAtoms
{
public static class VariableUtils
{
public static V RuntimeCreate<T, V, E1, E2>(bool createChangedEvent = true, bool createChangedWithHistoryEvent = false) where V : ScriptableObjectVariable<T, E1, E2> where E1 : GameEvent<T> where E2 : GameEvent<T, T>
public static V CreateVariable<T, V, E1, E2>(bool createChangedEvent = true, bool createChangedWithHistoryEvent = false) where V : ScriptableObjectVariable<T, E1, E2> where E1 : GameEvent<T> where E2 : GameEvent<T, T>
{
var sov = ScriptableObject.CreateInstance<V>();
sov.Changed = createChangedEvent ? ScriptableObject.CreateInstance<E1>() : null;

View File

@ -4,12 +4,12 @@
## 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).
[This](https://www.youtube.com/watch?v=6vmRwLYWNRo&t=738s) talk by Richard Fine is a forerunner to Ryan Hipple's talk during Unite 2017.
[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.
## 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 become:
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*