以下是更改后的代码:

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

namespace QFramework.Escape
{

    public class DiceRandomPanelData : UIPanelData
    {
        // 成功所需点数
        int successRoll;
        // 失败时触发的事件code
        string DiceDefeatEventCode;
    }
    public partial class DiceRandomPanel : UIPanel
    {
        public List<Sprite> sprites = new List<Sprite>();

        public static int RollResult = 0;
        private void Awake()
        {

        }
        public string statement;//此处为显示自身幸运&厄运(后期修改时可删除)
        public static int check = 1;

        public Image RollImage;
        public int DiceRun()//此处为幸运儿&厄运儿骰子投掷
        {
            //int[] items = { 1, 2, 3, 4, 5, 6 };
            int diceResult = Random.Range(1, 6);
            if (check == 1)
            {
                if (diceResult < 4)
                {
                    RollManage.Instance.items[5] = 1;
                }
                else
                {
                    RollManage.Instance.items[0] = 6;
                }
                check--;
            }
            diceResult = RollManage.Instance.items[Random.Range(0, 5)];
            RollImage.sprite = RollManage.RollImageDic[diceResult];
            return diceResult;
        }
        //public void tigermachine()
        //{
        //    int result1=Random.Range(1, 2);
        //    int result2= Random.Range(1, 2);
        //    int result3=Random.Range(1, 2);
        //    if(result1 == result2==result3) 
        //    {
        //        switch(result1)
        //        {
        //            case 1:
        //                item
        //        }
        //    }

        //}
        protected override void OnInit(IUIData uiData = null)
        {
            mData = uiData as DiceRandomPanelData ?? new DiceRandomPanelData();
            // please add init code here
            RollBtn.onClick.AddListener(() =>
            {
                RollImage.gameObject.SetActive(false);
                Image.SetBool('Dicing', true);
                RollResult = RollManage.Instance.items[Random.Range(0, 5)];
                // 3秒后停止骰子,并设置点数
                ActionKit.Delay(3.0f, () =>
                {
                    Image.SetBool('Dicing', false);
                    RefreshDiceImage();
                    ActionKit.Delay(2.0f, () =>
                    {
                        this.CloseSelf();
                    }).Start(this);
                }).Start(this);
            });

            StopBtn.onClick.AddListener(() =>
            {
                Image.SetBool('Dicing', false);
                RollResult = DiceRun();
            });
            RollResult = 0;
        }

        public void RefreshDiceImage()
        {
            RollImage.gameObject.SetActive(true);
            RollImage.sprite = RollManage.RollImageDic[RollResult];
        }

        protected override void OnOpen(IUIData uiData = null)
        {
            Player.Instance.Blocking = true;
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {

        }

        protected override void OnClose()
        {
            Player.Instance.Blocking = false;
        }

        // 添加以下代码
        private bool isTextFullyDisplayed;
        private int currentTextIndex;
        private string currentText;
        private bool isNextTextReady;

        public Text DialogText;
        public Button NextButton;

        private void Start()
        {
            NextButton.onClick.AddListener(OnClickNextButton);
        }

        private void Update()
        {
            if (isNextTextReady)
            {
                if (isTextFullyDisplayed)
                {
                    currentTextIndex = 0;
                    DialogText.text = '';
                    isTextFullyDisplayed = false;
                    isNextTextReady = false;
                }
                else
                {
                    DisplayNextCharacter();
                }
            }
        }

        private void OnClickNextButton()
        {
            if (!isTextFullyDisplayed)
            {
                DisplayFullText();
            }
            else
            {
                if (isNextTextReady)
                {
                    DisplayNextText();
                }
            }
        }

        private void DisplayFullText()
        {
            DialogText.text = currentText;
            isTextFullyDisplayed = true;
        }

        private void DisplayNextCharacter()
        {
            DialogText.text += currentText[currentTextIndex];
            currentTextIndex++;
            if (currentTextIndex >= currentText.Length)
            {
                isTextFullyDisplayed = true;
            }
        }

        private void DisplayNextText()
        {
            // 添加下一段对话的逻辑
        }
    }
}

代码功能解释:

  1. 逐字显示对话:

    • isTextFullyDisplayed: 表示当前对话是否已完全显示,初始为 false。
    • currentTextIndex: 记录当前显示的文字索引,初始为 0。
    • currentText: 存放当前要显示的对话内容。
    • isNextTextReady: 表示下一段对话是否准备就绪,初始为 false。
    • DialogText: 用于显示对话内容的 Text 组件。
    • NextButton: 用于触发显示下一段对话或完整对话的按钮。
  2. 点击按钮控制对话显示:

    • OnClickNextButton(): 处理点击 NextButton 事件的函数。
      • 当对话未完全显示时,点击 NextButton 会显示完整对话。
      • 当对话已完全显示且下一段对话准备就绪时,点击 NextButton 会显示下一段对话。
  3. 实现逐字显示效果:

    • DisplayNextCharacter(): 显示下一个字符并更新 currentTextIndex
    • currentTextIndex 等于 currentText.Length 时,说明对话已完全显示,isTextFullyDisplayed 设置为 true。
  4. 添加下一段对话逻辑:

    • DisplayNextText(): 在此函数中添加获取下一段对话内容的逻辑,并设置 currentTextisNextTextReady 等变量,以便切换到下一段对话。

使用说明:

  1. 将以上代码添加到你的 Unity 项目中。
  2. 在场景中添加一个 Text 组件和一个 Button 组件,分别将其赋值给 DialogTextNextButton
  3. 在你的代码中,在合适的地方调用 DisplayNextText() 函数,并设置 currentTextisNextTextReady 变量,以便切换到下一段对话。

注意事项:

  • 在使用此代码时,需要根据你的游戏逻辑自行添加获取下一段对话内容的代码。
  • 可以根据需要调整对话显示的速度,例如在 DisplayNextCharacter() 函数中使用 ActionKit.Delay() 函数控制显示速度。

示例:

// 在你需要显示对话的地方调用以下代码
private void ShowDialog()
{
    // 设置第一段对话内容
    currentText = "你好,世界!";
    isNextTextReady = true;
}

// 当用户点击 NextButton 时,调用以下代码显示下一段对话
private void DisplayNextText()
{
    // 获取下一段对话内容
    currentText = "欢迎来到我的游戏!";
    isNextTextReady = true;
}

通过以上代码,你就可以轻松地实现逐字显示对话并控制点击跳转的功能,使你的游戏对话更加生动有趣。


原文地址: http://www.cveoy.top/t/topic/phGY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录