Hi guys, hope some one can help me here.
So, i have this texture (Type, advanced, Sprite 2d GUI) that i use as an Atlas. In sprite editor i cut the Sprites and its works just fine.
Now im trying to get the coordinates of the sprites, Name, position (rect), border (rect), pivot (vector 2).
How can i do that by code?
I did , getComponentInChildren // or
and output
Debug.Log(renderer.sprite.name);
Debug.Log(renderer.sprite.rect);
Debug.Log(renderer.sprite.pivot);
Debug.Log(renderer.sprite.pixelsPerUnit);
Debug.Log(renderer.sprite.textureRect);
Debug.Log(renderer.sprite.rect.height);
but only the name is correct, all other info are wrong with some arbitrary numbers. Where are my mistake?
thank you!
ok, i made it.!
changing the atlas for a SD/HD/XHD for mobile 2d game.
void Awake(){
loadAtlasCoord ();
}
private void loadAtlasCoord(){
//Load the default Atlas for Coordinates, change if for JSON or XML later
var subSprites = Resources.LoadAll<Sprite>(defaultAtlas);
//Create the new Atlas
Texture2D atlasUI = Resources.Load<Texture2D> (defaultAtlas+"SD");
//Create the new Sprites
foreach (Sprite sprite in subSprites){
float rectX = sprite.rect.x;
float rectY = sprite.rect.y;
float rectW = sprite.rect.width;
float rectH = sprite.rect.height;
float pivotX = (sprite.pivot.x)/(sprite.rect.width);
float pivotY = (sprite.pivot.y)/(sprite.rect.height);
float pixelPerUnit = sprite.pixelsPerUnit;
Sprite newSprite = Sprite.Create(atlasUI, new Rect(rectX/2,rectY/2,rectW/2,rectH/2) , new Vector2(pivotX,pivotY), pixelPerUnit/2);
newSprite.name = sprite.name;
//Update the Sprites with the new ones SD
foreach (var renderer in GetComponentsInChildren<Image>()) {
string spriteName = renderer.sprite.name;
if(renderer.sprite.name == newSprite.name){
renderer.sprite = newSprite;
}
}
}
//Unload unused
Resources.UnloadUnusedAssets ();
}
}