从VideoPlayer源URL访问维度(宽度/高度)

我正在开发一个Unity项目,要求我下载video文件(mp4格式)并使用VideoPlayer组件播放它。 因为文件是在运行时下载的,所以我实际上必须通过URL将其“流”到其下载的位置,而不是将其作为VideoClip加载。

要准确调整video将播放的目标RenderTexture和GameObject的大小,我需要video文件本身的尺寸。 因为它不是VideoClip我不能使用:

VideoClip clip = videoPlayer.clip; float videoWidth = clip.width; float videoHeight = clip.height; 

因为我使用的是源URL而不是VideoClip,所以这将返回null。

我可以直接从文件中获取video尺寸吗?

您可以从VideoPlayer构造的纹理中检索这些信息。

获取VideoPlayer

 VideoPlayer videoPlayer = GetComponent(); 

获取纹理VideoPlayer

 Texture vidTex = videoPlayer.texture; 

获取VideoPlayer尺寸宽度/高度

 float videoWidth = vidTex.width; float videoHeight = vidTex.height; 

确保在videoPlayer.isPreparedtrue后才获取纹理。 有关如何播放video的完整代码,请参阅我的其他答案,使其显示在RawImage组件上。