Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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)

alt text

After playing, happened the problem (would not repaint)

alt text

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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
          }
 }

Comment
Add comment · Share
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Null reference exceptions when using a Custom Editor 0 Answers

How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers

Handles not displaying 1 Answer

Responsive Editor UI Button with custom style | How to remove GUIStyle.hover delay 0 Answers

Prefabs aren't saving with Undo.RecordObject 4 Answers


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