当前位置:   article > 正文

Unity图片轮播图功能实现_unity 按钮轮播图效果

unity 按钮轮播图效果

通过Unity+CurvedUI实现轮播效果,思路是将几张UI图按照较准确的位置放在空物体(作为父物体)下方,通过旋转父物体实现图片的旋转。

一、UI构建

简单构建UI结构,注意使用一个空物体作为几张轮播图的父级(记得Reset),大体结构如下:

二、CurvedUI实现UI的弯曲 

1)导入CurvedUI(收费的插件,就不提供,使用其他弯曲UI的插件都可以),添加下面的组件并根据需求调整Angle。

2)在父物体中添加下列代码,如下图:

  1. using UnityEngine.UI;
  2. public class uiCon1 : MonoBehaviour
  3. {
  4. public bool DevLeft = true;
  5. public float multiply = 20f;
  6. public bool isRestriction = true;
  7. public float boundary = 5f;
  8. private float dis = 0f;
  9. private float curPos;
  10. private float lastPos;
  11. private bool isMove;
  12. private float defTime = 0f;
  13. public float minDis = 5f;
  14. private Vector3 oriPos;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. dis = 0f;
  19. defTime = 0f;
  20. isMove = false;
  21. DevLeft = true;
  22. oriPos = this.GetComponent<Transform>().position;
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. if (Input.GetMouseButtonDown(0))
  28. {
  29. curPos = Input.mousePosition.x;
  30. isMove = true;
  31. }
  32. if (Input.GetMouseButtonUp(0))
  33. {
  34. isMove = false;
  35. }
  36. if (isMove)
  37. {
  38. if (Time.time - defTime >= 0.1f)
  39. {
  40. defTime = Time.time;
  41. if (Mathf.Abs(Input.mousePosition.x - curPos)>= minDis)
  42. {
  43. lastPos = Input.mousePosition.x;
  44. dis = (lastPos - curPos) / Mathf.Abs(lastPos - curPos);
  45. this.transform.eulerAngles += new Vector3(0, ((DevLeft)?(-1f):1f)*multiply*dis, 0);
  46. curPos = lastPos;
  47. }
  48. }
  49. }
  50. if (isRestriction)
  51. {
  52. RectTransform[] movePos = this.transform.GetComponentsInChildren<RectTransform>(true);
  53. foreach (var item in movePos)
  54. {
  55. if (item.position.z > (oriPos.z + boundary)) item.gameObject.SetActive(false);
  56. else item.gameObject.SetActive(true);
  57. }
  58. }
  59. }
  60. }

3)根据需求,调整下列几个属性即可 

 

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

闽ICP备14008679号