Files
5.24BuYu/Assets/MyGame/Scripts/Sprites/BgFit.cs
T

22 lines
527 B
C#
Raw Normal View History

2025-12-26 11:15:56 +08:00
using UnityEngine;
public class BgFit : MonoBehaviour
{
void Start()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
float width = sr.bounds.size.x;
float height = sr.bounds.size.y;
float worldHeight = Camera.main.orthographicSize * 2f;
float worldWidth = worldHeight * Screen.width / Screen.height;
transform.localScale = new Vector3(
worldWidth / width,
worldHeight / height,
1);
}
}