• [Unity]UI切换环形滚动效果


    一、源脚本

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. public class RollerController : MonoBehaviour
    7. {
    8. [System.Serializable]
    9. public struct RollerChildInfo
    10. {
    11. public Vector3 Position;
    12. public Vector3 Scale;
    13. public RollerChildInfo(Vector3 _pos, Vector3 _scale)
    14. {
    15. Position = _pos;
    16. Scale = _scale;
    17. }
    18. }
    19. //针对无序的RawImage,使用Sequence帮助其顺时针排序
    20. [Header("RawImage在数组中的顺序")]
    21. public int[] Sequence;
    22. [Header("RawImage数组")]
    23. public RawImage[] RawImages;
    24. [Header("RawImage属性信息,从左往右")]
    25. public RollerChildInfo[] RollerChilds;
    26. //当前的排列
    27. private int[] CurrentSequence;
    28. //是否需要更新
    29. private bool IsNeedUpdateRoller=false;
    30. void Start()
    31. {
    32. if(Sequence.Length==0|| RawImages.Length==0)
    33. {
    34. Debug.Log("请填写至少两个RawImage组件信息");
    35. return;
    36. }
    37. //
    38. CurrentSequence = new int[Sequence.Length];
    39. for (int i = 0; i < CurrentSequence.Length; i++)
    40. {
    41. CurrentSequence[i] = Sequence[i];
    42. }
    43. if(RollerChilds==null|| RollerChilds.Length==0)
    44. {
    45. //获取,信息
    46. RollerChilds = new RollerChildInfo[RawImages.Length];
    47. for(int i=0;i< RawImages.Length;i++)
    48. {
    49. if(RawImages[i]!= null)
    50. {
    51. RollerChilds[i].Position = RawImages[i].rectTransform.localPosition;
    52. RollerChilds[i].Scale = RawImages[i].rectTransform.localScale;
    53. }
    54. }
    55. }
    56. Debug.Log("RollerChilds:"+RollerChilds.Length);
    57. }
    58. // Update is called once per frame
    59. void Update()
    60. {
    61. if (IsNeedUpdateRoller)
    62. {
    63. if (Vector3.Distance(RawImages[CurrentSequence[0]].rectTransform.localPosition, RollerChilds[0].Position) > 5.0f)
    64. {
    65. for (int i = 0; i < RawImages.Length; i++)
    66. {
    67. RawImages[CurrentSequence[i]].rectTransform.localPosition = Vector3.Lerp(RawImages[CurrentSequence[i]].rectTransform.localPosition, RollerChilds[i].Position, 0.1f);
    68. RawImages[CurrentSequence[i]].rectTransform.localScale = Vector3.Lerp(RawImages[CurrentSequence[i]].rectTransform.localScale, RollerChilds[i].Scale, 0.1f);
    69. if (i != RawImages.Length - 1)
    70. {
    71. RawImages[CurrentSequence[i]].transform.SetAsFirstSibling();
    72. }
    73. else
    74. {
    75. RawImages[CurrentSequence[0]].transform.SetAsLastSibling();
    76. }
    77. }
    78. }
    79. else
    80. {
    81. //切换完成,todo
    82. IsNeedUpdateRoller = false;
    83. }
    84. }
    85. if (Input.GetKeyDown(KeyCode.LeftArrow))
    86. {
    87. Debug.Log(this.gameObject.name);
    88. StartCoroutine(MoveLeft());
    89. }
    90. else if (Input.GetKeyDown(KeyCode.RightArrow))
    91. {
    92. Debug.Log(this.gameObject.name);
    93. StartCoroutine(MoveRight());
    94. }
    95. }
    96. IEnumerator MoveLeft()
    97. {
    98. if (!IsNeedUpdateRoller)
    99. {
    100. int[] tempSequence = new int[CurrentSequence.Length];
    101. for (int i = 0; i < CurrentSequence.Length; i++)
    102. tempSequence[i] = CurrentSequence[i];
    103. for (int i = 0; i < tempSequence.Length; i++)
    104. {
    105. if (i < tempSequence.Length - 1)
    106. {
    107. CurrentSequence[i] = tempSequence[i + 1];
    108. }
    109. else
    110. {
    111. CurrentSequence[i] = tempSequence[0];
    112. }
    113. }
    114. IsNeedUpdateRoller = true;
    115. }
    116. yield return null;
    117. }
    118. IEnumerator MoveRight()
    119. {
    120. if (!IsNeedUpdateRoller)
    121. {
    122. int[] tempSequence = new int[CurrentSequence.Length];
    123. for (int i = 0; i < CurrentSequence.Length; i++)
    124. {
    125. tempSequence[i] = CurrentSequence[i];
    126. }
    127. for (int i = 0; i < tempSequence.Length; i++)
    128. {
    129. if (i == 0)
    130. {
    131. CurrentSequence[i] = tempSequence[tempSequence.Length - 1];
    132. }
    133. else
    134. {
    135. CurrentSequence[i] = tempSequence[i - 1];
    136. }
    137. }
    138. IsNeedUpdateRoller = true;
    139. }
    140. yield return null;
    141. }
    142. public void UpdateRoller(bool left)
    143. {
    144. if (!this.gameObject.activeSelf) return;
    145. if(left)
    146. {
    147. Debug.Log(this.gameObject.name);
    148. StartCoroutine(MoveLeft());
    149. }
    150. else
    151. {
    152. Debug.Log(this.gameObject.name);
    153. StartCoroutine(MoveRight());
    154. }
    155. }
    156. public int GetSelected()
    157. {
    158. return CurrentSequence[0];
    159. }
    160. public Vector3 GetFirstChildPosition()
    161. {
    162. if(RawImages != null&& RawImages[CurrentSequence[0]] !=null)
    163. {
    164. return RawImages[CurrentSequence[0]].gameObject.GetComponent().localPosition;
    165. }
    166. return new Vector3(0,0,0);
    167. }
    168. }

    二、使用方法

    1.将需要的选项排列好(以三选项为例)

            在UI上,新建Empty,改名Roller,在Roller里新加三个RawImages,将其按顺时针排序,给不同的缩放值(选中那个,永远是最前面那个,它一定要比其他的大,这样有一种视觉效果

    2.给Roller添加脚本RollerController

    其中RawImages是需要你添加已经排列好的Image,然后在Sequence里将它们排序,

           请注意,Sequence是RawImages的索引,它与RawImages的是一一对应的关系,如在UI里排在最前面的是Image1,先要看它在数组RawImages是第几个,然后在Sequence同样的索引处,将它的数值设置为0(代表它是第一个选项,右边最接近Image1的永远是最后一个,顺时针)

     3.效果如下

     

     

     

  • 相关阅读:
    【C语言】哈夫曼树,再来一次解剖
    DusQ1 dT phosphoramidite,DusQ1-dT磷酰胺
    Redis系列-Redis性能优化与安全【9】
    华为云如何实现实时音视频全球低时延网络架构
    第三章 内存管理 十二、请求分页管理方式
    Data Structure, Algorithm,and Applications in C++
    蓝桥杯 题库 简单 每日十题 day5
    BUUCTF:[GYCTF2020]FlaskApp
    Maxwell 一款简单易上手的实时抓取Mysql数据的软件
    mysql查询当天的数据
  • 原文地址:https://blog.csdn.net/qq_36251561/article/details/125907668