suriyun-production / mmorpg-kit-docs

This is document for MMORPG KIT project (https://www.assetstore.unity3d.com/#!/content/110188?aid=1100lGeN)
https://suriyun-production.github.io/mmorpg-kit-docs
48 stars 11 forks source link

Marked Tile instead of Sprite #2389

Closed anthonyselias closed 8 months ago

anthonyselias commented 8 months ago

I'm having a hard time figuring out how to get my market tile on the tile map. I have set my layers appropriately in the component, but it only works when its outside the tilemap.

this is my code

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

public class TileHighlighter : MonoBehaviour { private List allTilemaps; private LineRenderer lineRenderer;

void Start()
{
    allTilemaps = new List<Tilemap>(FindObjectsOfType<Tilemap>());
    SetupLineRenderer();
}

void SetupLineRenderer()
{
    lineRenderer = GetComponent<LineRenderer>();
    if (lineRenderer == null)
    {
        lineRenderer = gameObject.AddComponent<LineRenderer>();
    }

    Shader shader = Shader.Find("Unlit/Color") ?? Shader.Find("Sprites/Default");
    if (shader == null)
    {
        Debug.LogError("Failed to find a valid shader.");
        return;
    }

    lineRenderer.material = new Material(shader);
    lineRenderer.startColor = Color.green;
    lineRenderer.endColor = Color.green;
    lineRenderer.startWidth = 0.05f;
    lineRenderer.endWidth = 0.05f;
}

void Update()
{
    if (gameObject.activeSelf)
    {
        foreach (var tilemap in allTilemaps)
        {
            HighlightTileAtPosition(tilemap, transform.position);
        }
    }
}

private void HighlightTileAtPosition(Tilemap tilemap, Vector3 position)
{
    Vector3Int cellPosition = tilemap.WorldToCell(position);
    Vector3 cellCenter = tilemap.GetCellCenterWorld(cellPosition);

    DrawHighlight(cellCenter);
}

private void DrawHighlight(Vector3 cellCenter)
{
    float highlightWidth = 0.96f;
    float highlightHeight = 0.96f;

    Vector3[] corners = new Vector3[5];
    corners[0] = cellCenter + new Vector3(-highlightWidth / 2, -highlightHeight / 2, 0);
    corners[1] = cellCenter + new Vector3(-highlightWidth / 2, highlightHeight / 2, 0);
    corners[2] = cellCenter + new Vector3(highlightWidth / 2, highlightHeight / 2, 0);
    corners[3] = cellCenter + new Vector3(highlightWidth / 2, -highlightHeight / 2, 0);
    corners[4] = corners[0]; // Close the loop

    lineRenderer.positionCount = corners.Length;
    lineRenderer.SetPositions(corners);
    lineRenderer.enabled = true;
}

} `

https://github.com/suriyun-production/mmorpg-kit-docs/assets/134556081/64d93046-0ece-4a00-a6a8-4521fe73197f

insthync commented 8 months ago

Screenshot_1054

Try check gameplay camera's culling mark, and the line renderer's layer.

Also check the position, you might have to make it appearing above other spites by change position in Z-axis

anthonyselias commented 8 months ago

Figured it out myself