Skip to content

Instantly share code, notes, and snippets.

@arun02139
Created March 15, 2017 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun02139/a360b263780e1451890e323b1264acff to your computer and use it in GitHub Desktop.
Save arun02139/a360b263780e1451890e323b1264acff to your computer and use it in GitHub Desktop.
Menu options to add, remove, and alter cell ui settings
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Assertions;
public class CellUIsMenu : MonoBehaviour
{
[MenuItem ("Window/CellUIs/Add")]
static void Add()
{
var parent = GameObject.Find ("/Battle").transform;
var resource = "HexCellUI";
var prefab = Resources.Load (resource) as GameObject;
string debugString;
for (int i = 0; i < parent.childCount; i++)
{
var cell = parent.GetChild (i).GetComponent<Cell> ();
if (cell != null)
{
// debugString = string.Format("found cell on {0} (index={1})", parent.GetChild (i).name, i);
// Debug.Log (debugString);
var oldUI = cell.gameObject.FindRecursive (resource + "(Clone)");
if (oldUI != null) {
Object.DestroyImmediate (oldUI);
}
var instance = Object.Instantiate<GameObject>(prefab);
instance.transform.SetParent (cell.transform, false);
instance.GetComponent<HexCellUI> ().Init();
// HACK: for now, due to how the hex field was generated (Z-Y origianlly swapped), add a custom local rotation
instance.transform.localRotation = Quaternion.Euler(180f, 0f, 0f);
// NOTE: dure to other rotations in the hierarchy, this is equivanet to raising the UI in the global Y
instance.transform.localPosition = new Vector3(0f, 0f, 0.51f);
}
}
}
[MenuItem ("Window/CellUIs/Remove All")]
static void RemoveAll()
{
var resource = "HexCellUI";
var parent = GameObject.Find ("/Battle").transform;
for (int i = 0; i < parent.childCount; i++)
{
var cell = parent.GetChild (i).GetComponent<Cell> ();
if (cell != null)
{
var oldUI = cell.gameObject.FindRecursive (resource + "(Clone)");
if (oldUI != null)
Object.DestroyImmediate (oldUI);
}
}
}
[MenuItem ("Window/CellUIs/SwitchCoordinates/CubeCoordinates")]
static void ShowCubeCoordinates()
{
UpdateCellUIs(CoordinateType.Cube);
HexCellUI.StorePrefs (CoordinateType.Cube);
}
[MenuItem ("Window/CellUIs/SwitchCoordinates/OffsetCoordinates")]
static void ShowOffsetCoordinates()
{
UpdateCellUIs(CoordinateType.Offset);
HexCellUI.StorePrefs (CoordinateType.Offset);
}
[MenuItem ("Window/CellUIs/SwitchCoordinates/IndexCoordinates")]
static void ShowIndexCoordinates()
{
UpdateCellUIs(CoordinateType.Index);
HexCellUI.StorePrefs (CoordinateType.Index);
}
[MenuItem ("Window/CellUIs/SwitchCoordinates/Off")]
static void HideCoordinates()
{
UpdateCellUIs(CoordinateType.Off);
HexCellUI.StorePrefs (CoordinateType.Off);
}
static void UpdateCellUIs(CoordinateType coordinateType)
{
// loop through all CellUI components and set the
var CellUIGOs = GameObject.FindObjectsOfType<HexCellUI>();
for (int i = 0; i < CellUIGOs.Length; i++)
{
var cellUI = CellUIGOs[i].GetComponent<HexCellUI>();
cellUI.Show (coordinateType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment