0
votes

I have an assignment where I have to do the following:

  1. Create at least 5 UI objects and 3 3D objects in a scene, and import at least 8 different texture image files for each of them. If you only want to create 3 3D objects, they have to be one cube, one sphere and one cylinder. Assign different textures to the objects.

  2. Write a script to read the information of all of the image files used as textures in the scene and generate a single texture image file contains all of the used texture image files in the scene.

  3. Extend the script file wrote above to be able to automatically and correctly map the new single texture image file to all of the textured objects.

I'm pretty sure I can do the first one no problem but I'm not sure about the scripting part. I've looked up tutorials on the subject but they only talk about applying textures to objects and nothing about the scripting. Does anyone know any videos or web pages that could help me learn that part?

1

1 Answers

0
votes

Number 2 could be achieved by iterating through every GameObject in the scene using GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>(), then getting their Texture2D by accessing their respective Material.mainTexture property (doc), that will give an array of all used Texture2Ds. The next part

and generate a single texture image file contains all of the used texture image files in the scene

is ambigious. It can be done in whatever way you see fit, that generates one texture image, e.g. by placing all of them in a row or so. It could be however, that this references the texture atlas functionality, in which case you'll want to look at the Texture2D.PackTextures function right here. This function will pack you all the given textures (a Texture2D[]) into a single atlas, and will return an array of UV-coordinates to you.

Returns

Rect[] An array of rectangles containing the UV coordinates in the atlas for each input texture, or null if packing fails.

This is the key for solving number 3. If you will assign the generated packed texture back to the 3D objects in the sene, it is essiantial that the UV-coordinates are correct, so that you'll see only the part of the texture which it had in the beginning. Assign the the proper UV-coordinates back to the objects from the Rect[]the PackTextures() function gives to you (a rect has two Vector2 in it, one will be X/Y (vertex), the other one will be the U-V (tex-coord) you'll need), and the task should be solved.

(Reference for the UV part: http://answers.unity3d.com/questions/276212/what-is-the-simple-way-of-applying-texture-atlas-t.html)