Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active June 1, 2021 03:50
Show Gist options
  • Save tsubaki/ba829224927f66256cbb19798100f922 to your computer and use it in GitHub Desktop.
Save tsubaki/ba829224927f66256cbb19798100f922 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
#if UNITY_EDITOR
using UnityEditorInternal;
using UnityEditor;
#endif
public class GameEvent : MonoBehaviour
{
[Tooltip("イベント開始時の処理")]
[SerializeField] UnityEvent startEvent;
[Tooltip("イベント終了時の処理")]
[SerializeField] UnityEvent endEvent;
[Tooltip("Fire1で進むイベント")]
[HideInInspector] public UnityEvent[] events;
public void Play()
{
StartCoroutine(Progress());
}
IEnumerator Progress()
{
startEvent.Invoke();
foreach( var ev in events)
{
ev.Invoke();
yield return new WaitUntil(()=> Input.GetButtonDown("Fire1"));
yield return null;
}
endEvent.Invoke();
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(GameEvent))]
public class GameEventEditor : Editor
{
private ReorderableList reorderableList;
private SerializedProperty selectedProperty = null;
private bool isStartEnd = false;
void OnEnable()
{
var property = serializedObject.FindProperty("events");
reorderableList = new ReorderableList(serializedObject, property);
reorderableList.onSelectCallback = (reorderableList)=>{
selectedProperty = property.GetArrayElementAtIndex(reorderableList.index);
};
reorderableList.drawHeaderCallback += (rect)=>{ EditorGUI.LabelField(rect, "Event");};
}
public override void OnInspectorGUI()
{
isStartEnd = EditorGUILayout.Foldout(isStartEnd, "Callback");
if( isStartEnd )
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("startEvent"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("endEvent"));
}
reorderableList.DoLayoutList();
if( selectedProperty != null)
EditorGUILayout.PropertyField(selectedProperty);
if( GUI.changed)
{
Undo.RecordObject(target, "change value");
serializedObject.ApplyModifiedProperties();
}
}
}
#endif
@tsubaki
Copy link
Author

tsubaki commented Dec 24, 2019

【使い方】

準備

  • GameEventをシーン内に登録
  • reorderableListの「+」を押してElementを追加
  • Elementを選択
  • Element()の「+」を押し、イベントを追加。
  • 実行したい処理や変更したいパラメーターを追加

動かす

  • GameEventのPlay() を実行
  • ボタンを押すたびにElementに登録した処理が実行される

名称未設定

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment