Unity 3D中的平铺地图编辑器和绘制顺序

时间:2016-11-14 01:26:28

标签: c# dictionary unity3d draw tiled

我遇到类似的问题: stackoverflow

但在我的情况下,我使用的是Unity3D + TiledMapEditor + Tiled2Unity。

我通过Tiled2Unity程序将我的地图加载到Unity3D并作为播放器参数在第一层中的顺序可以通过以下方式轻松更改:

Renderer renderer = GetComponent<Renderer>();
renderer.sortingOrder = -(int)(transform.position.y * 100);

对象&#34;地图&#34;只能更改各个图层的参数Order In Layer。

例如:floor = 0,wall = 1,collision = 2. 我不知道怎么去单个&#34; tile&#34;地图并更改其在图层中的顺序,因为它位于何处。要从上到下绘制地图(图层中的顺序越低)。

该脚本挂钩了对象&#34; map&#34;:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;

namespace Tiled2Unity
{
    public class TiledMap : MonoBehaviour
    {
        public int NumTilesWide = 0;
        public int NumTilesHigh = 0;
        public int TileWidth = 0;
        public int TileHeight = 0;
        public float ExportScale = 1.0f;

        // Note: Because maps can be isometric and staggered we simply can't multply tile width (or height) by number of tiles wide (or high) to get width (or height)
        // We rely on the exporter to calculate the width and height of the map
        public int MapWidthInPixels = 0;
        public int MapHeightInPixels = 0;

        public float GetMapWidthInPixelsScaled()
        {
            return this.MapWidthInPixels * this.transform.lossyScale.x * this.ExportScale;
        }

        public float GetMapHeightInPixelsScaled()
        {
            return this.MapHeightInPixels * this.transform.lossyScale.y * this.ExportScale;
        }


        private void OnDrawGizmosSelected()
        {
            Vector2 pos_w = this.gameObject.transform.position;
            Vector2 topLeft = Vector2.zero + pos_w;
            Vector2 topRight = new Vector2(GetMapWidthInPixelsScaled(), 0) + pos_w;
            Vector2 bottomRight = new Vector2(GetMapWidthInPixelsScaled(), -GetMapHeightInPixelsScaled()) + pos_w;
            Vector2 bottomLeft = new Vector2(0, -GetMapHeightInPixelsScaled()) + pos_w;

            Gizmos.color = Color.red;
            Gizmos.DrawLine(topLeft, topRight);
            Gizmos.DrawLine(topRight, bottomRight);
            Gizmos.DrawLine(bottomRight, bottomLeft);
            Gizmos.DrawLine(bottomLeft, topLeft);
        }
    }
}

更好地理解(因为我的英语水平很差):

mesh.png

map.png

0 个答案:

没有答案