Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- // Require the rect transform to be on the same object as this navigation behaviour
- [RequireComponent(typeof(RectTransform))]
- public class GridNavigation : MonoBehaviour {
- // The layout
- public GridLayoutGroup layout;
- // Default starting position (top left)
- public Vector2Int position = new Vector2Int(0, 0);
- // Child elements of the transform
- private List<RectTransform> children;
- // The rect transform of the object
- private RectTransform rectTransform;
- public Vector2Int Position {
- get {
- return position;
- }
- set {
- bool update = false;
- if (position != value) {
- update = true;
- }
- position = value;
- if (update) {
- UpdateUI();
- }
- }
- }
- // Use this for initialization
- void Start() {
- rectTransform = GetComponent<RectTransform>();
- children = new List<RectTransform>();
- for (int i = 0; i < rectTransform.childCount; i++) {
- children.Add(rectTransform.GetChild(i).GetComponent<RectTransform>());
- }
- Debug.Log("Width: " + GetWidth());
- Debug.Log("Height: " + GetHeight());
- // Position = new Vector2Int(1, 1);
- }
- RectTransform GetTile(int x, int y) {
- return children[Get1DPosition(x, y)];
- }
- RectTransform GetTile(Vector2Int pos) {
- return children[Get1DPosition(pos.x, pos.y)];
- }
- Vector2Int Get2DPosition(int index) {
- return new Vector2Int(
- index % GetWidth(), // % is the "modulo operator", the remainder of i / width;
- index / GetWidth()); // where "/" is an integer division
- }
- int Get1DPosition(int x, int y) {
- return (GetWidth() * y) + x;
- }
- void SelectTile(RectTransform tile) {
- if (tile != null) {
- tile.GetComponent<Button>().Select();
- }
- else {
- Debug.LogError("Can't select null tile.");
- }
- }
- // Update is called once per frame
- void Update() {
- int v = 0, h = 0;
- if (Input.GetKeyDown(KeyCode.W)) { v = -1; h = 0; }
- else if (Input.GetKeyDown(KeyCode.A)) { v = 0; h = -1; }
- else if (Input.GetKeyDown(KeyCode.S)) { v = 1; h = 0; }
- else if (Input.GetKeyDown(KeyCode.D)) { v = 0; h = 1; }
- if (h != 0 || v != 0) {
- if (InBounds(h, v)) {
- Move(h, v);
- }
- else {
- Debug.LogError(string.Format("Invalid grid navigation move: ({0}, {1})", h, v));
- }
- }
- }
- void UpdateUI() {
- SelectTile(GetTile(Position.x, Position.y));
- }
- // Check if the move is valid, within the bounds of the grid
- bool InBounds(int h, int v) {
- bool valid = false;
- if (Position.y + v < GetHeight() && Position.y + v >= 0) {
- if (Position.x + h < GetWidth() && Position.x + h >= 0) {
- valid = true;
- }
- }
- return valid;
- }
- void Move(int h, int v) {
- Position += new Vector2Int(h, v);
- }
- public int ChildCount() {
- return transform.childCount;
- }
- public int GetWidth() {
- int width = 0;
- switch (layout.constraint) {
- case GridLayoutGroup.Constraint.Flexible:
- width = Mathf.FloorToInt(rectTransform.rect.width / (layout.cellSize.x + layout.spacing.x));
- break;
- case GridLayoutGroup.Constraint.FixedColumnCount:
- width = layout.constraintCount;
- break;
- case GridLayoutGroup.Constraint.FixedRowCount:
- width = (ChildCount() / layout.constraintCount);
- break;
- default:
- break;
- }
- return width;
- }
- public int GetHeight() {
- return 2;
- }
- }
Add Comment
Please, Sign In to add comment