C# Unity:视频在编辑器中工作,但在构建后不能在Android上工作
我想在我的游戏中播放一个在飞机上播放的视频(GameObject)。问题是,在unity editor中播放视频时,它工作正常,但当我在android上构建并运行它时,它只显示平面,而不显示视频。C# Unity:视频在编辑器中工作,但在构建后不能在Android上工作,c#,android,unity3d,video,C#,Android,Unity3d,Video,我想在我的游戏中播放一个在飞机上播放的视频(GameObject)。问题是,在unity editor中播放视频时,它工作正常,但当我在android上构建并运行它时,它只显示平面,而不显示视频。 这是一个简单的低质量5秒mp4视频(480x480维),任何硬件都可以轻松支持它 我已尝试启用/禁用多线程。 我还用-> 尺寸:四分之一分辨率(100x100) 纵横比:拉伸 编解码器:已在所有类型(自动、HP264、HP265、VP8)中试用过 比特率模式:低 空间质量:低空间质量 我还尝试了“材质
这是一个简单的低质量5秒mp4视频(480x480维),任何硬件都可以轻松支持它 我已尝试启用/禁用多线程。 我还用->
尺寸:四分之一分辨率(100x100)
纵横比:拉伸
编解码器:已在所有类型(自动、HP264、HP265、VP8)中试用过
比特率模式:低
空间质量:低空间质量 我还尝试了“材质覆盖”和“摄影机靠近平面”的渲染模式。两者在编辑器上都可以很好地工作,但在android上却不行 我还尝试在准备视频时等待5秒钟,还尝试在没有协同程序的情况下播放视频。什么都没用。 我在不同的Android设备上试过,但结果是一样的 这是我的飞机检查员(但我也尝试了不同的设置):
(来源:)
public void VideoPlay(int num)
{
Application.runInBackground=true;
Start例程(VideoPlayCoroutine(num));
}
IEnumerator VideoPlayCoroutine(int-num)
{
videoPlayer=GetComponent();
videoPlayer.url=“资产/视频/”+num+”.mp4”;
videoPlayer.Prepare();
而(!videoPlayer.isPrepared)
{
//调试日志(“准备视频”);
收益返回空;
}
Debug.Log(“完成准备视频”);
视频播放器;
while(videoPlayer.isPlaying)
{
收益返回空;
}
Log(“播放成功!!”;
}
公共视频站()
{
视频播放器。停止();
StopCoroutine(VideoPlayCoroutine(0));
}
VideoPlayer.url正在处理文件或HTTP url。
您应该在构建中添加绝对URL
“Assets/videos/”+num+“.mp4”仅是编辑器上的相对路径
若你们想使用URL,你们应该把你们的视频放到任何一台服务器或本地存储器中并传递它
一个简单的解决方案是声明视频剪辑列表并设置为videoPlayer.clip
[RequireComponent(typeof(UnityEngine.Video.VideoPlayer))]
public class MyVideoPlayer : MonoBehaviour
{
[SerializeField]
private System.Collections.Generic.List<UnityEngine.Video.VideoClip> videoClips = null;
private UnityEngine.Video.VideoPlayer videoPlayer = null;
private IEnumerator playEnumerator = null;
public void VideoPlay(int num)
{
Application.runInBackground = true;
playEnumerator = VideoPlayCoroutine(num);
StartCoroutine(playEnumerator);
}
public void VideoStop()
{
videoPlayer.Stop();
if (playEnumerator != null)
{
StopCoroutine(playEnumerator);
}
}
private System.Collections.IEnumerator VideoPlayCoroutine(int num)
{
videoPlayer = GetComponent<UnityEngine.Video.VideoPlayer>();
Debug.Assert(videoClips.Count > num);
Debug.Assert(videoClips[num] != null);
videoPlayer.clip = videoClips[num];
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}
Debug.Log("Done Preparing Video");
videoPlayer.Play();
while (videoPlayer.isPlaying)
{
yield return null;
}
Debug.Log("Played Successfully!!");
}
}
[RequireComponent(typeof(UnityEngine.Video.VideoPlayer))]
公共类MyVideoPlayer:单行为
{
[序列化字段]
private System.Collections.Generic.List videoClips=null;
私有UnityEngine.Video.VideoPlayer VideoPlayer=null;
私有IEnumerator playEnumerator=null;
公共无效视频播放(整数)
{
Application.runInBackground=true;
playEnumerator=VideoPlayCoroutine(num);
开始例行程序(播放枚举器);
}
公共视频站()
{
视频播放器。停止();
if(播放枚举器!=null)
{
StopCorroutine(播放枚举器);
}
}
private System.Collections.IEnumerator VideoPlayCoroutine(int num)
{
videoPlayer=GetComponent();
Assert(videoClips.Count>num);
Assert(videoClips[num]!=null);
videoPlayer.clip=videoClips[num];
videoPlayer.Prepare();
而(!videoPlayer.isPrepared)
{
调试日志(“准备视频”);
收益返回空;
}
Debug.Log(“完成准备视频”);
视频播放器;
while(videoPlayer.isPlaying)
{
收益返回空;
}
Log(“播放成功!!”;
}
}
VideoPlayer.url正在处理文件或HTTP url。
您应该在构建中添加绝对URL
“Assets/videos/”+num+“.mp4”仅是编辑器上的相对路径
若你们想使用URL,你们应该把你们的视频放到任何一台服务器或本地存储器中并传递它
一个简单的解决方案是声明视频剪辑列表并设置为videoPlayer.clip
[RequireComponent(typeof(UnityEngine.Video.VideoPlayer))]
public class MyVideoPlayer : MonoBehaviour
{
[SerializeField]
private System.Collections.Generic.List<UnityEngine.Video.VideoClip> videoClips = null;
private UnityEngine.Video.VideoPlayer videoPlayer = null;
private IEnumerator playEnumerator = null;
public void VideoPlay(int num)
{
Application.runInBackground = true;
playEnumerator = VideoPlayCoroutine(num);
StartCoroutine(playEnumerator);
}
public void VideoStop()
{
videoPlayer.Stop();
if (playEnumerator != null)
{
StopCoroutine(playEnumerator);
}
}
private System.Collections.IEnumerator VideoPlayCoroutine(int num)
{
videoPlayer = GetComponent<UnityEngine.Video.VideoPlayer>();
Debug.Assert(videoClips.Count > num);
Debug.Assert(videoClips[num] != null);
videoPlayer.clip = videoClips[num];
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}
Debug.Log("Done Preparing Video");
videoPlayer.Play();
while (videoPlayer.isPlaying)
{
yield return null;
}
Debug.Log("Played Successfully!!");
}
}
[RequireComponent(typeof(UnityEngine.Video.VideoPlayer))]
公共类MyVideoPlayer:单行为
{
[序列化字段]
private System.Collections.Generic.List videoClips=null;
私有UnityEngine.Video.VideoPlayer VideoPlayer=null;
私有IEnumerator playEnumerator=null;
公共无效视频播放(整数)
{
Application.runInBackground=true;
playEnumerator=VideoPlayCoroutine(num);
开始例行程序(播放枚举器);
}
公共视频站()
{
视频播放器。停止();
if(播放枚举器!=null)
{
StopCorroutine(播放枚举器);
}
}
private System.Collections.IEnumerator VideoPlayCoroutine(int num)
{
videoPlayer=GetComponent();
Assert(videoClips.Count>num);
Assert(videoClips[num]!=null);
videoPlayer.clip=videoClips[num];
videoPlayer.Prepare();
而(!videoPlayer.isPrepared)
{
调试日志(“准备视频”);
收益返回空;
}
Debug.Log(“完成准备视频”);
视频播放器;
while(videoPlayer.isPlaying)
{
收益返回空;
}
Log(“播放成功!!”;
}
}