Создаем пустой объект, вешаем на него скрипт. В настройках скрипта выбираем текстуру которую будем применять (пример на рисунке). Указываем количество объектов, перетягиваем объекты из окна иерархии в скрипт, задаем координаты x, y с которых будем вырезать текстуру, ширину и высоту для каждого обьекта.
using UnityEngine;
using System.Collections;
//[ExecuteInEditMode()]
public class _BackgroundTexturing_ : MonoBehaviour {
//Для чтения графики
public Texture2D sourceTex;
//public ArrayList a = new ArrayList();
public GameObject[] texObject = new GameObject[2];
//public int[] TexCount = new int[2];
public Rect[] sourceRect = new Rect[2];
// Use this for initialization
void Start () {
int n = texObject.Length;
for (int i=0; i <= n-1; i++) {
/*//var myObject = GameObject.Find(texObject[i]);*/
var myObject = texObject[i];
//Читаем графику))
int x = Mathf.FloorToInt(sourceRect[i].x);
int y = Mathf.FloorToInt(sourceRect[i].y);
int width = Mathf.FloorToInt(sourceRect[i].width);
int height = Mathf.FloorToInt(sourceRect[i].height);
Color[] pix = sourceTex.GetPixels(x, y, width, height);
Texture2D destTex = new Texture2D(width, height);
destTex.SetPixels(pix);
destTex.Apply();
myObject.renderer.material.mainTexture = destTex; //Применить текстуру к обьекту...
}
}
// Update is called once per frame
void Update () {
}
}

Комментариев нет:
Отправить комментарий