UnityTilemapMask icon indicating copy to clipboard operation
UnityTilemapMask copied to clipboard

Performance Issues

Open BlueAwesomeDinosaur opened this issue 2 years ago • 5 comments

Granted, I did come across this when updating a moderate area of tiles (20-30) every tenth second, but I thought I should note this just in case performance can improve.

If possible, maybe object pooling could be used and/or the generate button could include deleting/reusing the old gameobjects that make up the mask?

Apologies for posting another issue so soon after my last.

BlueAwesomeDinosaur avatar Mar 14 '24 04:03 BlueAwesomeDinosaur

Update: Tested it again and even at a slower speed (one update per 0.5s) it still does cause stuttering. Update 2 (apologies for the frequent updates): I was able to fix the issue in my own game because I was actually updating it too often but it still would be helpful if it was more performant.

BlueAwesomeDinosaur avatar Mar 14 '24 04:03 BlueAwesomeDinosaur

I modified the code to work more smoothly. Now it requires a predetermined size and it will pool the objects needed. I also included sort layer settings as well. Hope any of this helps.

` using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps;

public class TilemapMask : MonoBehaviour { //-- Made by @CalmlyGrass //-- License: Apache 2.0

public GameObject maskCell;
private GameObject maskParentObj;

[Header("Pooling")]
public bool poolFilled = false;
private GameObject[,] cells;
private List<GameObject> deactivateCells;
public Vector2Int maskDimensions = 10 * (Vector2Int.right + Vector2Int.up); 
[Header("Sort Layers")]
public int upperLayerSortingOrder;
public int upperLayer;
public int lowerLayerSortingOrder;
public int lowerLayer;

private void Start()
{
    if (transform.childCount == 0)
    {
        FillPool();
        poolFilled = true;
    }
    
}

private void FillPool()
{
    //Creates a pools of objects to use
    cells = new GameObject[maskDimensions.x, maskDimensions.y];
    deactivateCells = new List<GameObject>();
    GameObject tempObject;
    SpriteMask tempMask;

    for (int i = 0; i < maskDimensions.x; i++)
    {
        for (int j = 0; j < maskDimensions.y; j++)
        {
            tempObject = Instantiate(maskCell);
            //Sets proper values for layering
            tempMask = tempObject.GetComponent<SpriteMask>();
            tempMask.isCustomRangeActive = true;
            tempMask.frontSortingLayerID = SortingLayer.layers[upperLayerSortingOrder].id;
            tempMask.backSortingLayerID = SortingLayer.layers[lowerLayerSortingOrder].id;
            tempMask.backSortingOrder = lowerLayer;
            tempMask.frontSortingOrder = upperLayer;


            tempObject.transform.parent = transform;
            tempObject.SetActive(false);
            cells[i,j] = tempObject;
        }
    }
}

private GameObject GetPooledCell(int x, int y)
{
    return cells[x, y];
}

public void GenerateMask()
{
    if (!poolFilled)
    {
        FillPool();
        poolFilled = true;
    }

    Tilemap tilemap = GetComponent<Tilemap>();

    Vector3Int startCoord = tilemap.origin;
    Vector3Int size = tilemap.size;
    
    foreach(GameObject g in deactivateCells)
    {
        g.SetActive(false);
    }
    deactivateCells.Clear();

    //Iterate over each cell
    for (int x = startCoord.x; x < startCoord.x + size.x; x++)
    {
        for (int y = startCoord.y; y < startCoord.y + size.y; y++)
        {
            //Check if cell isn't empty
            if (tilemap.GetTile(new Vector3Int(x, y, startCoord.z)) != null)
            {
                //Create maskCell on the cell coords
                Vector3 coord = tilemap.CellToWorld(new Vector3Int(x, y, startCoord.z)) + new Vector3(0.5f, 0.5f, 0);
                GameObject cell = GetPooledCell(x - startCoord.x, y - startCoord.y);
                if (cell != null)
                {
                    cell.SetActive(true);
                    deactivateCells.Add(cell);
                    cell.transform.position = coord;
                    cell.transform.rotation = Quaternion.identity;
                    cell.GetComponent<SpriteMask>().sprite = tilemap.GetSprite(new Vector3Int(x, y, startCoord.z));
                }
            }
        }
    }
}

} `

BlueAwesomeDinosaur avatar Mar 14 '24 09:03 BlueAwesomeDinosaur

Thanks for the issue and for the code! I've noticed an issue with how Unity handles object destroying differently in the editor and in game. This issue may cause the component to create a new mask but not destroy the old one. I've fixed it in the latest commit.

Regarding pooling, it can significantly optimize mask updating and rendering processes. I will incorporate object pooling into the repository after some refinement to ensure simplicity and universality

justcoredev avatar Mar 15 '24 00:03 justcoredev

Sounds good! I'll keep using my version for now as it works well enough. The only thing I should improve on is to figure out how to make a runtime version of the prefab to use so that I don't have to set the layering for each mask cell. Also, I struggled to have the settings for the custom layering to be enabled via script so I had to modify the cell prefab to enable that. It would be great if I could figure out a solution for both problems. At least though those two issues only cause problems when "loading" so it really would just cut down on loading times.

BlueAwesomeDinosaur avatar Mar 15 '24 02:03 BlueAwesomeDinosaur

The reason I did away with the gameobject to hold the masking cells is because extra gameobject layers can be a performance issue I read.

BlueAwesomeDinosaur avatar Mar 15 '24 02:03 BlueAwesomeDinosaur