我想在Unity中的Sphere中放映电影。那很酷,它可以工作,但是没有声音。首先,我为自己的领域构建了一种新材料。那就是着色器(材料)的代码:
Shader "Flip Normals" {
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
}
SubShader{
Tags{ "RenderType" = "Opaque" }
Cull Front
CGPROGRAM
#pragma surface surf Lambert vertex:vert
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float4 color : COLOR;
};
void vert(inout appdata_full v)
{
v.normal.xyz = v.normal * -1;
}
void surf(Input IN, inout SurfaceOutput o) {
fixed3 result = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = result.rgb;
o.Alpha = 1;
}
ENDCG
}
Fallback "Diffuse"
}
此后,我为播放器编写了一个脚本。它工作正常,但是没有声音...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.Playables;
public class sphereStartTest : MonoBehaviour {
public VideoClip videoToPlay;
//video
private VideoPlayer videoPlayer;
private VideoSource videoSource;
//audio
private AudioSource audioSource;
// Use this for initialization
void Start () {
Application.runInBackground = true;
StartCoroutine(playVideo());
}
IEnumerator playVideo(){
//add VideoPlayer to the GameObjekt
videoPlayer = gameObject.AddComponent<VideoPlayer>();
//add AudioSource
audioSource = gameObject.AddComponent<AudioSource>();
//disable Play on Awayke for Video and Audio
videoPlayer.playOnAwake = true;
audioSource.playOnAwake = true;
Debug.Log("DEBUG: source Loading");
videoPlayer.source = VideoSource.VideoClip;
//set video to Play then prepare Audio to prevent Buffering
videoPlayer.clip = videoToPlay;
videoPlayer.Prepare();
Debug.Log("DEBUG: Done Preparing Video");
//set Audio Output to AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
//assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
//wait until video id prepared
while (!videoPlayer.isPrepared)
{
Debug.Log("DEBUG: Preparing Video");
yield return null;
}
//play Video
videoPlayer.Play();
//play Audio
audioSource.Play();
}
}
最后一个脚本是切换场景脚本。这很好。
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.SceneManagement;
public class switchScene : MonoBehaviour {
public VideoPlayer VideoPlayer;
public string SceneName;
// Use this for initialization
void Start () {
VideoPlayer.loopPointReached += loadScene;
}
//load the Scene
void loadScene(VideoPlayer vp)
{
SceneManager.LoadScene(SceneName);
}
}
该项目针对Android进行了优化。我使用了.mp4文件(未作为电影纹理导入)。有谁知道我如何将声音从.mp4文件发送到我的Projekt。 The "Video Player" at sphere got an Audio Source, but the AudioSource at my Sphere doesn't got an AudioClip.