建立一个 Cube,而后给这个 Cube 添加以下脚本。数组
using UnityEngine; using System.Collections; [RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))] // 防止获取组件失败 public class ProceduralTextureTest : MonoBehaviour { // Use this for initialization void Start () { var material = GetComponent<MeshRenderer>().material; material.mainTexture = GenerateTexture(); } Texture2D GenerateTexture() { // 建立一个 128*128 的二维纹理 var texture = new Texture2D(128,128,TextureFormat.ARGB32, false); // 定义一个颜色数组 var colors = new Color[32*32]; for (int i = 0; i < colors.Length; ++i) { colors[i] = new Color(0,0,0,1); } // 在纹理左下角 32*32 的范围绘制一块黑色区域 texture.SetPixels(0,0,31,31,colors); // Apply 使设置生效 texture.Apply(false,false); return texture; } }
效果图以下:ui