- Home /
Question by
ni2020ck · Dec 14, 2019 at 02:16 PM ·
2d gametilemapmouseclickdelete
calling setTile(location, null) freezes game for half a second
I have been having the problem in-game where I want to delete a specific tile that I click on during runtime. The method I am using works, but the game will freeze for half a second every time that I click on a tile to destroy. I narrowed it down to interactWithTilemaps() which just calls setTile() for a specific tile. Am I doing something wrong? Is this a bug? Is there a more efficient method?
using System.Collections;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UIElements;
public class MouseHandler : MonoBehaviour
{
public Tilemap[] interactableTilemaps;
public Queue deleteTiles;
private Vector3 mouseLoc;
private Vector3Int cellPos;
private TileBase clickedTile;
// Start is called before the first frame update
void Start()
{
deleteTiles = new Queue();
}
// Update is called once per frame
void Update()
{
float timeCheck = System.DateTime.Now.Millisecond;
if (Input.GetMouseButtonDown((int)MouseButton.LeftMouse))
{
//Debug.Log("Input Recieved. \n Time Diff from function start: " + (System.DateTime.Now.Millisecond - timeCheck)); //stop 1
//timeCheck = System.DateTime.Now.Millisecond;
mouseLoc = Camera.main.ScreenToWorldPoint(Input.mousePosition);
cellPos = interactableTilemaps[0].WorldToCell(mouseLoc); //0 because all tilemaps are at origin
//Debug.Log("Calculated Mouse Position With unity Function. \n Time Diff from last comment: " + (System.DateTime.Now.Millisecond - timeCheck)); //stop 2
//timeCheck = System.DateTime.Now.Millisecond;
//Debug.Log("mouse: " + mouseLoc + " cell: " + cellPos);
clickedTile = null;
int index = 0;
//for (int i = interactableTilemaps.Length - 1; i >= 0 && clickedTile == null; i--)
//{
clickedTile = interactableTilemaps[/*i*/0].GetTile(cellPos);
//index = i;
//}
//Debug.Log("Got the tile from the correct tilemap. \n Time Diff from last comment: " + (System.DateTime.Now.Millisecond - timeCheck)); //stop 3
//timeCheck = System.DateTime.Now.Millisecond;
//interactWithTilemaps() pass in cell coords
//do attack
if (clickedTile == null)
{
attack();
} else
{
interactWithTilemaps(cellPos, index);
}
//Debug.Log("Tile has been deleted. \n Time Diff from last comment: " + (System.DateTime.Now.Millisecond - timeCheck) + "\n\n"); //stop 4
}
}
/**
*
*/
public void attack()
{
Debug.Log("whack");
}
/**
*
*/
public void interactWithTilemaps(Vector3Int cellPos, int tilemapIndex)
{
interactableTilemaps[tilemapIndex].SetTile(cellPos, null); //This may or may not lag the crap out of the game
}
}
Comment
Figured out that it's because of the size of my tilemap that I want to set the tile on. Does this mean I should make a custom tilemap system?