- Home /
Using Public Enums in OnDrawGizmo, good practice?
![alt text][1]Hi this is my first foray into Unity and I am attempting to create a Tower Defense grid made out of cubes (Nodes) that creates the "ground". All of these Nodes are based on the same Prefab, and in that Prefab, I have a script which defines an Public Enum called "NodeType" which appears in the inspector, so i can select whether that Node is for the Player (buildable area) or the Enemy (walking path). My thoughts were, I could use the value of this Enum for alot of decision making in the future of what each Node can / cannot do.
What was lacking was some "visual indication" in the Scene Editor of which Node was a Player node or Enemy node, because by default, all Nodes were the same color... So i found a way of doing this onDrawGizmo function where i programmatically change the material of the Node based on the value of the Enum. This surprisingly worked (altho it sometimes took a couple seconds to update in Scene view). Now I can change the value in the inspector and in Scene View i can clearly see which Nodes in the grid are Nodes for players and which are for enemies.
The reason i did this was because I wanted to create levels visually easily. E.g I have 1 template Scene with a whole grid of Nodes, I copy this to a new Scene, and I create an "enemy walking path" easily by selecting the nodes on the grid, and mass changing their NodeType to Enemy. Make sense? Rinse and repeat for other levels.
Does this make sense and is this good practice? Just wanted to get some thoughts. MY simple script below which is attached to the Node Prefab.
public class Node : MonoBehaviour {
public enum NodeType {
Player, Enemy
};
public NodeType nodeType;
public Material matNodeTypePlayer;
public Material matNodeTypeEnemy;
void Start () {
//Update Materials in Game View
switch(nodeType) {
case NodeType.Player:
GetComponent<Renderer> ().material = matNodeTypePlayer;
break;
case NodeType.Enemy:
GetComponent<Renderer> ().material = matNodeTypeEnemy;
break;
default:
Debug.Log("Some Node not assigned Node Types!");
break;
}
}
void OnDrawGizmos () {
//Update Materials in Scene View / Editor
switch(nodeType) {
case NodeType.Player:
GetComponent<Renderer> ().material = matNodeTypePlayer;
break;
case NodeType.Enemy:
GetComponent<Renderer> ().material = matNodeTypeEnemy;
break;
default:
Debug.Log("Some Node not assigned Node Types!");
break;
}
}
}
Edit: Added Screenshot to describe my intention [1]: /storage/temp/85420-screen-shot-2017-01-06-at-60038-pm.png