using UnityEngine; using System.Collections; /// /// This component is typically used on planes which are not generated by NGUI. E.g. Sperms. /// It reads texture coordinates from NGUI and applies them to the material used by the renderer on this gameObject /// public class ReadTextureCoordinatesFromNGUI : MonoBehaviour { public UIAtlas atlas; public string spriteName; public bool IsOtherAtlas; const float SPERM_SCALE_FACTOR = .75f; static float scaleFactor = SPERM_SCALE_FACTOR; void Start() { Init(); } public void Init() { if (IsOtherAtlas) { if (ResolutionSwitchController.IsSD) { scaleFactor = SPERM_SCALE_FACTOR; } else if (ResolutionSwitchController.IsSHD) { scaleFactor = SPERM_SCALE_FACTOR; } atlas.coordinates = UIAtlas.Coordinates.TexCoords; UIAtlas.Sprite sprite = atlas.GetSprite(spriteName); Material m = GetComponent().material; m.mainTexture = atlas.texture; m.mainTextureOffset = new Vector2(sprite.inner.x, sprite.inner.y); m.mainTextureScale = new Vector2(sprite.inner.width, sprite.inner.height); atlas.coordinates = UIAtlas.Coordinates.Pixels; transform.localScale = new Vector3(sprite.inner.width * scaleFactor, sprite.inner.height * scaleFactor); } else { if (ResolutionSwitchController.IsSD) { scaleFactor = SPERM_SCALE_FACTOR * 2; } else if (ResolutionSwitchController.IsSHD) { scaleFactor = SPERM_SCALE_FACTOR / 2; } atlas.coordinates = UIAtlas.Coordinates.TexCoords; UIAtlas.Sprite sprite = atlas.GetSprite(spriteName); Material m = GetComponent().material; m.mainTexture = atlas.texture; m.mainTextureOffset = new Vector2(sprite.inner.x, sprite.inner.y); m.mainTextureScale = new Vector2(sprite.inner.width, sprite.inner.height); atlas.coordinates = UIAtlas.Coordinates.Pixels; transform.localScale = new Vector3(sprite.inner.width * scaleFactor, sprite.inner.height * scaleFactor); } } }