Created
June 13, 2018 22:34
-
-
Save chuwilliamson/03a1a3efced0f292019afdf6abbc0822 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEditorInternal; | |
using UnityEngine; | |
using UnityEngine.Events; | |
namespace Zyron | |
{ | |
[CreateAssetMenu(menuName = "Scriptables/Sequencer")] | |
public class Sequencer : ScriptableObject | |
{ | |
public List<SequenceEvent> Sequence; | |
public SequenceEvent Current; | |
public int start = 0; | |
public void OnEnable() | |
{ | |
Sequence.ForEach(s => s._onDone = MoveNext); | |
Current = Sequence[start]; | |
} | |
public void BeginSequence(MonoBehaviour mb) | |
{ | |
Current.Doit(mb); | |
} | |
public void MoveNext() | |
{ | |
start++; | |
Current = Sequence[start]; | |
} | |
} | |
[System.Serializable] | |
public class SequenceEvent | |
{ | |
public SequenceEvent(System.Action onDone) | |
{ | |
_onDone = onDone; | |
} | |
public System.Action _onDone; | |
[SerializeField] | |
private UnityEvent Response; | |
public float time = 2; | |
public void Doit(MonoBehaviour mb) | |
{ | |
mb.StartCoroutine(Invoke()); | |
} | |
public IEnumerator Invoke() | |
{ | |
var ct = time; | |
while (ct > 0) | |
{ | |
ct -= Time.deltaTime; | |
yield return null; | |
} | |
Response.Invoke(); | |
time = 2; | |
_onDone.Invoke(); | |
} | |
} | |
[CustomEditor(typeof(Sequencer))] | |
public class SequenceEditor : Editor | |
{ | |
private ReorderableList list; | |
private SerializedProperty spEvent; | |
private void OnEnable() | |
{ | |
// Create the list | |
list = new ReorderableList(serializedObject, serializedObject.FindProperty("Sequence"), true, true, true, true) | |
{ | |
drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, "Sequence"); } | |
}; | |
// Draw header label | |
// Draw each element in the list | |
list.drawElementCallback = | |
(Rect rect, int index, bool isActive, bool isFocused) => | |
{ | |
var element = list.serializedProperty.GetArrayElementAtIndex(index); | |
rect.y += 20; | |
EditorGUI.PropertyField( | |
new Rect(rect.x, rect.y, rect.width - 30, EditorGUIUtility.singleLineHeight), | |
element.FindPropertyRelative("Response"), GUIContent.none); | |
EditorGUI.PropertyField( | |
new Rect(rect.x + rect.width - 25, rect.y, 25, EditorGUIUtility.singleLineHeight), | |
element.FindPropertyRelative("time"), GUIContent.none); | |
}; | |
} | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
GUILayout.Space(25); | |
list.DoLayoutList(); | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment