用Rawimage播放高清视频时,画质特别模糊,原因时画面比例失调。解决方法:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI; //解决VideoClip不清晰问题
[AddComponentMenu("Video/PlayVideoOnUGUI")]
public class PlayVideoOnUGUI : MonoBehaviour
{
//定义参数获取VideoPlayer组件和RawImage组件
private VideoPlayer videoPlayer;
private RawImage rawImage;
void Awake()
{
Screen.fullScreen = true;
//获取场景中对应的组件
videoPlayer = this.GetComponent<VideoPlayer>();
rawImage = this.GetComponent<RawImage>();
}
void Update()
{
//如果videoPlayer没有对应的视频texture,则返回
if (videoPlayer.texture == null)
{
return;
}
//把VideoPlayerd的视频渲染到UGUI的RawImage
rawImage.texture = videoPlayer.texture;
}
}
如果是3d物体播放视频:
调整长宽比即可。
using UnityEngine;
using UnityEngine.Video;
public class VideoFilt : MonoBehaviour
{
VideoPlayer vp;
[Tooltip("原始宽度|若为视频不必设置")]
public float w = 1;
[Tooltip("原始高度")]
public float h = 1;
[Tooltip("是否为图片|以宽为基准等比例缩放")]
public bool isTexture = false;
void Start()
{
if (isTexture)
{
float ratio = w / h;
Vector3 LScale = transform.localScale;
transform.localScale = new Vector3(LScale.x, LScale.x / ratio, LScale.z);
}
else
{
vp = transform.GetComponent<VideoPlayer>();
float ratio = (float)vp.width / vp.height;
Vector3 LScale = transform.localScale;
transform.localPosition = Vector3.one;
transform.localScale = new Vector3(LScale.x, LScale.x / ratio, LScale.z);
}
}
}