当前位置:   article > 正文

(Unity)UI上的轮转图(3D效果)_unity3d中ui弧形轮番旋转(ui 3d旋转)

unity3d中ui弧形轮番旋转(ui 3d旋转)

轮转图也叫做轮播图

 在UI上,我们只要掌握好近大远小以及角标层级的分配,就能模拟出3D的轮转效果了。

不管是横向、纵向、斜向都能有不错的表现。 

这里以纵向代码举例:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class Circle2D : MonoBehaviour,IDragHandler,IEndDragHandler
  7. {
  8. public InputField inputf;
  9. public int num = 14;//Item数量
  10. public float offest = 10;//间距
  11. public Image prefab;//Item预制体
  12. public float max = 1;//Item最大比例
  13. public float min = 0.5f;//Item最小比例
  14. public float dec = 10;//减速度
  15. float c;//周长
  16. float r;//半径
  17. float ang;//每个角弧度
  18. float allang;//移动的总弧度
  19. public List<GameObject> list = new List<GameObject>();
  20. public List<Transform> sort = new List<Transform>();
  21. DT dt;
  22. DT dt0;
  23. void Start()
  24. {
  25. c = num * (prefab.rectTransform.sizeDelta.y+offest);
  26. r = c / (360 * Mathf.Deg2Rad);
  27. ang = 360 * Mathf.Deg2Rad / num;
  28. Move();
  29. }
  30. public void Move()
  31. {
  32. for(int i = 0; i < num; i++)
  33. {
  34. if (list.Count <= i)
  35. {
  36. list.Add(Instantiate(prefab.gameObject, transform));
  37. sort.Add(list[i].transform);
  38. list[i].transform.GetComponentInChildren<Text>().text = i.ToString();
  39. }
  40. float y = Mathf.Sin(i * ang+allang) * r;
  41. float z = Mathf.Cos(i * ang+allang) * r;
  42. float p = (z + r) / (r + r);
  43. float scale = max * (1 - p) + min * p;
  44. list[i].transform.localPosition = new Vector3(0, y, 0);
  45. list[i].transform.localScale = Vector3.one * scale;
  46. }
  47. //排序设置角标
  48. Sort();
  49. }
  50. public void OnDrag(PointerEventData eventData)
  51. {
  52. if (dt != null)
  53. {
  54. Destroy(dt.gameObject);
  55. }
  56. if (dt0 != null)
  57. {
  58. Destroy(dt0.gameObject);
  59. }
  60. allang-=eventData.delta.y/r;
  61. Move();
  62. }
  63. public void OnEndDrag(PointerEventData eventData)
  64. {
  65. float dis = eventData.delta.y;
  66. float time = Mathf.Abs(dis / dec);
  67. dt=DT.To((a) =>
  68. {
  69. allang -= a / r;
  70. Move();
  71. }, dis, 0, time).OnComlate(() =>
  72. {
  73. float moveang = Mathf.Asin(sort[num - 1].localPosition.y / r);
  74. float movetime = Mathf.Abs(moveang * r / dec);
  75. dt0=DT.To((b) =>
  76. {
  77. allang = b;
  78. Move();
  79. }, allang, allang + moveang, movetime).OnComlate(() =>
  80. {
  81. });
  82. });
  83. }
  84. public void Sort()
  85. {
  86. sort.Sort((a, b) =>
  87. {
  88. if (a.localScale.z < b.localScale.z)
  89. {
  90. return -1;
  91. }
  92. else if (a.localScale.z == b.localScale.z)
  93. {
  94. return 0;
  95. }
  96. else
  97. {
  98. return 1;
  99. }
  100. });
  101. for (int i = 0; i < sort.Count; i++)
  102. {
  103. sort[i].SetSiblingIndex(i);
  104. }
  105. }
  106. public void OnButton()
  107. {
  108. int next = int.Parse(inputf.text);
  109. Sort();
  110. //当前位置
  111. int id = list.IndexOf(sort[^1].gameObject);
  112. int n0 = id - next;
  113. int n1 = num - Mathf.Abs(n0);
  114. int n2 = n0 > 0 ? -n1 : n1;
  115. int n3 = Mathf.Abs(n0) < Mathf.Abs(n2) ? n0 : n2;
  116. //预计位置
  117. float moveang = Mathf.Asin(sort[num - 1].localPosition.y / r) + n3 * ang;
  118. float movetime = Mathf.Abs(moveang * r / dec);
  119. dt0 = DT.To((b) =>
  120. {
  121. allang = b;
  122. Move();
  123. }, allang, allang + moveang, movetime).OnComlate(() =>
  124. {
  125. });
  126. }
  127. void Update()
  128. {
  129. }
  130. }

这里用了一个简单的脚本代替DoTween

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class DT : MonoBehaviour
  6. {
  7. float starttime, nowtime, alltime;
  8. float now;
  9. Action<float> action;
  10. Action comlate;
  11. bool isplay = true;
  12. void Start()
  13. {
  14. }
  15. public static DT To(Action<float>action,float starttime,float nowtime,float alltime)
  16. {
  17. GameObject obj = new GameObject("Dt");
  18. DT self = obj.AddComponent<DT>();
  19. self.starttime = starttime;
  20. self.nowtime = nowtime;
  21. self.alltime = alltime;
  22. self.now = Time.time;
  23. self.action = action;
  24. return self;
  25. }
  26. public DT OnComlate(Action comlate)
  27. {
  28. this.comlate = comlate;
  29. return this.GetComponent<DT>();
  30. }
  31. void Update()
  32. {
  33. if (Time.time - now < alltime)
  34. {
  35. float p = (Time.time - now) / alltime;
  36. float c = (1 - p) * starttime + nowtime * p;
  37. if (action != null)
  38. {
  39. action(c);
  40. }
  41. }
  42. else if(isplay)
  43. {
  44. isplay = false;
  45. if (action != null)
  46. {
  47. action(nowtime);
  48. }
  49. if (comlate != null)
  50. {
  51. comlate();
  52. }
  53. Destroy(gameObject);
  54. }
  55. }
  56. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/606797
推荐阅读
相关标签
  

闽ICP备14008679号