Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections.Generic;
  5.  
  6. public class UIRingCommand : UIBehaviour, ILayoutGroup {
  7.  
  8. public Vector3 offsetPosition = new Vector3(0f, 0f, 0f);
  9. public float offsetAngle;
  10. public Vector3 distance = new Vector3(0f, 100f, 0f);
  11. public float maxAngle = 360f;
  12. public int currentItemIndex;
  13. public bool isShow;
  14. public bool isArcMode;
  15. public float speed = 10f;
  16.  
  17. private List<RectTransform> items = new List<RectTransform>();
  18. private RectTransform rectTransform;
  19. private Vector3 currentDistance;
  20. private float currentItemMaxAngle;
  21. private float currentBaseCircleAngle;
  22. private float itemAngleStep;
  23.  
  24. private float LerpSpeed {
  25. get { return speed * Time.deltaTime; }
  26. }
  27.  
  28. void RefreshItemList() {
  29. items.Clear();
  30. foreach (RectTransform t in transform) {
  31. items.Add(t);
  32. }
  33. }
  34.  
  35. protected override void Start() {
  36. base.Awake();
  37. rectTransform = GetComponent<RectTransform>();
  38. RefreshItemList();
  39. }
  40.  
  41. void Update() {
  42. UpdateValues();
  43. for (int idx = 0; idx < items.Count; idx++) {
  44. items[idx].anchoredPosition3D =
  45. Quaternion.Euler(0f, 0f, itemAngleStep * idx) * // それぞれのアイテムの位置に回転。
  46. Quaternion.Euler(0f, 0f, currentBaseCircleAngle - offsetAngle) * // 選択中のアイテムIdxに応じて回転。
  47. currentDistance + offsetPosition;
  48. }
  49. }
  50.  
  51. void UpdateValues() {
  52. itemAngleStep = Mathf.Lerp(itemAngleStep, currentItemMaxAngle / items.Count, LerpSpeed * 2f);
  53.  
  54. if (isArcMode) {
  55. currentDistance = rectTransform.anchoredPosition3D + distance;
  56. } else {
  57. currentDistance = Vector3.Lerp(
  58. currentDistance,
  59. (isShow ? rectTransform.anchoredPosition3D + distance : Vector3.zero),
  60. LerpSpeed);
  61. }
  62.  
  63. if (isArcMode) {
  64. currentItemMaxAngle = Mathf.Lerp(
  65. currentItemMaxAngle,
  66. (isShow ? -maxAngle : 0f),
  67. LerpSpeed);
  68. } else {
  69. currentItemMaxAngle = -maxAngle;
  70. }
  71.  
  72. currentBaseCircleAngle = Mathf.Lerp(
  73. currentBaseCircleAngle,
  74. -itemAngleStep * currentItemIndex,
  75. LerpSpeed);
  76. }
  77.  
  78. #region ILayoutGroup
  79. // UI要素変化時のコールバック
  80. // 両方呼ばれるようなので、片方でだけアイテムリストの更新を行う。
  81. public void SetLayoutHorizontal() {
  82. RefreshItemList();
  83. }
  84. public void SetLayoutVertical() { }
  85. #endregion
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement