我正在尝试使用Unity程序,其中我有5个UI对象和3个3D对象,并在脚本中获取纹理并生成包含所有这些纹理的新纹理文件,然后将该纹理指定给对象。我的3D对象会根据命令改变纹理,但它们只会变白并且我的UI对象根本不会改变
using UnityEngine;
using System.Collections;
public class texture : MonoBehaviour {
public GameObject[] gameObjects;
public Texture2D[] textures = new Texture2D[8];
void Start () {
}
// Update is called once per frame
void Update () {
}
public void ChangeTex()
{
Texture2D ctex = new Texture2D (800, 200);
for(int i = 0; i < 8; i++)
{
for(int x = 0; x < textures[i].width; x++)
{
for(int y = 0; y < textures[i].height; y++)
{
ctex.SetPixel(x + (100 * i), y, textures[i].GetPixel (x, y));
}
}
}
foreach (GameObject g in gameObjects) {
g.GetComponent<Renderer>().material.mainTexture = ctex;
}
}
}
我是否需要在UI对象中添加某种类型的组件,或者我的代码逻辑是否有问题?
答案 0 :(得分:0)
看起来像缺少Texture2D.Apply() http://docs.unity3d.com/ScriptReference/Texture2D.Apply.html
尝试在foreach循环之前添加它:
ctex.Apply();