- Home /
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613185015im_/https://answers.unity.com/themes/thub/images/avi.jpg)