- Home /
Different Sprites for iPhone, iPhone Retina & iPad
Hi all,
I've been developing games for iOS using cocos2d-iphone for some time and don't quite understand how to provide different sprites for different devices/resolutions in Unity.
In cocos2d you simply create 3 set of assets (sd, hd, ipadhd). In other words you have each sprite/spritesheet in 3 sizes. For example if I have a background image I will have 3 files:
bacgkround.png - loaded on iPhone 3GS
background-hd.png - loaded on iPhone 4,4S,5,5S and iPad
background-ipadhd.png - loaded on iPad Retina
I want to follow the same principle in Unity. I've been researching this for a few hours already and still didn't find a good way to do this.
The closest way to do this is suggested in this topic: http://answers.unity3d.com/questions/37118/how-to-support-retina-display.html
As far as I understand I need to do something like this in a script attached to each sprite:
public Sprite sd;
public Sprite hd;
public Sprite ipadhd;
void Start()
{
if ( /* iphone 3Gs */)
((SpriteRenderer)this.renderer).sprite = sd;
else if ( /* ipad retina */ )
((SpriteRenderer)this.renderer).sprite = ipadhd;
else
((SpriteRenderer)this.renderer).sprite = hd;
}
But in this case I either need to set Sprite to none in Inspector and make Editor window simply useless or the Sprite set in the Inspector will be loaded before the Start is called, and only after that I'll load correct sprite.
Is there any official way to do this correctly?
There isn't any built in way to do this, but it can be easily done by script. The script you have there should work fine but you might want to try changing the Start() function to Awake() - this should ensure that the sprite set in the inspector won't be visible before you set the new sprite.
Thanks @merry_christmas, I've managed to hack some script, if it works I'll post it later.
I've also thought about Awake, but according to documentation the object is already loaded in Awake, doesn't it mean the texture I've set in the Inspector is already loaded in memory and wasted some space/time loading?
Also it looks like I need to change camera aspect and sprite position in the script, as well as collider size if any.
$$anonymous$$aybe there is a plug-in (preferably free:)) that does everything for me?
To be honest I'm not sure if the sprite set in the sprite renderer component will be drawn before the Awake function is called - but according to this page https://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html, it sounds like the Awake function will be called before any rendering is done. Whether this means that the sprite set in the inspector won't need to be loaded, I'm not sure.
Sorry I can't be of more help.. Normally however for simple sprites this isn't really a big issue.
Your answer
Follow this Question
Related Questions
2d Sprite Sheet on characters ruined in unity 1 Answer
Replace a sprite by another in every game object using it 1 Answer
Best way to sacale entire object in runtime?? 0 Answers
Rotate a 2D sprite towards target sprite? 0 Answers
Where to start? 3 Answers