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;
                }
            }
        }

    }

在这里插入图片描述