Last active
January 1, 2022 10:06
-
-
Save atori708/aa66cd80fdeee2fb9b24208a44ca89a4 to your computer and use it in GitHub Desktop.
【Unity】Timeline側に仕込んだイベントをフックする
This file contains 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; | |
using System.Reflection; | |
using UnityEngine; | |
using UnityEditor.Timeline; | |
public class TimelinePlayHeadTest : MonoBehaviour | |
{ | |
System.Object timelineWindowObj; | |
EventInfo onChangedPlayHead; | |
Delegate changedPlayHeadHandlerDelegate; | |
void Start() | |
{ | |
Assembly assem = typeof(TimelineEditor).Assembly; | |
// TimelineWindowクラスのインスタンスを生成 | |
Type timeilneWindowType = assem.GetType("UnityEditor.Timeline.TimelineWindow"); | |
timelineWindowObj = timeilneWindowType.GetProperty("instance", BindingFlags.Public | BindingFlags.Static).GetValue(null); | |
// OnChangedPlayHeadのイベントを取得 | |
onChangedPlayHead = timeilneWindowType.GetEvent("OnChangedPlayHead"); | |
Type tDelegate = onChangedPlayHead.EventHandlerType; | |
// ハンドラーを取得 | |
MethodInfo miHandler = typeof(TimelinePlayHeadTest).GetMethod("ChangePlayHeadHandler", BindingFlags.NonPublic | BindingFlags.Instance); | |
// ハンドラーのデリゲートを作成 | |
changedPlayHeadHandlerDelegate = Delegate.CreateDelegate(tDelegate, this, miHandler); | |
// OnChangedPlayHeadのAddメソッドを取得 | |
MethodInfo addHandler = onChangedPlayHead.GetAddMethod(); | |
// ハンドラーを引数にAddメソッドを実行 | |
System.Object[] addHandlerArgs = { changedPlayHeadHandlerDelegate }; | |
addHandler.Invoke(timelineWindowObj, addHandlerArgs); | |
} | |
private void OnDestroy() | |
{ | |
// OnChangedPlayHeadのRemoveメソッドを取得 | |
MethodInfo addHandler = onChangedPlayHead.GetRemoveMethod(); | |
// ハンドラーを引数にRemoveメソッドを実行 | |
System.Object[] addHandlerArgs = { changedPlayHeadHandlerDelegate }; | |
addHandler.Invoke(timelineWindowObj, addHandlerArgs); | |
} | |
void ChangePlayHeadHandler(double oldTime, double currentTime) | |
{ | |
Debug.Log($"Changed! oldTime:{oldTime}, currentTime:{currentTime}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment