LoginSignup
4
4

More than 5 years have passed since last update.

Unity2017.1b4のタイムラインでScriptを動かす

Last updated at Posted at 2017-05-11

雑メモです。

噂のTimelineをUnity2017.1のベータ4で触ってみたんですが、なんかどんどん仕様が変わってるようで、ダウンロードしたサンプルコードが動かなかったり、ネット上に正しい情報も見つからないので、雑な作業メモを残しておきます。(が、もう2017.1b5が出ていたので、すでに仕様が変わっているかもしれない。)

以下、色々やってみて動いたタイムライン上からscriptを動かすやり方。

1) TimeLineアセットを作成。
2) PlayableDirectorコンポーネントを、適当なゲームオブジェクトに適用。TimeLineアセットを紐づける。
3) PlayableBehaviourを継承したHogePlayableBehaviourを作ってタイムラインからのイベントを処理するコードを書く。
4) PlayableAssetを継承したHogePlayableAssetを作る。インスペクタで設定したいパラメータがあればExposedReferenceで宣言する。HogePlayableBehaviourをインスタンス化する。パラメータを受けわたすコードを書く。ScriptPlayableを作成して返す。
5) TimeLineにPlayableTrackを追加。
6) PlayableTrackにHogePlayableBehaviourを配置する。HogePlayableBehaviourのインスペクタでのパラメータがあれば設定する。

HogePlayableAsset.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

[System.Serializable]
public class HogePlayableAsset : PlayableAsset
{
    public ExposedReference<UnityEngine.UI.Text> textField; // テキストフィールドを受け渡してみる
    public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    {
        var b = new HogePlayableBehaviour();
        b.textField = textField.Resolve(graph.GetResolver());
        return ScriptPlayable<HogePlayableBehaviour>.Create(graph, b);
    }
}
HogePlayableBehaviour.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class HogePlayableBehaviour : PlayableBehaviour
{
    public UnityEngine.UI.Text textField;

    public HogePlayableBehaviour()
    {
    }

    public override void OnGraphStart(Playable playable) {
    }

    public override void OnGraphStop(Playable playable) {
    }

    public override void OnBehaviourPlay(Playable playable, FrameData info) {
    }

    public override void OnBehaviourPause(Playable playable, FrameData info) {
    }

    public override void PrepareFrame(Playable playable, FrameData info) {
        if(textField) textField.text = "deltaTime : " + info.deltaTime;
    }
}

タイムライン自体は良い感じのですが、どうも使うまでの手順が面倒ですねえ。元Flasherからすると、シーンに置いたオブジェクトが自動でタイムラインに乗っちゃう勢いでも構わないんですが。(そう言うエディタ拡張を作れと言う話?)

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4