private void OnSceneGUI() { if (Tools.current != Tool.None && Tools.current != Tool.Rect) { paintBox = false; return; } if (!paintBox) { return; } if (selectedLayer < 0 || selectedLayer > tilemap.Layers.Count || selectedTile == 0) { return; } Handles.BeginGUI(); if (tilemap.UndoEnabled) { if (GUI.Button(new Rect(5, 5, 50, 25), "Undo")) { tilemap.Undo(); tilemap.RefreshAll(true); } if (GUI.Button(new Rect(60, 5, 50, 25), "Redo")) { tilemap.Redo(); tilemap.RefreshAll(true); } } Handles.EndGUI(); Event e = Event.current; int controlID = GUIUtility.GetControlID(FocusType.Passive); HandleUtility.AddDefaultControl(controlID); Tools.current = Tool.None; switch (e.GetTypeForControl(controlID)) { case EventType.MouseUp: GUIUtility.hotControl = 0; if (tilemap.IsRecordingCommand) { tilemap.EndRecordCommand(); } mouseUpBefore = true; e.Use(); break; } Vector3 mousePosition = e.mousePosition; Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition); mousePosition = ray.origin; if (!tilemap.UpdatePreviewBrushPos(mousePosition)) { GenTexture(); } if (!e.shift && shiftBefore) { shiftBefore = false; GenTexture(false); } if (e.shift && !shiftBefore) { shiftBefore = true; GenTexture(true); } if (e.type == EventType.MouseDown && e.button == 0) { var localPos = tilemap.transform.InverseTransformPoint(mousePosition); var gridX = Utilites.GetGridX(localPos); var gridY = Utilites.GetGridY(localPos); LastMousePos = new Vector2Int(gridX, gridY); } if ((e.type == EventType.MouseDrag || e.type == EventType.MouseDown) && e.button == 0) { if (mouseUpBefore && !tilemap.IsRecordingCommand) { tilemap.BeginRecordCommand(); mouseUpBefore = false; } GUIUtility.hotControl = controlID; Vector2 localPos = tilemap.transform.InverseTransformPoint(mousePosition); var gridX = Utilites.GetGridX(localPos); var gridY = Utilites.GetGridY(localPos); if (e.shift) { int eraseMin = eraseSize / 2; int eraseMax = eraseSize - eraseMin; BresenhamLine(LastMousePos.x, LastMousePos.y, gridX, gridY, eraseMin, eraseMax, false); } else if (e.control) { selectedTile = tilemap.GetTile(gridX, gridY, selectedLayer); } else { int brushMin = brushSize / 2; int brushMax = brushSize - brushMin; BresenhamLine(LastMousePos.x, LastMousePos.y, gridX, gridY, brushMin, brushMax); } LastMousePos = new Vector2Int(gridX, gridY); //tilemap.Layers[selectedLayer].UpdateMesh(); EditorUtility.SetDirty(tilemap); e.Use(); } }
private void OnEnable() { tilemap = target as ATilemap; tilemap.OnValidate(); layersList = new ReorderableList(tilemap.Layers, typeof(Tile), true, true, true, true); layersList.drawHeaderCallback += DrawHeader; layersList.drawElementCallback += DrawElement; layersList.onAddCallback += AddItem; layersList.onRemoveCallback += RemoveItem; layersList.onReorderCallback += (index) => { tilemap.CalculateLayersOrder(); tilemap.RefreshAll(true); CalculateIndexes(); }; }