Skip to content

Instantly share code, notes, and snippets.

@arun02139
Created March 15, 2017 13:37
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/3ee43392ad8be77c1b1762cd75dd808c to your computer and use it in GitHub Desktop.
Save arun02139/3ee43392ad8be77c1b1762cd75dd808c to your computer and use it in GitHub Desktop.
Script attached to my hexagonal cell prefab, inherits from Hexagon
using UnityEngine;
public class MyHexagon : Hexagon
{
public void Start()
{
SetColor(Color.white);
SetOutlineColor(Color.black);
}
public override Vector3 GetCellDimensions()
{
var center = Vector3.zero;
foreach (Transform child in transform.FindChild("Outline"))
{
if (child.GetComponent<Renderer>() == null)
continue;
center += child.GetComponent<Renderer>().bounds.center;
}
center /= transform.FindChild("Outline").childCount;
Bounds ret = new Bounds(center, Vector3.zero);
foreach (Transform child in transform.FindChild("Outline"))
{
if (child.GetComponent<Renderer>() == null)
continue;
ret.Encapsulate(child.GetComponent<Renderer>().bounds);
}
return ret.size;
}
public override void MarkAsReachable()
{
SetColor(Color.yellow);
}
public override void MarkAsAttackable()
{
SetColor(Color.red);
}
public override void MarkAsPath()
{
SetColor(Color.green);
}
public override void MarkAsHighlighted()
{
SetOutlineColor(Color.blue);
}
public override void UnMark()
{
SetColor(Color.white);
SetOutlineColor(Color.black);
}
void SetColor(Color color)
{
for (int i = 0; i < transform.childCount; i++)
{
var rendererComponent = transform.GetChild(i).GetComponent<Renderer>();
if (rendererComponent != null)
rendererComponent.material.color = color;
}
}
void SetOutlineColor(Color color)
{
var outline = transform.FindChild("Outline");
for (int i = 0; i < outline.transform.childCount; i++)
{
var rendererComponent = outline.transform.GetChild(i).GetComponent<Renderer>();
if (rendererComponent != null)
rendererComponent.material.color = color;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment