Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by advo · Jan 06, 2017 at 09:58 AM · prefabenumscene-view

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

screen-shot-2017-01-06-at-60038-pm.png (452.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing the direction of a prefab when colliding with an object 0 Answers

In Unity how can I set a value to property while initializing a prefab with Instantiate() method like OOP constructor with parameters? 1 Answer

Use Array values in enum 1 Answer

Can't access enum property of an instantiated prefab. 1 Answer

GameObject not null before Instantiate 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges