I have a custom editorwindow script that works fine but whenever i restart unity it says failed to load. When i then restart the editorwindow it works fine. Whats causing this?
Code:
[Serializable]
public class TileMapEditor2D : EditorWindow
{
private TileMapController2D _tileMap;
public TileMapController2D TileMap
{
get
{
//Hacky way to get around the problem that the editor still references the 'old' MapController when entering playmode. This basicaly checks if _tileMap.gameObject throws a error or not and if so it tries to find the tilemap again.
try
{
var dummy = _tileMap.gameObject;
}
catch
{
_tileMap = GameObject.FindObjectOfType<TileMapController2D>();
}
return _tileMap;
}
}
[MenuItem("Tools/TileMapEditor")]
// ReSharper disable once UnusedMember.Local
private static void Init()
{
GetWindow(typeof(TileMapEditor2D));
}
public void OnEnable()
{
SceneView.onSceneGUIDelegate += GridUpdate;
}
public void OnDisable()
{
SceneView.onSceneGUIDelegate -= GridUpdate;
}
private void GridUpdate(SceneView sceneview)
{
int controlId = GUIUtility.GetControlID(FocusType.Passive);
Event e = Event.current;
Ray r = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight));
IntVector2D mousePos = new IntVector2D(Mathf.FloorToInt(r.origin.x), Mathf.FloorToInt(r.origin.y));
//LeftMouseButton
if (GetMouseInput(e,0))
{
DrawTile(mousePos, 1, e, controlId);
}
//RightMouseButton
if (GetMouseInput(e, 1))
{
DrawTile(mousePos, 0, e, controlId);
}
}
private bool GetMouseInput(Event e, int index)
{
return e.type == EventType.mouseDrag && e.button == index || e.type == EventType.mouseDown && e.button == index;
}
private void DrawTile(IntVector2D drawPosition, byte tileID, Event e, int controlId)
{
e.Use();
GUIUtility.hotControl = controlId;
TileMap.DrawTile(drawPosition, tileID, 1, true, 0);
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
}