ReisenYoumuOuting/Assets/Scripts/GetPixelSize.cs

22 lines
745 B
C#

using System;
using UnityEngine;
public class GetPixelSize : MonoBehaviour
{
private void Start()
{
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
Bounds bounds = spriteRenderer.bounds; // World-space boundary box
// Convert the minimum and maximum world space corners to screen pixel positions
Vector3 minScreenPos = Camera.main.WorldToScreenPoint(bounds.min);
Vector3 maxScreenPos = Camera.main.WorldToScreenPoint(bounds.max);
// Calculate the actual screen pixel delta
float screenPixelWidth = maxScreenPos.x - minScreenPos.x;
float screenPixelHeight = maxScreenPos.y - minScreenPos.y;
Debug.Log($"Name: {gameObject.name}Actual Screen Size: {screenPixelWidth}x{screenPixelHeight} physical pixels");
}
}