Unity UGUI播放视频不清楚解决办法

用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);
        }
    }
}
  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值