[Unity implements chat] TimeLine implements virtual camera turning to NPC and pops up a dialogue

Effect:

1: Create TimeLine

1: Click the npc with the mouse, click Window-Sequencing-Timeline in the Unity menu bar , and save the playable file to the current project.

2: Drag the virtual camera of the npc sub-object to the timeline, and select Add Activation Track to display the camera.

Afterwards, the NPC will add the following components on its own, remember to uncheck:

This director component can help us control playback or pause the timeline

Note that we hide this virtual camera first . It is specifically looking at NPCs . The specific parameters can be adjusted by yourself~

Two: Hang the script on the NPC <NPC>

1: Implement the interface ITimeControl

There will be the following methods for us to use:

At this time, we need to right-click on  the npcTimeline - AddControlTrack and drag the npc into it.

When the pointer reaches the starting position of the NPC in the second row, the three methods implemented by the interface above will be executed , and the UI can be controlled to realize various functional interactions~

Specific execution sequence:

...omitting a lot of time in between...

2: Declare variables

Declare the director class PlayableDirector, the chat box prefab chat, the chat Text, the text display speed speed, the parent object btnbox that instantiates the answer button, and the chat content

How to play the timeline we created?

We can write collision detection. When the player reaches near the npc, it will use the director class director to play.

do you remember? This director controls how we display the virtual camera!

 3: Specific implementation:

OnControlTimeStart method,

 public void OnControlTimeStart()
    {
        print("start");

        if (chat == null)
        {
            //实例化聊天预制体
            chat = Instantiate(Resources.Load<GameObject>("Chat"), GameObject.Find("chatPos").transform);
           //设置icon
            chat.transform.Find("Image").GetComponent<Image>().sprite = Resources.Load<Sprite>("npc");
            chat.transform.Find("nickNameText").GetComponent<Text>().text = "小美:";
          //设置具体显示文字聊天框
            label = chat.transform.Find("contentText").GetComponent<Text>();
           //生成回复按钮的地方
            btnbox = chat.transform.Find("box");
          
        }
    }

 setTime method:

  public void SetTime(double time)
    {
        print("settime"+time);
        //逐渐展示npc对话内容,也就是一个一个字往外蹦跶
        if ((int)(time * speed) < content.Length)
        {
            label.text = content.Substring(0, (int)(time * speed));
        }
        else

        {
            label.text = content;
        }
        
    }

 OnControlTimeStop method

 public void OnControlTimeStop()
    {
        print("stop");
        director.Pause();
        //生成button
        GameObject btn = Instantiate(Resources.Load<GameObject>("Button"), btnbox);
       //设置回复内容
        btn.GetComponentInChildren<Text>().text = "那就看看吧";
       //按下用来回复的按钮
        btn.GetComponent<Button>().onClick.AddListener(()=> 
        {
            //删掉聊天内容
            if (chat != null)
            {
                Destroy(chat.gameObject);
                chat = null;
                label = null;
            }
            //实例化商店(下一篇文章再写)
            Instantiate(Resources.Load<GameObject>("Shop"), GameObject.Find("Canvas").transform);

        });
    }

 Three: Run to achieve the effect

Please listen to the next chapter to explain the various effects of the store~

Guess you like

Origin blog.csdn.net/m0_74022070/article/details/130640655
Recommended