Guest User

Untitled

a guest
Mar 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. // Require the rect transform to be on the same object as this navigation behaviour
  7. [RequireComponent(typeof(RectTransform))]
  8. public class GridNavigation : MonoBehaviour {
  9.  
  10. // The layout
  11. public GridLayoutGroup layout;
  12.  
  13. // Default starting position (top left)
  14. public Vector2Int position = new Vector2Int(0, 0);
  15.  
  16. // Child elements of the transform
  17. private List<RectTransform> children;
  18.  
  19. // The rect transform of the object
  20. private RectTransform rectTransform;
  21.  
  22. public Vector2Int Position {
  23. get {
  24. return position;
  25. }
  26. set {
  27. bool update = false;
  28. if (position != value) {
  29. update = true;
  30. }
  31. position = value;
  32. if (update) {
  33. UpdateUI();
  34. }
  35. }
  36. }
  37.  
  38. // Use this for initialization
  39. void Start() {
  40. rectTransform = GetComponent<RectTransform>();
  41. children = new List<RectTransform>();
  42. for (int i = 0; i < rectTransform.childCount; i++) {
  43. children.Add(rectTransform.GetChild(i).GetComponent<RectTransform>());
  44. }
  45. Debug.Log("Width: " + GetWidth());
  46. Debug.Log("Height: " + GetHeight());
  47. // Position = new Vector2Int(1, 1);
  48. }
  49.  
  50. RectTransform GetTile(int x, int y) {
  51. return children[Get1DPosition(x, y)];
  52. }
  53.  
  54. RectTransform GetTile(Vector2Int pos) {
  55. return children[Get1DPosition(pos.x, pos.y)];
  56. }
  57.  
  58. Vector2Int Get2DPosition(int index) {
  59. return new Vector2Int(
  60. index % GetWidth(), // % is the "modulo operator", the remainder of i / width;
  61. index / GetWidth()); // where "/" is an integer division
  62. }
  63.  
  64. int Get1DPosition(int x, int y) {
  65. return (GetWidth() * y) + x;
  66. }
  67.  
  68. void SelectTile(RectTransform tile) {
  69. if (tile != null) {
  70. tile.GetComponent<Button>().Select();
  71. }
  72. else {
  73. Debug.LogError("Can't select null tile.");
  74. }
  75. }
  76.  
  77. // Update is called once per frame
  78. void Update() {
  79. int v = 0, h = 0;
  80. if (Input.GetKeyDown(KeyCode.W)) { v = -1; h = 0; }
  81. else if (Input.GetKeyDown(KeyCode.A)) { v = 0; h = -1; }
  82. else if (Input.GetKeyDown(KeyCode.S)) { v = 1; h = 0; }
  83. else if (Input.GetKeyDown(KeyCode.D)) { v = 0; h = 1; }
  84. if (h != 0 || v != 0) {
  85. if (InBounds(h, v)) {
  86. Move(h, v);
  87. }
  88. else {
  89. Debug.LogError(string.Format("Invalid grid navigation move: ({0}, {1})", h, v));
  90. }
  91. }
  92. }
  93.  
  94. void UpdateUI() {
  95. SelectTile(GetTile(Position.x, Position.y));
  96. }
  97.  
  98. // Check if the move is valid, within the bounds of the grid
  99. bool InBounds(int h, int v) {
  100. bool valid = false;
  101. if (Position.y + v < GetHeight() && Position.y + v >= 0) {
  102. if (Position.x + h < GetWidth() && Position.x + h >= 0) {
  103. valid = true;
  104. }
  105. }
  106. return valid;
  107. }
  108.  
  109. void Move(int h, int v) {
  110. Position += new Vector2Int(h, v);
  111. }
  112.  
  113. public int ChildCount() {
  114. return transform.childCount;
  115. }
  116.  
  117. public int GetWidth() {
  118. int width = 0;
  119. switch (layout.constraint) {
  120. case GridLayoutGroup.Constraint.Flexible:
  121. width = Mathf.FloorToInt(rectTransform.rect.width / (layout.cellSize.x + layout.spacing.x));
  122. break;
  123. case GridLayoutGroup.Constraint.FixedColumnCount:
  124. width = layout.constraintCount;
  125. break;
  126. case GridLayoutGroup.Constraint.FixedRowCount:
  127. width = (ChildCount() / layout.constraintCount);
  128. break;
  129. default:
  130. break;
  131. }
  132. return width;
  133. }
  134.  
  135. public int GetHeight() {
  136. return 2;
  137. }
  138. }
Add Comment
Please, Sign In to add comment