Unity实现Tiled贴图

作者:追风剑情 发布于:2014-5-5 18:18 分类:Unity3d

1. 新建个脚本,挂在主像机上。

using UnityEngine;
using System.Collections;

public class TileTest : MonoBehaviour {

    public Texture tex;

    void OnGUI()
    {
        DrawTiledTexture(new Rect(0, 0, 160, 160), tex);
    }

    void DrawTiledTexture(Rect rect, Texture tex)
    {
        GUI.BeginGroup(rect);
        {
            int width = Mathf.RoundToInt(rect.width);
            int height = Mathf.RoundToInt(rect.height);

            for (int y = 0; y < height; y += tex.height)
            {
                for (int x = 0; x < width; x += tex.width)
                {
                    GUI.DrawTexture(new Rect(x, y, tex.width, tex.height), tex);
                }
            }
        }
        GUI.EndGroup();
    }
}

 

2. 放一张图片在Assets目录下。

tile_project.png

 

3.运行效果

tile_run.png

 

 

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号