• 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 Luiz_Thiago · Mar 04, 2015 at 05:23 PM · ondrawgizmos

OnDrawGizmos just running in Debug mode of Inspector

Hello everybody. After upgrade to the Unity5, the OnDrawGizmos method only works when I activate the Debug mode in the inspector.

Can anyone help me?

 public enum GridEditorStatus { Nothing, Editing }    
 public class Grid : MonoBehaviour {
     public bool DrawGrid;
     public float Width = 32.0f;
     public float Height = 32.0f;
     public Color LineColor = Color.white;
     public GridEditorStatus Status = GridEditorStatus.Nothing;
     public WorldManager WorldManager;
 
     void OnDrawGizmos () {
         Draw();
     }
 
     void OnDrawGizmosSelected () {
         Draw();
     }
 
     void Draw () {
         if (!DrawGrid) return;
 
         Vector3 pos = new Vector3(0f, 0, 0f);
         Gizmos.color = LineColor;
 
         for (float z = pos.z; z < pos.z + 200.0f; z += Height)
         {
             Gizmos.DrawLine(new Vector3(-0.5f, 0, Mathf.Floor(z / Height) * Height - 0.5f), new Vector3(200f, 0, Mathf.Floor(z / Height) * Height - 0.5f));
         }
 
         for (float x = pos.x; x < 200.0f; x += Height)
         {
             Gizmos.DrawLine(new Vector3(Mathf.Floor(x / Width) * Width - 0.5f, 0, -0.5f), new Vector3(Mathf.Floor(x / Width) * Width - 0.5f, 0, 200.0f));
         }
 
 
         for (int z = 0; z < 200; z++)
         {
             for (int x = 0; x < 200; x++)
             {
                 if (WorldManager.WorldData == null)
                 {
                     WorldManager.SetupWorldData();
                 }
 
                 var tile = WorldManager.WorldData[x].Row[z];
                 if (tile != null)
                 {
                     if (tile.Status == null)
                     {
                         tile.Status = new List<TileType>();
                     }
 
                     if (tile.Status.Count > 0)
                     {
                         var position = new Vector3(x, 10.5f, z);
                         Gizmos.color = tile.GetColor();
                         Gizmos.DrawCube(position, new Vector3(1, 1, 1));
                     }
                 }
             }
         }
     }


 enum ClickState { Nothing, Simple, Hold }
 [CustomEditor(typeof(Grid))]
 public class GridEditor : Editor {
     SerializedProperty Width;
     SerializedProperty Height;
     SerializedProperty LineColor;
     Grid grid;
     int controlId = GUIUtility.GetControlID(FocusType.Passive);
     TileType currentType;
     ClickState currentState;
 
     public void OnEnable() {
         Width = serializedObject.FindProperty("Width");
         Height = serializedObject.FindProperty("Height");
         LineColor = serializedObject.FindProperty("LineColor");
 
         grid = target as Grid;
 
         if (grid.WorldManager == null)
         {
             Debug.Log("Adding WorldManager");
             grid.WorldManager = grid.transform.GetComponent<WorldManager>();
         }
 
         if (grid.WorldManager.WorldData == null)
         {
             Debug.Log("WorldData Null");
             grid.WorldManager.SetupWorldData();
         }
     }
 
     public override void OnInspectorGUI () {
         serializedObject.Update();
 
         if(GUILayout.Button("SETUP WORD"))
         {
             grid.WorldManager.SetupWorldData();
         }
         
         EditorGUILayout.Space();
         EditorGUILayout.Space();
 
         if (grid.Status == GridEditorStatus.Editing)
         {
             if(GUILayout.Button("TURN OFF EDITION MODE"))
             {
                 grid.Status = GridEditorStatus.Nothing;
                 grid.DrawGrid = false;
             
                 SceneView.RepaintAll();
                 return;
             }
 
             EditorGUILayout.LabelField("Grid Settings:", EditorStyles.boldLabel);
             EditorGUILayout.PropertyField(LineColor, new GUIContent("Line Color: "));
             EditorGUILayout.Slider(Width, 1.0f, 100.0f, new GUIContent("Width: "));
             EditorGUILayout.Slider(Height, 1.0f, 100.0f, new GUIContent("Height: "));
             
             EditorGUILayout.Space();
             EditorGUILayout.Space();
             
             EditorGUILayout.LabelField("Tile Settings:", EditorStyles.boldLabel);
             currentType = (TileType)EditorGUILayout.EnumPopup("Tile Type: ", currentType);
         }
         else if (grid.Status == GridEditorStatus.Nothing)
         {
             if(GUILayout.Button("TURN ON EDITION MODE"))
             {
                 grid.Status = GridEditorStatus.Editing;
                 grid.DrawGrid = true;
 
                 SceneView.RepaintAll();
                 return;
             }
         }
         EditorGUILayout.Space();
         EditorGUILayout.Space();
 
         serializedObject.ApplyModifiedProperties();
     }
     
     public void OnSceneGUI()
     {
         if (grid.DrawGrid && grid.Status == GridEditorStatus.Editing)
         {
             Event e = Event.current;
 
             if (e.type == EventType.MouseDown && e.button == 0)
             {
                 GUIUtility.hotControl = controlId;
                 currentState = ClickState.Simple;
             }
             if (e.type == EventType.MouseDrag && e.button == 0)
             {
                 GUIUtility.hotControl = controlId;
                 currentState = ClickState.Hold;
             }
             if (e.type == EventType.MouseUp && e.button == 0)
             {
                 GUIUtility.hotControl = controlId;
                 currentState = ClickState.Nothing;
             }
 
             e.Use();
 
             switch (currentState)
             {
             case ClickState.Simple:
             {
                 var index = GetMousePosition();
 
                 if (index.x < 0 || index.x >= 200 || index.y < 0 || index.y >= 200)
                 {
                     break;
                 }
 
                 if (grid.WorldManager.WorldData == null)
                 {
                     grid.WorldManager.SetupWorldData();
                 }
                 
                 var worldData = grid.WorldManager.GetWorldData((int)index.x , (int)index.y);
                 
                 if (worldData == null && currentType != TileType.Nothing)
                 {
                     grid.WorldManager.SetWorldData((int)index.x, (int)index.y, currentType);
                 }
                 else
                 {
                     if (currentType != TileType.Nothing && worldData.Status != null && worldData.Status.Contains(currentType))
                     {
                         grid.WorldManager.RemoveWorldDataStatus((int)index.x, (int)index.y, currentType);
                     }
                     else
                     {
                         grid.WorldManager.SetWorldData((int)index.x, (int)index.y, currentType);
                     }
                 }
                 break;
             }
             case ClickState.Hold:
             {
                 var index = GetMousePosition();
 
                 if (index.x < 0 || index.x >= 200 || index.y < 0 || index.y >= 200)
                 {
                     break;
                 }
 
                 if (grid.WorldManager.WorldData == null)
                 {
                     grid.WorldManager.SetupWorldData();
                 }
 
                 var worldData = grid.WorldManager.GetWorldData((int)index.x , (int)index.y);
                 if (worldData == null)
                 {
                     grid.WorldManager.SetWorldData((int)index.x, (int)index.y, currentType);
                 }
                 else if (worldData != null && worldData.Status != null && !worldData.Status.Contains(currentType))
                 {
                     grid.WorldManager.SetWorldData((int)index.x, (int)index.y, currentType);
                 }
                 break;
             }
             }
         }
     }
 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pick OnDrawGizmos gizmo? 1 Answer

Equivalent of Awake() for Gizmos? 0 Answers

what is the different between OnDrawGizmosSelected and OnDrawGizmos 1 Answer

Gizmos render with an offset, but editor still displays the "right" transform.position 0 Answers

Permanent handles (when object is not selected) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges