Created
March 15, 2017 01:50
-
-
Save arun02139/a749e453fcf4295ca58af94e6c2e254b to your computer and use it in GitHub Desktop.
Single ui to display various cell coordinate systems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Assertions; | |
public enum CoordinateType { Off, Cube, Offset, Index } // NOTE: offset coordinates are type 'even-q' | |
public class HexCellUI : MonoBehaviour | |
{ | |
const string _prefKey = "HexCellUI"; | |
Hexagon _hexCell; | |
CoordinateType _coordinateSetting = CoordinateType.Off; // NOTE: debug setting; PlayerPrefs api may not be available on consoles | |
public GameObject OffsetCoordinates; | |
public GameObject CubeCoordinates; | |
public GameObject IndexCoordinates; | |
void Awake() | |
{ | |
Init (); | |
} | |
public void Init() | |
{ | |
_coordinateSetting = HexCellUI.GetPrefs (); | |
_hexCell = GetComponentInParent<Hexagon> (); | |
SetOffsetCoordinates (); | |
SetCubeCoordinates (); | |
SetIndexCoordinates (); | |
Show (_coordinateSetting); | |
} | |
void SetOffsetCoordinates () | |
{ | |
var text = OffsetCoordinates.GetComponentInChildren<Text> (); | |
text.text = string.Format ("{0},{1}", _hexCell.OffsetCoord.x.ToString ("0"), _hexCell.OffsetCoord.y.ToString ("0")); | |
} | |
void SetCubeCoordinates () | |
{ | |
// NOTE: cube coordinates for hexagonal cells are derived from offset coordinates which are | |
// in the XZ plane (i.e. _offsetCoord.x = x-value, _offsetCoord.y = z-value) so accordingly | |
// CubeCoord.x = x-value, CubeCoord.y = z-value and CubeCoord.z = y value | |
_hexCell = GetComponentInParent<Hexagon> (); | |
CubeCoordinates.FindRecursive ("X").GetComponent<Text> ().text = _hexCell.CubeCoord.x.ToString ("0"); | |
CubeCoordinates.FindRecursive ("Y").GetComponent<Text> ().text = _hexCell.CubeCoord.z.ToString ("0"); // NOTE the | |
CubeCoordinates.FindRecursive ("Z").GetComponent<Text> ().text = _hexCell.CubeCoord.y.ToString ("0"); // z-y switch! | |
} | |
// NOTE: unlike the other coordinates, the index value is calculated | |
// here on the fly (i.e. when the function is run) | |
void SetIndexCoordinates () | |
{ | |
var text = IndexCoordinates.GetComponentInChildren<Text> (); | |
text.text = string.Format ("{0}", transform.GetSiblingIndex ()); | |
} | |
public void Show(CoordinateType coordinateType) | |
{ | |
CubeCoordinates.SetActive(false); | |
OffsetCoordinates.SetActive(false); | |
IndexCoordinates.SetActive(false); | |
if (coordinateType == CoordinateType.Cube) | |
CubeCoordinates.SetActive(true); | |
else if(coordinateType == CoordinateType.Offset) | |
OffsetCoordinates.SetActive(true); | |
else if(coordinateType == CoordinateType.Index) | |
IndexCoordinates.SetActive(true); | |
} | |
public static CoordinateType GetPrefs () | |
{ | |
if (PlayerPrefs.HasKey (_prefKey)) { | |
return (CoordinateType)PlayerPrefs.GetInt (_prefKey); | |
} else { | |
StorePrefs(CoordinateType.Off); | |
return CoordinateType.Off; // Default | |
} | |
} | |
public static void StorePrefs (CoordinateType coordinateType) | |
{ | |
PlayerPrefs.SetInt (_prefKey, (int)coordinateType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment