WXMC/.svn/pristine/0d/0d80427dae4521629f9ec42ecac5b3ea39cb1f74.svn-base

29 lines
791 B
Plaintext
Raw Normal View History

2024-12-04 16:18:46 +08:00
using UnityEngine;
using System.Collections;
public class ScaleToScreenSize : MonoBehaviour
{
void Start ()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
float worldScreenHeight = Camera.main.orthographicSize * 2f;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
Vector3 xWidth = transform.localScale;
xWidth.x = worldScreenWidth / width;
transform.localScale = xWidth;
Vector3 yHeight = transform.localScale;
yHeight.y = worldScreenHeight / height;
transform.localScale = yHeight;
}
}