Created
March 15, 2017 13:37
-
-
Save arun02139/3ee43392ad8be77c1b1762cd75dd808c to your computer and use it in GitHub Desktop.
Script attached to my hexagonal cell prefab, inherits from Hexagon
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 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