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