• 抽奖


    需求:

      奖品循环滚动(慢 > 快 > 慢 > 停)停到指定奖励的位置

    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public enum DrawType
    {
        Idle,
        Start,
        Center,
        End,
    }
    
    public class LotteryDraw : MonoBehaviour
    {
        public Transform parent;
    
        public float position_start;
        public float position_center;
        private float position_end;
    
        public float speed_start;
        public float speed_add;
        public float speed_min;
        public float speed_max;
    
        private DrawType _drawState;
        public DrawType drawState
        {
            get
            {
                return _drawState;
            }
            set
            {
                _drawState = value;
                switch (_drawState)
                {
                    case DrawType.Idle:
                        move = false;
                        break;
                    case DrawType.Start:
                        speed_move = speed_start;
                        speed_add = Mathf.Abs(speed_add);
                        move = true;
                        break;
                    case DrawType.Center:
                        dis_cur = 0;
                        GetPrePosition();
                        break;
                    case DrawType.End:
                        speed_add = -speed_add;
                        break;
                    default:
                        break;
                }
            }
        }
    
        private float speed_move;
        private float dis_max;
        private float dis_cur;
        private bool move;
    
        private List<Transform> items;
        private int itemCount = 15;
    
        //测试要滚动到指定的格子位置
        public int index;
    
        private void Awake()
        {
            items = new List<Transform>();
    
            //此处的100表示格子大小
            float position = position_start;
            position_end = position_start + 100 * itemCount;
    
            for (int i = 0; i < itemCount; i++)
            {
                Object prefab = Resources.Load("Item"); ;
                GameObject go = Instantiate((GameObject)prefab, parent);
                Transform tr = go.transform;
                Text text = tr.GetComponentInChildren<Text>();
                go.name = "Item_" + i;
                text.text = i.ToString();
                tr.localPosition = new Vector3(position, 0, 0);
                position += 100;
                items.Add(tr);
            }
        }
    
        private void Update()
        {
            //按P开始测试
            if (Input.GetKeyDown(KeyCode.P))
            {
                drawState = DrawType.Start;
            }
    
            if (move)
            {
                if (drawState == DrawType.Center)
                {
                    dis_cur += speed_move;
                    if (dis_cur >= dis_max)
                    {
                        drawState = DrawType.End;
                    }
                }
                else
                {
                    speed_move += speed_add;
                    if (speed_move >= speed_max)
                    {
                        drawState = DrawType.Center;
                    }
                    if (speed_move <= speed_min)
                    {
                        speed_move = speed_min;
                        if (Mathf.Abs(items[index].localPosition.x - position_center) < speed_min)
                        {
                            drawState = DrawType.Idle;
                        }
                    }
                }
                if (move)
                {
                    Move_Update();
                }
            }
        }
    
        private void Move_Update()
        {
            for (int i = 0; i < items.Count; i++)
            {
                items[i].Translate(Vector3.right * speed_move);
                if (items[i].localPosition.x >= position_end)
                {
                    float out_p = items[i].localPosition.x - position_end;
                    items[i].localPosition = new Vector3(position_start + out_p, 0, 0);
                }
                
                //特效:根据格子位置设置大小
                float distance = items[i].localPosition.x - position_center;
                distance = Mathf.Abs(distance);
                if (distance < 50)
                {
                    items[i].localScale = Vector3.one * 1.5f;
                }
                else if (distance < 110)
                {
                    items[i].localScale = Vector3.one * 1.2f;
                }
                else
                {
                    items[i].localScale = Vector3.one;
                }
            }
        }
    
        private void GetPrePosition()
        {
            dis_max = Mathf.Abs(position_start) + position_end;
            dis_max = dis_max / 2;
            dis_max -= items[index].localPosition.x;
            dis_max -= 400;
        }
    }
    View Code

    缺点:

      若奖励列表过长,走到指定奖励位置 表现效果(以最小速度)不佳

  • 相关阅读:
    北航算法作业三
    水库抽样
    python命名空间
    我说
    Fn键
    windows批处理运行java程序
    Java Sound初探
    java.io.IOException: mark/reset not supported
    三层交换机对链路层数据帧的处理
    北航数值分析作业三
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9821495.html
Copyright © 2020-2023  润新知