- Home /
Question by
CastleWSC · Dec 23, 2014 at 11:32 AM ·
editorcustom editorcustom-inspector
Custom editor does not work well after Play
I want to create a grid editor and I have two parts: GridManager, GridEditor
Beginning the custom editor works well, but after Playing I got a problem. The Unity editor couldn't repaint scene view when modify the grid properties in the Inspector panel.
Beginning the editor was well (would repaint scene view)
After playing, happened the problem (would not repaint)
GridManager.cs
[ExecuteInEditMode]
public class GridManager : MonoBehaviour
{
private static GridManager Instance = null;
public static GridManager instance
{
get
{
if(Instance = null)
{
Instance = FindObjectOfType(typeof(GridManager)) as GridManager;
if (Instance == null)
{
Debug.LogError("Could not find the GridManager script");
}
}
return Instance;
}
}
public int numOfColumns;
public int numOfRows;
public float gridCellSize;
private Vector3 origin = new Vector3();
public Vector3 Origin { get { return origin; } }
public Node[,] nodes { get; set; }
public List<int> obstacles = new List<int>();
void Awake()
{
Initialize();
}
void Update()
{
origin = transform.position;
}
void Initialize()
{
// declare nodes
}
}
GridEditor.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(GridManager))]
public class GridEditor : Editor {
GridManager grid;
static bool showGrid;
static bool showGridLabel;
static bool showObstacles;
static int labelSize;
static Color gridColor = Color.white;
static Color obstacleColor = new Color(0.5f, 0.0f, 0.0f, 0.25f);
static Color labelColor = Color.green;
static Color coordinateColor = Color.cyan;
public void OnEnable()
{
grid = target as GridManager;
if (SceneView.onSceneGUIDelegate == null)
{
SceneView.onSceneGUIDelegate += OnScene;
}
}
public void OnDestory()
{
SceneView.onSceneGUIDelegate -= OnScene;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
showGrid = EditorGUILayout.Toggle("Show Grid", showGrid);
gridColor = EditorGUILayout.ColorField(gridColor);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
showGridLabel = EditorGUILayout.Toggle("Show Grid Label", showGridLabel);
labelColor = EditorGUILayout.ColorField(labelColor);
coordinateColor = EditorGUILayout.ColorField(coordinateColor);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
showObstacles = EditorGUILayout.Toggle("Show Obstacles", showObstacles);
obstacleColor = EditorGUILayout.ColorField(obstacleColor);
EditorGUILayout.EndHorizontal();
labelSize = EditorGUILayout.IntSlider("Label Size", labelSize, 0, 50);
EditorGUILayout.Space();
if (GUILayout.Button("Clear Obstacles", GUILayout.Height(30)))
{
grid.ClearObstacles();
}
EditorGUILayout.Space();
EditorGUILayout.HelpBox("When enable the 'Show Obstacle' toggle \nPress key 'A' to add or remove the obstacle",
MessageType.None, true);
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
SceneView.RepaintAll();
}
void OnScene(SceneView sceneview)
{
if (showGrid)
{
DrawGrid();
}
if (showObstacles)
{
ObstacleEditor(Event.current);
DrawObstacles();
}
}
void DrawGrid()
{
int columns = grid.numOfColumns;
int rows = grid.numOfRows;
float cellSize = grid.gridCellSize;
Vector3 origin = grid.Origin;
Handles.color = gridColor;
if (columns > 0 || rows > 0 || cellSize > 0)
{
float width = columns * cellSize;
float height = rows * cellSize;
// draw the horizontal lines
for (int i = 0; i < rows + 1; i++)
{
Vector3 startPos = origin + i * cellSize * new Vector3(0.0f, 0.0f, -1.0f);
Vector3 endPos = startPos + width * new Vector3(1.0f, 0.0f, 0.0f);
Handles.DrawLine(startPos, endPos);
}
// draw the vertical lines
for (int i = 0; i < columns + 1; i++)
{
Vector3 startPos = origin + i * cellSize * new Vector3(1.0f, 0.0f, 0.0f);
Vector3 endPos = startPos + height * new Vector3(0.0f, 0.0f, -1.0f);
Handles.DrawLine(startPos, endPos);
}
// draw the gird labels
if (showGridLabel)
{
int index = 0;
float posOffset = cellSize * 0.25f;
GUIStyle guistyle = new GUIStyle();
guistyle.normal.textColor = labelColor;
guistyle.fontSize = labelSize;
// draw the cell index
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
float xPos = j * cellSize + posOffset;
float zPos = i * (-cellSize) - posOffset;
Vector3 labelPos = origin + new Vector3(xPos, origin.y, zPos);
Handles.Label(labelPos, index.ToString(), guistyle);
index++;
}
}
// draw the row and column label
guistyle.normal.textColor = coordinateColor;
for (int i = 0; i < rows; i++)
{
float xPos = (-cellSize) + posOffset;
float zPos = i * (-cellSize) - posOffset;
Vector3 labelPos = origin + new Vector3(xPos, origin.y, zPos);
Handles.Label(labelPos, i.ToString(), guistyle);
}
for (int i = 0; i < columns; i++)
{
float xPos = i * cellSize + posOffset;
float zPos = cellSize - posOffset;
Vector3 labelPos = origin + new Vector3(xPos, origin.y, zPos);
Handles.Label(labelPos, i.ToString(), guistyle);
}
}
}
}
void ObstacleEditor(Event e)
{
float cellSize = grid.gridCellSize;
if (e.type == EventType.KeyUp && e.keyCode == KeyCode.A)
{
Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
Vector3 cellPos = new Vector3(
Mathf.Floor(r.origin.x / cellSize) * cellSize + cellSize * 0.5f,
grid.Origin.y,
Mathf.Floor(r.origin.z / cellSize) * cellSize + cellSize * 0.5f);
int index = grid.GetGridIndex(cellPos);
MarkObstacle(index);
}
}
void MarkObstacle(int index)
{
if (!grid.AddObstacle(index))
{
grid.RemoveObstacle(index);
}
}
void DrawObstacles()
{
int columns = grid.numOfColumns;
int rows = grid.numOfRows;
float cellSize = grid.gridCellSize;
List<int> list = grid.obstacles;
if (columns > 0 && rows > 0 && cellSize > 0 && list != null)
{
foreach (int index in list)
{
Vector3 v = grid.GetGridCellCenter(grid.GetGridCoordinate(index));
Vector3[] verts = new Vector3[] {
v + new Vector3(-cellSize*0.5f, 0.0f, +cellSize*0.5f),
v + new Vector3(cellSize*0.5f, 0.0f, +cellSize*0.5f),
v + new Vector3(cellSize*0.5f, 0.0f, -cellSize*0.5f),
v + new Vector3(-cellSize*0.5f, 0.0f, -cellSize*0.5f)
};
Handles.DrawSolidRectangleWithOutline(verts, obstacleColor, obstacleColor);
}
}
}
}
What happened about this ?
firsttime.png
(161.0 kB)
afterplaying.png
(173.6 kB)
Comment
Answer by CastleWSC · Dec 24, 2014 at 07:18 AM
I got it what happened. I have to alter some codes in GridEditor.cs
We don't need OnDestory function, but we need a OnDisable function
GridEditor.cs
public void OnEnable()
{
grid = target as GridManager;
if (SceneView.onSceneGUIDelegate != OnScene)
{
SceneView.onSceneGUIDelegate += OnScene;
}
}
public void OnDisable()
{
if(SceneView.onSceneGUIDelegate == OnScene)
{
SceneView.onSceneGUIDelegate -= OnScene;
}
}