【游戏开发】TimelinePlayable自定义_弹幕游戏随笔

在制作弹幕游戏的时候,使用了Playable自定义的TimeLine。

小熊的上一篇完全理解笔记:https://blog.csdn.net/qq_34013247/article/details/120472131

本篇教程是上一篇的补充(感谢读者的私信,让我们一起变得更好💪)。


获得PlayableDirector组件:

我们在上一篇的笔记中讲到了如下代码:

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public class LightControlBehaviour : PlayableBehaviour {
    //public Light light = null; 不再需要它了
    public Color color = Color.white;
    public float intensity = 1f;
    public PlayableDirector playableDirector;
    public TimelineClip clip;
    double startTime = 0f;
    double totalTime = 0f;
    public override void ProcessFrame(Playable playable, FrameData info, object playerData) {
        Light light = playerData as Light; // 这个地方有变化
        if (light != null) {
            light.color = color;
            light.intensity = intensity;
        }
        //使用下面到判断来在pasue自带的暂停之前来停下TimeLine
        if (playable.GetDuration() - playable.GetTime() < 0.083333f) {
            //playableDirector.time -= 0.08f;
            playableDirector.time = startTime;
#if InDebugMode
            Debug.Log("手动暂停");
#endif
        }
    }
    public override void OnBehaviourPlay(Playable playable, FrameData info) {
        base.OnBehaviourPlay(playable, info);
        startTime = playableDirector.time;
        Debug.Log("开始");
    }
}


其中对于第8行的playableDirector我们是怎么去获取的呢?
其实,我们是在对应的Asset文件里面,在创建的时候,进行的获取。
CreatePlayable函数的第二个参数是owner,然后我们使用,owner.GetComponent<PlayerDirector>()👈代码自己打的,不保证拼写正确,小熊英语炒鸡无敌烂。

不方便,对话框的代码进度条需要很短

解决方法很简单,那就是当已经按到最后的时候,直接把PlayerDirector的Time直接跳到最后就可以啦!

新思路,将TimeLine上面的Asset东西变成屏幕中的文字。

(1)当时间播放到66的时候:在这里插入图片描述
TimeLine播放到了第二行的那个小方块上面,小方块的信息如下:在这里插入图片描述
(2)当TimeLine播放到186的时候:
在这里插入图片描述
第二行的第二个小方块(Asset)的信息:在这里插入图片描述

(3)播放到309的时候:
在这里插入图片描述
同上,第二行的第三个Asset的信息
(4)当处于间隙的时候:
在这里插入图片描述
可以看到屏幕上面是没有文字的
这是因为在TimeLine的最上面的初始化轨道,(就是第一行唯一的长块)它的值是null
我们知道,在Timeline中规定下面的轨道优先级大于上面的轨道,所以当我们在第二行放入Asset就会自动覆盖掉null,这样就会有文字啦!

参考代码:

这个小功能实在太简陋了。
毕竟是用来记笔记用的,那么更高级的想法就留给读者了。

首先我在场景中放入了一个UI,然后又放入了一个Text在UI下面,使用一个GameObject(DialogShowControl)和一个MonoBehaviour(ShowDialog)来实时的刷新UI的Text。在这里插入图片描述

Track:

DialogControlTrack.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
[TrackClipType(typeof(DialogControlAsset))]
[TrackBindingType(typeof(ShowDialog))]
public class DialogControlTrack : TrackAsset {
}

在上一篇教程我们已经详细说过,这个文件不需要挂在场景的物体上面。就这样然后保存代码就可以了。

Asset:

DialogControlAsset.cs

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

[System.Serializable]
public class DialogControlAsset : PlayableAsset {
    public string str;
    // Factory method that generates a playable based on this asset
    public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {
        var scriptPlayable = ScriptPlayable<DialogControlBehaviour>.Create(graph);
        var scriptBehaviour = scriptPlayable.GetBehaviour();
        scriptBehaviour.str = str;
        return scriptPlayable;
    }
}

Behavior:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

// A behaviour that is attached to a playable
public class DialogControlBehaviour : PlayableBehaviour {
    public string str;
    public override void ProcessFrame(Playable playable, FrameData info, object playerData) {
        ShowDialog showDialog = playerData as ShowDialog;
        if (showDialog != null) {
            showDialog.str = str;
        }
    }
}

其实和上一篇根本就没什么区别hhhhh

ShowDialog.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShowDialog : MonoBehaviour {
    public Text text;
    public string str;

    void Update() {
        text.text = str;
    }
}

最近学校事情炒鸡多
写得比较简陋
请见谅

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值