ScreenSpaceBackground.cs 1.1 KB

12345678910111213141516171819202122232425
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Olympus
  4. {
  5. // The content keeps its 750x1334 layout. One continuous illustration covers
  6. // the entire display, including the extra space above/below a tall device.
  7. [ExecuteAlways, DefaultExecutionOrder(100), RequireComponent(typeof(Image))]
  8. public sealed class ScreenSpaceBackground : MonoBehaviour
  9. {
  10. void LateUpdate()
  11. {
  12. var image=GetComponent<Image>();var canvas=GetComponentInParent<Canvas>();
  13. if(!image.sprite||!canvas||canvas.renderMode!=RenderMode.ScreenSpaceOverlay)return;
  14. var r=image.rectTransform;var parent=(RectTransform)r.parent;
  15. float scale=Mathf.Abs(parent.lossyScale.x);if(scale<.001f)return;
  16. float aspect=image.sprite.rect.width/image.sprite.rect.height;
  17. float width=Mathf.Max(Screen.width/scale,Screen.height/scale*aspect);
  18. r.anchorMin=r.anchorMax=r.pivot=new Vector2(.5f,.5f);
  19. r.sizeDelta=new Vector2(width,width/aspect);
  20. r.position=new Vector3(Screen.width*.5f,Screen.height*.5f,r.position.z);
  21. image.preserveAspect=true;
  22. }
  23. }
  24. }