Hi,
My goal is to use a different background texture for iPhone4/iPad1&2, iPhone5, and iPad retina. I'm wondering how to change a UITexture to achieve this at run-time. You cannot set the pixel size for UITextures like you can for UIAtlases so I'm curious how it is accomplished.
My current attempt is to query for screen size and then switch the texture but I don't know how this is affected by my UIRoot settings.
using UnityEngine;
using System.Collections;
[RequireComponent
(typeof(UITexture
))] public class NGUITextureSwitcher : MonoBehaviour
{
public UITextureSet[] atlasSets;
private UITexture m_texture;
private void Awake()
{
this.m_texture = GetComponent<UITexture>();
}
private void Start()
{
if (atlasSets.Length == 0) return;
UITextureSet set = atlasSets[0];
for (int i = 0; i < atlasSets.Length; i++)
{
Debug.Log (Screen.height);
if (Screen.height >= atlasSets[i].triggerHeight)
set = atlasSets[i];
}
if (set != null)
{
if (m_texture.name != set.textureName)
{
if (m_texture.mainTexture != null)
{
Resources.UnloadAsset(m_texture.mainTexture);
}
m_texture.mainTexture = Resources.Load("Backgrounds/" + set.textureName) as Texture2D;
}
}
}
}
[System.Serializable]
public class UITextureSet
{
public string name;
public int triggerHeight;
public Vector2 spriteRes;
public Vector2 textureRes;
public string textureName;
}
Thanks,
Kevin