自用Unity剧情系统

本文介绍了如何在Unity中创建自用剧情系统,包括剧本的存储方式、最终展示效果以及利用Lua进行交互控制的实现细节,展示了类似文字游戏的交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要展示剧情部分

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TextViewer : PlotTexts
{
  public GameObject PlotShower,PlotChooser;
  public Image CG;
  public Text Name, Talk;

  private void Awake()
  {

  }

  private void Update()
  {
    if (Plots.Count>0)
    {
      PlotShower.SetActive(true);
      Ploting();
    }
    else
    {
      PlotShower.SetActive(false);
    }
  }

  public void Ploting()
  { 
    Debug.Log(Plots.Count);
    CG.sprite = Plots[0].TalkCg;
    Name.text = Plots[0].Name;
    Talk.text = Plots[0].Say;
    if (Plots.Count>0&&Plots[0].PlotEvent==0)
    {    
      PlotChooser.SetActive(false);
      if (Input.GetMouseButtonDown(0))
      {
        Plots.RemoveAt(0);
      }
    }
    else if (Plots[0].PlotEvent!=0)
    {
      PlotEvents(Plots[0].PlotEvent);
    }

   
  }

  public void PlotEvents(int index)
  {
    Choose('A').onClick.RemoveAllListeners();
    Choose('B').onClick.RemoveAllListeners();
    
    switch (index)
    {
      case 1:
        PlotChooser.SetActive(true);
      Choose('A').onClick.AddListener(() =>
        {
          Plots.RemoveAt(0);
          SceneManager.LoadScene("Battle");
        
        });
        Choose('B').onClick.AddListener(() =>
        {
          Plots.RemoveAt(0);
          Plots.Add(new Plot(){PlotEvent = 0,Say = "测试剧情选项B",Name = "测试人"});
          Plots.Add(new Plot(){PlotEvent = 0,Say = "测试剧情选项B第二句",Name = "测试人"});
         
        });
        break;
      case -1:
      {
        if (Input.GetMouseButtonDown(0))
        {
          Plots.RemoveAt(0);
          SceneManager.LoadScene("Main");
        }
      }
        break;
      case -2:
      {
        if (Input.GetMouseButtonDown(0))
        { Plots.RemoveAt(0);
          SceneManager.LoadScene("Main");
        }
      }
        break;
    }

    Button Choose(char finder)
    {
      if (finder=='A')
      {
        return PlotChooser.transform.Find("A").GetComponent<Button>();
      }
      else
      {
        return PlotChooser.transform.Find("B").GetComponent<Button>();
      }
    }
  }


}

剧本存储部分

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

public class PlotTexts : MonoBehaviour
{
    public struct Plot
    {
        public int PlotEvent;
        public string Say;
        public string Name;
        public Sprite TalkCg;
   
    }
      
    public static List<Plot> Plots=new List<Plot>();

    public static void PlotAdder(int index)
    {
        switch (index)
        {
            case 1:    Plots.Add(new Plot(){PlotEvent = 0,Say 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值