FixSlicingOnDifferentResolutions.cs 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for fixing the slicing according to what atlas resolution is being used.
  5. /// This is an annoyance that NGUI doesn't take into consideration and so we have to fix it on Start
  6. /// </summary>
  7. public class FixSlicingOnDifferentResolutions : MonoBehaviour
  8. {
  9. void Start()
  10. {
  11. if (ResolutionSwitchController.IsSD)
  12. {
  13. ScaleSpritesBy(0.5f);
  14. } else
  15. if (ResolutionSwitchController.IsSHD)
  16. {
  17. ScaleSpritesBy(2);
  18. }
  19. Destroy(this);
  20. }
  21. void ScaleSpritesBy(float scaleFactor)
  22. {
  23. Vector3 scale = transform.localScale;
  24. scale.x /= scaleFactor;
  25. scale.y /= scaleFactor;
  26. transform.localScale = scale;
  27. UISprite[] sprites = GetComponentsInChildren<UISprite>(true);
  28. foreach (UISprite sprite in sprites)
  29. {
  30. if (sprite.type == UISprite.Type.Sliced)
  31. {
  32. scale = sprite.cachedTransform.localScale;
  33. scale.x *= scaleFactor;
  34. scale.y *= scaleFactor;
  35. sprite.cachedTransform.localScale = scale;
  36. }
  37. }
  38. }
  39. }