(051)Raywenderlich: 怎么用Timeline讲一个笑话

本文详细介绍了如何利用Unity2020.1.6f1和Timeline创建一个自定义对话系统,实现逐字显示笑话内容,并通过信号系统控制节奏。首先设置场景,包括UI元素和喜剧人物,然后编写Typewriter脚本实现逐字显示功能。接着讲解Timeline的使用,包括创建TimelineAsset、TimelineTrack和PlayableDirector,以及激活物体。进一步,通过SignalEmitter、SignalReceiver和SignalAsset实现信号发送和接收,控制笑话显示的节奏。最后,创建JokeTrack并在Timeline中添加JokeMarker,实现不同时间点的笑话内容展示。

版本

Unity 2020.1.6f1

资源

下载资源

学习内容

1.什么是 Timeline
2.怎么用 Timeline 触发自定义事件?
3.怎么实现一个自定义对话系统?

设置场景

1.打开 JokeMaching 场景。
2.添加 UI - Image 组件,并命名为 JokeWindow
3.设置 JokeWinodw 的下列属性:

  • Source Image : 为 ChatBubble

  • Colorx = 80, y = 0, z = 80。

  • width = 1000, Height = 600。

  • Pos X = 90,Pos Y = 160。
    4.确认 Packages 中,安装了
    在这里插入图片描述
    5.为 JokeWindow 添加子组件 UI - Text - TextMeshPro ,并命名为 JokeText。设置 JokeText 的属性:

  • Width = 600, Height = 200。

  • font Size = 50, Vertex Color 为白色。

  • 设置字体居中。
    在这里插入图片描述

  • Text Input 添加文字:

    There's 10 types of people in the world.
    Those who know binary.
    and those who don't.
    

    在这里插入图片描述

添加喜剧人物

1.添加 UI - Image,并命名为 Comedian。设置 Comedian 的属性为:

  • Source ImageComedian
  • Width = 400,Height = 400。
  • Pos X = -390,Pos Y = -160。
    在这里插入图片描述
    2.选择 Canvas,设置屏幕的缩放比例
  • UI Scale ModeScale With Screen Size
  • Reference Resolution: x = 1280, y =720。
  • Screen Math Mode: Expand
    在这里插入图片描述
    运行游戏:
    在这里插入图片描述

逐字显示

1.Assets / Scripts 新建脚本 :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Typewriter : MonoBehaviour
{
    private TMP_Text _textBox;

    private float _timePerCharactor = 0.05f;

    private void Awake()
    {
        _textBox = GetComponent<TMP_Text>();
        // 所有字都在一行内
        _textBox.enableWordWrapping = true;
        // 隐藏字体
        _textBox.maxVisibleCharacters = 0;
    }

    private void OnEnable()
    {
        StartCoroutine(Reveal(0));
    }
    
    public IEnumerator Reveal(int startLetterIndex)
    {
        _textBox.ForceMeshUpdate();

        int totalVisibleCharacters = _textBox.textInfo.characterCount;

        for (int i = startLetterIndex; i < totalVisibleCharacters; i++)
        {
            _textBox.maxVisibleCharacters = i + 1;
            yield return new WaitForSeconds(_timePerCharactor);
        }

        _textBox.maxVisibleCharacters = totalVisibleCharacters;
    }
}

2.挂载到 JokeText 物体上。

理解Timeline

  1. Timeline Asset:通过 Timeline Window 创建的文件。
  2. Timeline Track:一系列相似的动画帧。
  3. Playable Director GameObjectTimeline AssetGameObject 的纽带。
  4. Signal Emitter
  5. Signal Receiver
  6. Signal Asset

准备材料

1.创建一个空的 GameObject,命名为 Timeline
2.Window - Sequencing - Timeline,打开 Timeline Window 窗口。
3.选中步骤一新建的物体 Timeline,点击 Timeline Windowcreate 按钮,将新建的文件命名为 JokeTimeline,并保存到 Assets / RW / Timeline。勾选 Play On Awake
在这里插入图片描述

激活物体

1.拖动 ComedianTimeline Window 的左侧窗体,选择 Add Activation Track
在这里插入图片描述
2.点击 Timeline Window 右上角的设置按钮,选择 Seconds。设置 Comedian Clip 的在 [0:00 - 20:00] 之间。
在这里插入图片描述
在这里插入图片描述
2.对 JokeWindow 重复上面的操作,动画帧设置在 [2:00 - 18:00]。
3.运行游戏,查看效果。

Timeline 节奏控制:信号发送

1.信号系统组成包括三个部分:

  • Signal Emitter
  • Signal Receiver
  • Signal Asset
    2.在 Typewriter.cs 文件中,添加类:
public class Typewriter
{
	...
}

public class Dialog
    {
        // 要发送的文本
        public string Quote;
        // 控制每个字母出现的间隔
        public float PausePerLetter;
        // 控制是否开启新的对话
        public bool NewPage;
        // 控制是否开启新行
        public bool NewLine;
    }

