I'm getting my feed wet with extending the Unity tools. Long story short I want to make a custom grid that I can use to snap specific objects. Right now I'm trying to make a window that shows you the grid that you can define. My issue here is that this the line drawing doesn't seem to update in a window very well.
If I toggle it on or off... doesn't update. If it's on and I select a viewport, it draw, but If I change a number, it does not update. If I change a number and select a viewport, it now draws the previous iteration as well as the last one.
I'm not even sure if I'm using the correct draw functions here. drawing gizmos didn't seem to work. As well, I got a memory leak error from this. So, any suggestions as to a better method or a way to force a draw update in the viewports.
Here is the code I have so far.
class TileWindow extends EditorWindow
{
var showGrid:boolean=false;
var xTiles:int=24;
var zTiles:int=24;
var tileSize:float=1;
var xOffset:float=0;
var zOffset:float=0;
var topLeft:Vector3;
var topRight:Vector3;
var botLeft:Vector3;
var botRight:Vector3;
@MenuItem ("Window/Tile View")
static function ShowWindow () {
EditorWindow.GetWindow (TileWindow);
}
function OnGUI () {
showGrid=EditorGUILayout.Toggle("Grid View",showGrid);
tileSize=EditorGUILayout.FloatField("Tile Size",tileSize);
xTiles=EditorGUILayout.FloatField("X Tiles",xTiles);
zTiles=EditorGUILayout.FloatField("Z Tiles",zTiles);
}
function OnInspectorUpdate () {
xOffset=-(xTiles*tileSize)/2;
zOffset=-(zTiles*tileSize)/2;
if (showGrid){
for (i=0;i<xTiles;i++){
for (j=0;j<zTiles;j++){
topLeft= Vector3(xOffset+(tileSize*i),0,zOffset+(tileSize*j));
topRight=Vector3(xOffset+(tileSize*i+tileSize),0,zOffset+(tileSize*j));
botLeft= Vector3(xOffset+(tileSize*i),0,zOffset+(tileSize*j+tileSize));
botRight=Vector3(xOffset+(tileSize*i+tileSize),0,zOffset+(tileSize*j+tileSize));
Debug.DrawLine(topLeft,topRight,Color.red);
Debug.DrawLine(botLeft,botRight,Color.red);
Debug.DrawLine(topLeft,botLeft,Color.red);
Debug.DrawLine(topRight,botRight,Color.red);
}
}
}
}
}
You should use OnEnable() to register your delegates. And delegates can be checked https://mcmap.net/q/9272/-has-an-event-handler-already-been-added
– VitriolicThanks for the tip on checking delegates. I didn't check OnEnable, but it's not documented as a message received by EditorWindow. Does it work anyway?
– KickstandThats a cool snippet, didn't know I can work with the
– CoagulaseSceneView
insideEditorWindow
too.If you got
– DurningTargetParameterCountException
error, then try to usestatic
on void OnSceneGUI(SceneView sceneView){} to be static void OnSceneGUI(SceneView sceneView) {} don't know if this is will help or not, just figuring out when I got that exception while using the same method as describe above for drawing GUI elements on SceneView while not on Play Mode.