- Home /
Need help packing menu textures
I have a menu that is essentially a background image and 4 buttons using GUITextures. What I'm trying to do is get the buttons into one draw call.
My idea is to use the PackTextures() function to create an atlas, but I need help! I'm unfamiliar with non-runtime code and was wondering if someone could give me the basics of how I'd go about replacing my buttons into one atlas and getting them back into the scene?
I'm thinking something along the lines of…
Create temporary GameObject
Attach the button textures as an array
Run PackTextures() function on array to create atlas
Delete/disable the temp GameObject
Find the resulting atlas [where?] and attach it to all 4 buttons
Get the rect coordinates [how?]
Specify the rect coordinates to each GUITexture component so it knows which area of atlas to use [also how?]
Answer by Wolfram · Jun 15, 2012 at 10:35 PM
Hm, I don't think you can change the UV mapping of GUITextures, you might need to use UnityGUI instead (the stuff in OnGUI(), although I'm also not sure it's possible with that, either, since I don't use it), or use a planar 3D object attached to your camera parallel to your screen (there are scripts around on how to accomplish that)
But to answer the rest of your questions:
1+4 You don't need an additional GameObject, just attach the script to an empty object, or your target object
5 PackTextures() is a method of Texture2D, and the resulting atlas will replace that texture. So for example, you could create an empty texture, call myTexture.PackTextures() on it, and now myTexture will be your atlas
6 The Rect[] array matching your Texture2D[] input array is returned by PackTextures()
Thanks Wolfram. It's making a bit more sense now. I will try the plane approach. (Then I can do some funky stuff with 3D buttons!)
Though I'm still confused on how to use a script like this… I'm guessing it's not something I want to have happen at runtime right? $$anonymous$$y understanding is I'd set-up the script, press the editor's play button, it would create the atlas, I'd stop the game in the editor, and now I can disable that script for the future because it created the atlas already. — Is this right? I'm thinking my understanding may be a bit off.
You can do both. Either just do it at runtime, in a regular script. Then your script would also need to replace all textures with the atlas reference, and replace the UVs of all objects involved.
Or you could write an EditorWindow script, which would create the atlas, and store it as an asset (AssetDatabase.CreateAsset). But then you'd have to think about something to also store the UV values somewhere.
In our projects, we're doing our GUIs using 3D objects exclusively, since OnGUI() doesn't work with uniTUIO, which we rely on, and GUITextures are far less flexible. See my answer here: http://answers.unity3d.com/questions/24863/3d-plane-width-in-pixels-worldtoscreensize.html
Also realized that BatchTools will do this for me anyway! :)
Your answer
Follow this Question
Related Questions
Why do I have to click GUI Menu twice to start in Web player? 0 Answers
Textures on iPhone appear blurry? 1 Answer
Calling Function in other Script via Touch => iOS Crash 1 Answer
How will i get animated gif images in scene? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers