Hey I am new to NGUI and Texture atlases
I created a texture atlas/ material/ prefab. But can someone gimme an example of how to draw a particular image from the atlas through scripting.
For example, If i Press R. I need to draw the Red box from the atlas.
Hi, you must instanciate a UISprite component on a GameObject belonging to the UIRoot hierarchy and set some variables to get it working but it will be messy.
NGUI’s magic is best served from the editor… Setup your UI from the editor, then reference the UISprite to the script component you wish to have it edited.
Another way is to make a prefab of the object with the UISprite you previously setup in the editor, then instanciate it from you script that need to spawn it. And don’t forget to parent it under the UIRoot
As for the code to get the sprite in the UISprite, should be something like:
public UIAtlas atlas; //assign in the editor
public UISprite uiSprite; //assign in the editor
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
UISpriteData spriteData = atlas.GetSprite("TheRedCubeFromTheAtlas");
uiSprite.SetAtlasSprite(spriteData);
}
}