当前位置:   article > 正文

Unity解决:Unity SpriteRenderer屏幕自适应的多种模式【动态调整大小 以遮盖Ipad所谓的安全区问题】_unity sprite renderer 铺满屏幕视口

unity sprite renderer 铺满屏幕视口

上代码:

  1. using UnityEngine;
  2. [RequireComponent(typeof(Camera))]
  3. public class SpriteAdapter : MonoBehaviour {
  4. [System.Serializable]
  5. public class SpriteInfo
  6. {
  7. public SpriteRenderer Value = null;
  8. public EFillModel Model = EFillModel.ShowAll;
  9. }
  10. public enum EFillModel
  11. {
  12. /// <summary>
  13. /// 显示图片的所有内容
  14. /// </summary>
  15. ShowAll,
  16. /// <summary>
  17. /// 使图片内容填满屏幕
  18. /// </summary>
  19. Full,
  20. /// <summary>
  21. /// 根据图片高度填充屏幕
  22. /// </summary>
  23. WithHeight,
  24. /// <summary>
  25. /// 根据图片宽度填充屏幕
  26. /// </summary>
  27. WithWidth
  28. }
  29. public enum EUpdateType
  30. {
  31. /// <summary>
  32. /// 只在唤醒时更新一次
  33. /// </summary>
  34. UpdateOnAwake,
  35. /// <summary>
  36. /// 再每次视口发生变化的时候更新一次
  37. /// </summary>
  38. UpdateOnViewportChanged
  39. }
  40. public EUpdateType TickType = EUpdateType.UpdateOnAwake;
  41. public SpriteInfo[] Members;
  42. Camera Viewport;
  43. float ScreenRatio;
  44. void Awake () {
  45. Viewport = GetComponent<Camera>();
  46. UpdateAll();
  47. }
  48. private void LateUpdate()
  49. {
  50. if (TickType != EUpdateType.UpdateOnViewportChanged) return;
  51. if (ScreenRatio != Viewport.aspect)
  52. {
  53. UpdateAll();
  54. }
  55. }
  56. /// <summary>
  57. /// 更新所有应用屏幕适配的图片
  58. /// </summary>
  59. void UpdateAll()
  60. {
  61. for (int i = 0; i < Members.Length; i++)
  62. {
  63. AdaptSpriteRender(Members[i]);
  64. }
  65. ScreenRatio = Viewport.aspect;
  66. }
  67. /// <summary>
  68. /// 使sprite铺满整个屏幕
  69. /// </summary>
  70. private void AdaptSpriteRender(SpriteInfo _spriteInfo)
  71. {
  72. SpriteRenderer spriteRenderer = _spriteInfo.Value;
  73. Vector3 scale = spriteRenderer.transform.localScale;
  74. float cameraheight = Viewport.orthographicSize * 2;
  75. float camerawidth = cameraheight * Viewport.aspect;
  76. float texr = (float)spriteRenderer.sprite.texture.width / spriteRenderer.sprite.texture.height;
  77. float viewr = camerawidth / cameraheight;
  78. switch (_spriteInfo.Model)
  79. {
  80. case EFillModel.WithHeight:
  81. //> 根据图片高度进行填充
  82. scale *= cameraheight / spriteRenderer.bounds.size.y;
  83. break;
  84. case EFillModel.WithWidth:
  85. //> 根据图片宽度进行填充
  86. scale *= camerawidth / spriteRenderer.bounds.size.x;
  87. break;
  88. case EFillModel.Full:
  89. //> 填满整个屏幕
  90. if (viewr >= texr)
  91. {
  92. if(viewr >= 1 && texr >= 1 || texr < 1)
  93. scale *= camerawidth / spriteRenderer.bounds.size.x;
  94. else
  95. scale *= cameraheight / spriteRenderer.bounds.size.y;
  96. }
  97. else
  98. {
  99. if (viewr <= 1 || texr > 1)
  100. scale *= cameraheight / spriteRenderer.bounds.size.y;
  101. else
  102. scale *= camerawidth / spriteRenderer.bounds.size.x;
  103. }
  104. break;
  105. default:
  106. //> 在屏幕上显示图片的全部内容
  107. if (viewr >= texr)
  108. {
  109. scale *= cameraheight / spriteRenderer.bounds.size.y;
  110. }
  111. else
  112. {
  113. scale *= camerawidth / spriteRenderer.bounds.size.x;
  114. }
  115. break;
  116. }
  117. spriteRenderer.transform.localScale = scale;
  118. }
  119. }

如何使用:

1.把脚本挂在Camera

2.把需要进行屏幕适配的SpriteRender对象放在Member队列中

3.选择更新类型EUpdateType、选择适配类型EFillModel即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/106568
推荐阅读
相关标签
  

闽ICP备14008679号