从VideoPlayer源URL访问尺寸(宽度/高度) [英] Accessing dimensions (width/height) from a VideoPlayer source URL
问题描述
我正在开发一个Unity项目,该项目需要我下载视频文件(mp4格式)并使用VideoPlayer组件播放.由于该文件是在运行时下载的,因此与将其作为VideoClip加载相比,我实际上必须通过URL将其流式传输"到其下载位置.
I am working on a Unity project that requires me to download a video file (mp4 format) and play it using the VideoPlayer component. Because the file is downloaded at runtime, I effectively have to "stream" it via a url to its downloaded location, as opposed to loading it as a VideoClip.
要准确调整视频将要播放到的目标RenderTexture和GameObject的大小,我需要视频文件本身的尺寸.因为它不是VideoClip,所以我不能使用:
To accurately size the target RenderTexture and GameObject the video will be playing to, I need the dimensions of the video file itself. Because it is not a VideoClip I cannot use:
VideoClip clip = videoPlayer.clip;
float videoWidth = clip.width;
float videoHeight = clip.height;
因为我使用的是视频URL而不是VideoClip,所以它将返回null.
Because I am using a source URL as opposed to a VideoClip, this will return null.
我可以通过某种方式直接从文件中获取视频尺寸吗?
Can I get the video dimensions directly from the file somehow?
推荐答案
您可以从VideoPlayer构造的Texture中检索这些信息.
You can retrieve these information from the Texture that VideoPlayer constructs.
获取VideoPlayer
Get VideoPlayer
VideoPlayer videoPlayer = GetComponent<VideoPlayer>();
获取纹理VideoPlayer
Get the Texture VideoPlayer
Texture vidTex = videoPlayer.texture;
获取VideoPlayer的尺寸宽度/高度
Get VideoPlayer dimension width/height
float videoWidth = vidTex.width;
float videoHeight = vidTex.height;
确保仅在videoPlayer.isPrepared
为true
之后获得纹理.请参阅我的其他答案,以获取有关如何播放视频使其显示在RawImage
组件上的完整代码.
Make sure to only get the texture after videoPlayer.isPrepared
is true
. See my other answer for full code on how to play video make it display on RawImage
component.
这篇关于从VideoPlayer源URL访问尺寸(宽度/高度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!