- Home /
Use different sprites in new Unity UI for SD vs HD
Hi all,
We are looking at the new Unity UI coming from using NGUI. Looking at the documentation for the Canvas Scaler and Designing UI for Multiple resolutions, I understand how to set the layout for my UI components and how to get them to scale properly.
One thing I am unclear on however is the ability to specify a different set of images depending on the resolution (SD vs HD), similar to what NGUI provides with Reference Atlases.
We want to load different sprites (or sprite atlases) depending on the supported resolution for the device. Two reasons: (1) on higher resolution devices we want pixel perfect images (larger images) but (2) on lower resolution devices we want to use as little memory as possible (since lower res devices tend to also be lower RAM)
It seems the Canvas Scaler only allow us to either (a) provide large textures scaled down for lower resolution devices (correct visuals but larger memory footprint) or (b) provide small textures scaled up on higher resolution devices (lower memory footprint but scaling artifacts)
I haven't seen any documentation that addresses this topic specifically. Is there a way to do this?
Thanks.
Stephane
We also stumbled over this with our project. We were compensating with 2D Toolkit for Sprites. You could probably write your own Script, that deter$$anonymous$$es the device and chooses to Resources.Load whatever atlas you need. $$anonymous$$y guess is you're out of luck, there's no such thing integrated into Unity yet.
Answer by Yury-Habets · Mar 07, 2015 at 03:58 AM
You might want to use Asset bundles: http://docs.unity3d.com/Manual/AssetBundlesIntro.html
I have looked at asset bundles, specifically assetBundles variants. The one issue I am running into with bundles is that I cannot get them to work for assets used by a prefab. If I have the prefab in one asset bundle and the assets it uses in a separate bundle, the prefab seems to lose the connection to its dependencies (even though variants are supposed to maintain the ID).
Answer by jrhtcg · Mar 08, 2015 at 05:02 PM
I did not test the code below, but it should be close to what you need. Create a script and attach to each UI component. Assign the HdSprite in the inspector.
public Sprite HdSprite;
void Start()
{
if (Screen.width > 800)
{
this.GetComponent<Image>().sprite = HdSprite;
}
}
Answer by TimB · Mar 24, 2015 at 06:16 PM
You should check out https://www.youtube.com/watch?v=rMCLWt1DuqI if you haven't seen it yet. He begins discussing it in the first few minutes. I am going through the same process as you deciding to try the Unity UI/2D for our next project or stick with our NGui / 2D toolkit approach. I would love to come up with a solution using the native Unity tools, but I am currently leaning towards just using 2D toolkit for everything, including UI.