3.在 Assets / RW / Scripts 中,新建脚本 JokeMarker.cs

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class JokeMarker : Marker, INotification
{
    [SerializeField] private string quote = "";

    [SerializeField] private float pausePerLetter = 0.1f;

    [SerializeField] private bool startNewPage;

    [SerializeField] private bool startNewLine;

    public string quote1 => quote;

    public float pausePerLetter1 => pausePerLetter;

    public bool startNewPage1 => startNewPage;

    public bool startNewLine1 => startNewLine;

    public PropertyName id => new PropertyName();
}

2.在 Typewriter.cs 中,增加 函数:

...
public class Typewriter
{
	...
	 public void AddDialog(Dialog message)
    {
        
    }
	...
}

3.挂载 JokeReceiver.cs 脚本到 JokeText,并且将 Typewriter 分配到 JokeReceiver - Dialog Animator字段上。
在这里插入图片描述

信号处理

1.修改 Typewriter.cs 脚本的函数 AddDialog 函数:

 public void AddDialog(Dialog message)
    {
         _timePerCharactor = message.PausePerLetter;

        if (message.NewPage)
        {
            _textBox.maxVisibleCharacters = 0;
            _textBox.text = message.Quote;
            StartCoroutine(Reveal(0));
        }
        else
        {
            int currentLetterIndex = _textBox.maxVisibleCharacters;

            if (message.NewLine)
            {
                _textBox.text = string.Concat(_textBox.text, "\n", message.Quote);
            }
            else
            {
                _textBox.text = string.Concat(_textBox.text, " ", message.Quote);
            }

            StartCoroutine(Reveal(currentLetterIndex));
        }
    }

接收信号

1.在 Assets / RW / Scripts 中,新建脚本 JokeReceiver.cs

using UnityEngine;
using UnityEngine.Playables;

public class JokeReceiver : MonoBehaviour, INotificationReceiver
{
    [SerializeField] private Typewriter dialogAnimator;

    public void OnNotify(Playable origin, INotification notification, object context)
    {
        if (notification is JokeMarker dialog && dialogAnimator != null)
        {
            var newdialog = new Dialog()
            {
                Quote = dialog.quote,
                PausePerLetter = dialog.pausePerLetter,
                NewPage = dialog.startNewPage,
                NewLine = dialog.startNewLine
            };

            dialogAnimator.AddDialog(newdialog);
        }
    }
}

2.删除 TypewriteronEnable 函数。

创建 JokeTrack

1.在 Assets / RW / Scripts 中,新建脚本 JokeTrack.cs

using UnityEngine.Timeline;

[TrackBindingType(typeof(JokeReceiver))]
[TrackColor(255f/255f, 140f/255f, 0f/255f)]
public class JokeTrack : MarkerTrack
{
}

开始讲笑话

1.打开 Asssets / Rw / Timeline / JokeTimeline.playable,选中 Timeline 物体。
2.拖动 JokeTextTimeline window 的左侧窗体,选择 Add Joke Track
3.点击 Timeline window3:00 秒的标记点,右键 JokeTrack ,并且选择 Add Joke Marker
在这里插入图片描述
3:00 秒标记:

  • Quote : There’s 10 types of people in the world.
  • Pause Per Letter: 0.05
  • Start New Page: true
  • Start New Line: false

7:00 秒标记:

  • Quote : Those who know binary,
  • Pause Per Letter: 0.1
  • Start New Page: false
  • Start New Line: true

11:00 秒标记:

  • Quote : and those who don’t!
  • Pause Per Letter: 0.01
  • Start New Page: fale
  • Start New Line: true

14:00 秒标记:

  • Quote : 01 01 01 01 01 01 01 01
  • Pause Per Letter: 0.03
  • Start New Page: true
  • Start New Line: false

在这里插入图片描述

4.运行游戏。

[1] how-to-tell-a-joke-with-unity-timeline

「LLM那些事」系列第 4 篇《上下文窗口的边界》,文章连接:https://blog.csdn.net/houwenjin/article/details/163999753。 演示什么:在「预测」Sheet 的黄色格子里输入一句话(默认「来泡一杯」),四个「模型」——分别只统计最后 1 / 2 / 3 / 4 个字的 n-gram 查表——同时预测下一个字。同一个输入,看的上下文越长,候选越少、预测越确定: ┌────────────────┬──────────┬───────────────┬──────┐ │ 只看最后几个字 │ 用的前缀 │ 候选下一字数 │ 预测 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 1 个 │ 杯 │ 3(茶/子/水) │ 模糊 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 2 个 │ 一杯 │ 2(茶/水) │ 收窄 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 3 个 │ 泡一杯 │ 1(茶) │ 确定 │ ├────────────────┼──────────┼───────────────┼──────┤ │ 4 个 │ 来泡一杯 │ 1(茶) │ 确定 │ └────────────────┴──────────┴───────────────┴──────┘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值