2016-12-01 42 views
0

我在Unity Pro中有幾個視頻,可以通過對象和MovieTexture播放。通過檢查員,我手動將MovieTexture設置爲我擁有的.ogg視頻之一,並且可以成功播放。如何在Unity Pro中在運行時更改影片紋理?

我想要做的是在滿足特定條件時通過代碼在運行時更改電影紋理(爲了簡單起見,當您按空格鍵時當前正在播放的視頻改變爲另一個視頻) 。

什麼目前對我的作品,但使用的檢查是這樣的:

MovieTexture movTex; 
AudioSource movAudio; 

void Start() 
{ 
    // I have set the movie manually through the inspector by drag and drop 
    // onto the texture component 
    movTex = (MovieTexture)GetComponent<Renderer>().material.mainTexture; 
    movAudio = GetComponent<AudioSource>(); 
    movAudio.clip = movTex.audioClip; 
    movTex.Play(); // Autoplay on start 
    movAudio.Play(); 
    movTex.loop = true; // Loop forever 
    ... 
} 

,我已經嘗試過,我在this post找到的代碼,把當然我期望的視頻下的資源文件夾,但它不起作用。沒有音頻的純白色紋理就是我所得到的。

這是代碼我試圖做工作,但我不能

MovieTexture movTex; 
AudioSource movAudio; 

void Start() 
{ 
    movTex = (MovieTexture) Resources.Load("V00001" , typeof(MovieTexture)); 
    transform.renderer.material.mainTexture = movie; 
    transform.audio.clip = movie.audioClip; 
    movAudio = GetComponent<AudioSource>(); 
    movAudio.clip = movTex.audioClip; 
    movTex.Play(); // Autoplay on start 
    movAudio.Play(); 
    movTex.loop = true; // Loop forever 
    ... 
} 

void Update() 
{ 
    if("Spacebar is pressed"){ 
     movTex = (MovieTexture) Resources.Load("V00001" , typeof(MovieTexture)); 
     movAudio = GetComponent<AudioSource>(); 
     movAudio.clip = movTex.audioClip; 
     movTex.Play(); // Autoplay on start 
     movAudio.Play(); 
     movTex.loop = true; // Loop forever 
    } 
    ... 
} 

我在做什麼錯?

+0

那麼,有什麼問題?你的問題沒有提到它的任何問題。首先,你說它的工作,然後它不...... – Programmer

回答

1

movieTexture本質上就像其他紋理一樣,它們可以像其他紋理一樣應用。然而,你需要記住的是,爲了加載不在場景中的東西,通常你必須從Resources文件夾中取出它。

讓我們假設你在你的資產文件夾在你的資源文件夾中的電影文件,某個地方,你只要寫

MovieTexture movie = (MovieTexture) Resources.Load("fileName" , typeof(MovieTexture)); 

之後加載ID,就可以用它在你的對象材料質地寫

if(yourConditions){ 
    myGameObject.GetComponent<MeshRenderer>().material.mainTexture = movie; 
}  

}

完整的功能可以是這樣的:

void Start() 
{ 
    MovieTexture movie = (MovieTexture) Resources.Load("fileName" , typeof(MovieTexture)); 
    if(movie != null){ 
     this.GetComponent<MeshRenderer>().material.mainTexture = movie; 
     // making sure there is an audio source attached 
     if(this.GetComponent<AudioSource>() == null){ 
      this.AddComponent<AudioSource>(); 
     } 
     AudioSource aud = GetComponent<AudioSource>(); 
     aud.clip = movie.audioClip; 
     movie.Play(); // Autoplay on start 
     aud.Play();  
    } 

} 
+0

我試圖和你的代碼之間唯一的區別是'this.GetComponent RabidOrange

+0

現在修復。我不確定它是否是'this'元素或Resources文件夾,因爲我改變了它在項目中的位置,現在它確實有效。然而,我不得不刪除'if(this.GetComponent ()== null){this.AddComponent ();}'因爲MonoDevelop在'AddComponent ()'部分引發錯誤,我不知道爲什麼。所以只要該元素有音頻源,該解決方案就可以工作。順便說一下,我的Unity版本是5.3.5。 – RabidOrange

+0

昨天我沒有和我在一起,我無法嘗試。這可能是因爲這在一個Component的上下文中是指組件本身而不是GameObject本身。嘗試使用「this.gameObject」而不是「this」,它應該工作。否則,如果它對你的腳本沒有問題(它可能在很長的一系列情況下),你可以添加[RequireComponent](https://docs.unity3d.com/ScriptReference/RequireComponent.html)屬性在確保您的遊戲對象始終擁有一個AudioSource。 PS:請下一次附上錯誤統一給你 – FreeFly

相關問題