39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public class adaptive_ui : MonoBehaviour
|
|||
|
{
|
|||
|
public RawImage rawImage; // ʹ<><CAB9>RawImage<67><65><EFBFBD><EFBFBD>չʾ<D5B9><CABE><EFBFBD><EFBFBD>
|
|||
|
public RectTransform boundaryRect; // <20><><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD><EFBFBD><EFBFBD>ε<EFBFBD>RectTransform
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
ScaleAndCropTexture();
|
|||
|
}
|
|||
|
|
|||
|
void ScaleAndCropTexture()
|
|||
|
{
|
|||
|
Texture texture = rawImage.texture;
|
|||
|
float textureWidth = texture.width;
|
|||
|
float textureHeight = texture.height;
|
|||
|
|
|||
|
float boundaryWidth = boundaryRect.rect.width;
|
|||
|
float boundaryHeight = boundaryRect.rect.height;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>϶̵<CFB6>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ӧ<EFBFBD>߽<EFBFBD><DFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
float scaleFactor = Mathf.Max(boundaryWidth / textureWidth, boundaryHeight / textureHeight);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA>ijߴ<C4B3>
|
|||
|
float newWidth = textureWidth * scaleFactor;
|
|||
|
float newHeight = textureHeight * scaleFactor;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>RawImage<67><65>RectTransform<72>ߴ磬ʹ<E7A3AC><CAB9><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD>DZ߽<C7B1><DFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
rawImage.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
|
|||
|
|
|||
|
// <20>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
|
|||
|
rawImage.rectTransform.anchoredPosition = Vector2.zero; // ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
}
|