Unity 点击 随机选择

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomSelect : MonoBehaviour {
    Button btn;
    public Text text;
    public  GameObject coverImage;
    public  Transform[] items;
    public ParticleSystem ps;
    void Start () {
        btn = GetComponent<Button>();
            btn.onClick.AddListener(StartRandom);
        ps.Stop();
    }
    int index = 0;
    void Update () {
        if(isRandoming)
        {
            if(randomTime>0)
            {
                //Debug.Log(randomTime);
                randomTime -= Time.deltaTime;
                if(randomTime<0)
                {
                    ps.Play();
                }
                stepTime -= Time.deltaTime;
                if(stepTime<0)
                {
                    if (index == items.Length)
                        index = 0;
                    coverImage.transform.position = items[index].transform.position;
                    text.text = items[index].transform.GetChild(0).GetComponent<Text>().text;
                    index++;
                    stepTime = randomTime > 4 ? 0.05f : fadeTime += 0.1f;
                    Debug.Log(stepTime);
                }
            }
            else
            {
                isRandoming = false;
            }
        }
    }
    //结尾处走的慢点,间隔时间长点
    float fadeTime = 0.05f;
    float stepTime = 0.1f;
    bool isRandoming = false;
    float randomTime = 0;
    void StartRandom()
    {
        ps.Stop();
        coverImage.SetActive(true);
        isRandoming = true;
        randomTime = Random.Range(7, 9);
        fadeTime = 0.05f;
    }
}

自己的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RadomDraw : MonoBehaviour
{
    float maxTime = 6;
    float time = 0.5f;
    public GameObject obj;
    public Button start;
    public Transform image;
    bool Operation = false;
    public List<Transform> my_Son = new List<Transform>();
    public Text name;
    void Start()
    {
        start.onClick.AddListener(Rad);
        for (int i = 0; i < transform.childCount; i++)
        {
            my_Son.Add(transform.GetChild(i));
        }
        obj.SetActive(false);
    }
    void Rad()
    {
        Operation = true;
    }
    Transform oneposition;
    void Update()
    {
        name.text = image.parent.GetChild(0).name.ToString();
        if (Operation)
        {
            obj.SetActive(true);
            maxTime -= Time.deltaTime;
            for (int i = 0; i < my_Son.Count; i++)
            {
                if (maxTime > 3f)
                {
                    time -= Time.deltaTime;
                    if (time <= 0)
                    {
                        image.SetParent(my_Son[i]);
                        image.localPosition = Vector3.zero;
                        time = 0.5f;
                    }
                }
                else if (maxTime > 0 && maxTime < 3f)
                {
                    time -= Time.deltaTime;
                    if (time <= 0)
                    {
                        Debug.Log("执行了");
                        image.SetParent(my_Son[i].transform);
                        image.localPosition = Vector3.zero;
                        time = 1.2f;
                    }
                }
                else if (maxTime <= 0)
                {
                    //time = maxTime > 4 ? 0.2f : time + 0.2f;
                    obj.SetActive(false);
                    Operation = false;
                    maxTime = 6;
                }
            }
        }

    }

在这里插入图片描述

### 实现 Unity 中按钮点击随机显示文本功能 为了实现在 Unity 中当按钮被点击随机显示一段文本的功能,可以利用 `UnityEngine.UI.Button` 组件以及 `System.Collections.Generic.List<T>` 来存储可能的文本选项。下面是一个具体的实现方法。 #### 创建场景并准备资源 1. 在 Unity 编辑器中创建一个新的 UI Button 和 Text 元素。 2. 将这些元素放置在一个 Canvas 下面以便它们能在屏幕上正确渲染出来。 #### 脚本编写 接下来,在项目文件夹下新建 C# Script 文件命名为 `RandomTextDisplayer.cs` 并将其挂载到任意 GameObject 上: ```csharp using UnityEngine; using UnityEngine.UI; public class RandomTextDisplayer : MonoBehaviour { public Button button; // 按钮组件引用 public Text textDisplay; // 文字展示区域引用 private List<string> texts = new List<string> { "Hello, World!", "Welcome to the game.", "This is a random message." }; // 可选的文字列表 void Start() { if (button != null && textDisplay != null) { button.onClick.AddListener(DisplayRandomText); } else { Debug.LogError("Button or Text component not assigned."); } } void DisplayRandomText() { int index = Random.Range(0, texts.Count); // 获取一个介于 0 到 texts 数组长度之间的随机索引 string selectedText = texts[index]; // 使用该索引来选取一条消息 textDisplay.text = selectedText; // 更新 UI 的文字内容为所选的消息 } } ``` 此脚本定义了一个名为 `texts` 的字符串列表来保存所有可作为候选的信息条目,并通过 `Start()` 方法初始化监听事件,使得每次按下指定按钮都会调用 `DisplayRandomText()` 函数执行一次新的随机选择操作[^1]。 #### 设置 Inspector 参数 回到 Unity Editor 后,请确保将上述脚本中的 `button` 字段和 `textDisplay` 字段分别拖拽关联至对应的 UI 控件实例上。 这样就完成了一个简单的例子,每当用户单击按钮时就会从预设好的几句话里挑选一句不同的句子显示给玩家看。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值