Unityトラマシンのスクロール抽選効果を実現するコード例


直接効果図を見てみましょう。
choujiang
作成の考え方:
4枚の写真を設計して、5つの点、各写真は同時に下の点に移動して、最後の一つまで0番の点に戻ります。これで循環します。
1.2
シーン構築:
  • テレビ枠の背景としてImageを作成する。
  • は、Image名Maskを作成し、テレビ枠の内容としてMaskコンポーネントを追加してカバー枠を表示する。
  • は、スクロール画像として4つのImageを作成する。
  • 抽選開始ボタンを作成します。
  • 1.3
    PS:実際のプロジェクトでは、必要に応じて写真の表示を動的に修正して、賞品の内容を毎回抽選します。
    ソース共有:
    
    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class ScollToDraw : MonoBehaviour
    {
          //     
          public Button DrowBtn;
          
          //     
          public Image[] ArardImgArr;
    
          //     
          public float AniMoveSpeed = 3f;
    
          //   
          private float[] progress = new[] {0f, 1f, 2f, 3f, 4f};
    
          //       
          private Vector3[] AniPosV3 = new[]
                {Vector3.up * 240, Vector3.up * 120, Vector3.zero, Vector3.down * 120, Vector3.down * 240};
    
          //       
          private bool isAutoStop;
          //            UI
          private bool isStopUpdatePos;
          
          void Start()
          {
                DrowBtn.onClick.AddListener(DrawFun);
                isAutoStop = false;
                isStopUpdatePos = false;
          }
    
          void Update()
          {
                if (isStopUpdatePos) return;
                
                float t = Time.deltaTime * AniMoveSpeed;
                for (int i = 0; i < ArardImgArr.Length; i++)
                {
                      progress[i] += t;
                      ArardImgArr[i].transform.localPosition = MovePosition(i);
                }
          }
          
          //            
          Vector3 MovePosition(int i)
          {
                int index = Mathf.FloorToInt(progress[i]);
                if (index > AniPosV3.Length - 2)
                {
                      //       ,       0
                      progress[i] -= index; 
                      index = 0;
                      //    2    ,   0      
                      if (i == 2 && isAutoStop)
                      {
                            isStopUpdatePos = true;
                            Debug.Log("      ...");
                            // todo...        
                      }
                      return AniPosV3[index];
                }
                else
                {
                      return Vector3.Lerp(AniPosV3[index], AniPosV3[index + 1], progress[i] - index);
                }
          }
          
          /// <summary>
          ///     
          /// </summary>
          void DrawFun()
          {
                isAutoStop = false;
                isStopUpdatePos = false;
                StartCoroutine(SetMoveSpeed(2));
                // DoTween       
                // Transform tran = DrowBtn.transform;
                //tran.DOLocalMoveY(-60, 0.2f).OnComplete(() =>
                //{
                //      tran.DOLocalMoveY(50, 0.2f);
                //
                //});
          }
          
          //         
          IEnumerator SetMoveSpeed(int time)
          {
                AniMoveSpeed = 10;
                yield return new WaitForSeconds(time);
                AniMoveSpeed = 1;
                yield return new WaitForSeconds(time);
                isAutoStop = true;
          }
    }
    ここでUnityがトラマシンのローリング抽選の効果を実現するための例示的なコードについての記事を紹介します。Unityトラマシンのローリング抽選の内容については、以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。