-1

I'm trying to have a Unity program where I have 5 UI objects and 3 3D objects and take their texture in the script and generate a new texture file with all those and then assign that texture to the objects. I have my 3D objects change texture upon command but they just turn white and my UI objects don't change at all

before command

after command

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;

    }
}
}

Do I need to add a certain type of component to my UI objects or is there a problem with my codes logic?

Jake Howard
  • 21
  • 1
  • 8

1 Answers1

0

Looks like missing Texture2D.Apply() http://docs.unity3d.com/ScriptReference/Texture2D.Apply.html

Try adding this before the foreach loop:

ctex.Apply(); 
mgear
  • 1,333
  • 2
  • 22
  • 39
  • Thanks, that did the trick my 3d game objects get the combined texture now. Now I just have to figure out why my UI objects textures won't change. This is my first time doing Texture Mapping and a lot of the tutorials I find only go over inspector stuff and don't go into scripting that much. – Jake Howard Oct 25 '15 at 00:36