unity开发安卓视频文件适配手机和平板

using UnityEngine;
using UnityEngine.UI;

public class VideoResize : MonoBehaviour
{
    
    
    private RawImage rawImage;
    private VideoPlayer videoPlayer;

    private void Start()
    {
    
    
        rawImage = GetComponent<RawImage();
        videoPlayer = GetComponent<VideoPlayer>();

        // 播放视频
        videoPlayer.Play();

        // 调整视频大小以适应屏幕
        ResizeVideo();
    }

    private void ResizeVideo()
    {
    
    
               int videoWidth = 1920;//视频的宽
                    int videoHeight = 1080;//视频的高

                    int targetWidth = Screen.width;//屏幕宽
                    int targetHeight = Screen.height;//屏幕高
                    
                    float scaleWidth = (float)targetWidth / videoWidth;
                    float scaleHeight = (float)targetHeight / videoHeight;

                    // 确保保持原始宽高比
                    float scaleFactor = Mathf.Min(scaleWidth, scaleHeight);

                    int newWidth = Mathf.RoundToInt(videoWidth * scaleFactor);
                    int newHeight = Mathf.RoundToInt(videoHeight * scaleFactor);
                    rawImage.rectTransform.sizeDelta = new Vector2(newWidth , newHeight );
    }
}
                   

猜你喜欢

转载自blog.csdn.net/o_ojjj/article/details/134061223