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目录下。
3.运行效果