unity2017虽然官方支持了瓷砖,但是总觉得不够好用,就自己写了个简单的自动瓷砖,效果如图。

思路是遍历特点范围的每个GameObjec,然后计算坐标,再通过坐标来处理瓷砖间的相对关系,顺便也会合并Collider2D来节省资源。
先看父类,主要处理变换坐标的工作:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoTile : MonoBehaviour
{
public float sideLength; /* 图块边长 */
public bool isCoillder; /* 是否合并碰撞体 */
public GameObject startOne; /* 左下角的起始块,标定开始计算的位置 */
List<ArrayList> mapContainer = new List<ArrayList>(); /* 存储地图 */
protected Transform[] childrenTransforms; /* 存储子物体 */
protected Vector2 basePoint; /* 从startone获取的起点 */
/* 初始化起点 */
protected void intinalBasePoint()
{
basePoint = startOne.transform.position;
/* Debug.Log ("BasePoint=" + basePoint); */
}
/* 初始化存储块信息的变长数组 */
protected void intinalMapContainer()
{
/* 获取子物体的数组 */
childrenTransforms = GetComponentsInChildren<Transform> ( false );
foreach ( Transform tra in childrenTransforms )
{
/* 利用tag判断是否纳入计算 */
if ( tra.tag == "Tile" )
{
Vector2 pos = tra.position;
int x, y;
x = (int) ( (pos.x - basePoint.x) / sideLength + 0.05f); /* 加0.05防止误差 */
y = (int) ( (pos.y -