之前用lua撸了一个转盘,为了方便以后用,今天撸一个C#版的转盘,主体思路一致,便于展示用U3D拼了一版简易UI

代码来了!

  1 using UnityEngine;
  2 using UnityEngine.UI;
  3
  4 public class NewBehaviourScript : MonoBehaviour
  5 {
  6     private int col = 5;
  7     private int row = 5;
  8     public Transform[] Col0;
  9     public Transform[] Col1;
 10     public Transform[] Col2;
 11     public Transform[] Col3;
 12     public Transform[] Col4;
 13     public Transform[] ColPos;
 14     public int Speed;
 15     private int[] colState = new int[5]{2,2,2,2,2}; //1旋转 2停止 3即将停止
 16     private int[] colDelay = new int[5];
 17     private int startPos = 33;
 18     private int endPos = -30;
 19     private Transform[][] Items;
 20     public Button Btn_Start;
 21     public Button Btn_Stop;
 22     private Object[] list_Sprites;
 23
 24     // Start is called before the first frame update
 25     void Start()
 26     {
 27         Items = new Transform[][]{Col0,Col1,Col2,Col3,Col4};
 28         Btn_Start.onClick.AddListener(()=>{
 29             StartSpin();
 30             Btn_Start.gameObject.SetActive(false);
 31             Btn_Stop.gameObject.SetActive(true);
 32             Btn_Stop.interactable = true;
 33         });
 34
 35         Btn_Stop.onClick.AddListener(()=>{
 36             StopSpin();
 37         });
 38
 39         list_Sprites = Resources.LoadAll<Sprite>("Sprites");
 40         for (int i =0; i<col;i++){
 41             for (int j=0; j<row;j++){
 42                 Items[i][j].GetComponent<Image>().sprite = (Sprite)list_Sprites[(int)Random.Range(0,list_Sprites.Length-1)];
 43             }
 44         }
 45     }
 46
 47     // Update is called once per frame
 48     void Update()
 49     {
 50         for (int i = 0; i< col; i++){
 51             if (colState[i] == 1){
 52                 ColPos[i].localPosition = new Vector3(ColPos[i].localPosition.x,ColPos[i].localPosition.y - Speed,ColPos[i].localPosition.z);
 53                 if (ColPos[i].localPosition.y <= endPos){
 54                     ColPos[i].localPosition = new Vector3(ColPos[i].localPosition.x,startPos,ColPos[i].localPosition.z);
 55                 }
 56                 for (int j = row -1;j>=1;j--){
 57                     Items[i][j].GetComponent<Image>().sprite = Items[i][j-1].GetComponent<Image>().sprite;
 58                 }
 59                 Items[i][0].GetComponent<Image>().sprite = (Sprite)list_Sprites[(int)Random.Range(0,list_Sprites.Length-1)];
 60                 if (colDelay[i] >= 0)
 61                 {
 62                     colDelay[i]--;
 63                     if (colDelay[i] <= 0)
 64                     {
 65                         colState[i] = 3;
 66                     }
 67                 }
 68             }
 69             else if (colState[i] == 3){
 70                 ColPos[i].localPosition = new Vector3(ColPos[i].localPosition.x,ColPos[i].localPosition.y - Speed,ColPos[i].localPosition.z);
 71                 ColPos[i].localPosition = new Vector3(ColPos[i].localPosition.x,startPos,ColPos[i].localPosition.z);
 72                 for (int j = row -1;j>=1;j--){
 73                     Items[i][j].GetComponent<Image>().sprite = Items[i][j-1].GetComponent<Image>().sprite;
 74                 }
 75                 Items[i][0].GetComponent<Image>().sprite = (Sprite)list_Sprites[(int)Random.Range(0,list_Sprites.Length-1)];
 76                 colState[i] = 2;
 77                 if (i == 4){
 78                     Btn_Start.gameObject.SetActive(true);
 79                     Btn_Stop.gameObject.SetActive(false);
 80                 }
 81             }
 82         }
 83     }
 84
 85     void StartSpin()
 86     {
 87         for (int i =0; i<col ;i++)
 88         {
 89             colState[i] = 1;
 90             colDelay[i] = 60 + i * 20;
 91         }
 92     }
 93
 94     void StopSpin()
 95     {
 96         for (int i = 0; i < col; i++)
 97         {
 98             colDelay[i] = i * 20;
 99         }
100         Btn_Stop.interactable = false;
101     }
102 }
02-10 14:16