Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Video Player on iOS and uGUI

Discussion in '5.6 Beta' started by TechDevTom, Jan 21, 2017.

  1. TechDevTom

    TechDevTom

    Joined:
    Oct 21, 2011
    Posts:
    33
    Hello folks!

    I spent a few hours trying to get 5.6's new Video Player (THANK YOU UNITY DEVS, THANK YOU!) to work with uGUI's Raw Image component on iOS as I was having a little difficulty and managed to figure out how to get it to work. The method I used is not as good as I thought, originally I had posted it here, but for now I have removed it until I can get answers to some questions.

    Question: If I attach a VideoPlayer component to a Raw Image and create a script that basically tells the Raw Image's texture to be that of the VideoPlayer's texture after the VideoPlayer has been prepared, should that Raw Image then show the video I want to play?

    Details: I have written a test script, you can see it below. I have been using that script to test whether a Raw Image component can play a video on my iPad 3. I have a Raw Image with a VideoPlayer attached to it as well as the script shown below. The VideoPlayer has no clip assigned to it initially, as this is done by the script, and has a Render Mode of 'API Only'. But in my tests I can only ever get the Raw Image to play a video if there is another Raw Image in the scene that has a VideoPlayer attached to it with the Render Mode of 'Render Texture'. If I don't have that second VideoPlayer using the 'Render Texture' mode in the scene, I never see any video on the first Raw Image using the 'API Only' Render Mode VideoPlayer. I only ever hear the sound. Does the 'Render Texture' Render Mode do something behind the scenes to initialise some kind of play behaviour for the VideoPlayer stuff?

    Any help anyone can offer me on all of this would be great. Some documentation on how you can use uGUI with the new VideoPlayer would also be very much appreciated, even if it is just the most basic of guides and info on the what is good and bad.

    Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Video;
    4.  
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8.  
    9. public class VideoPlayer_RawImage_002 : MonoBehaviour
    10. {
    11.     public VideoPlayer         videoPlayer;
    12.     public VideoClip         videoClip;
    13.     public RawImage         rawImage;
    14.  
    15.     public bool             playOnClick;
    16.  
    17.  
    18.     void Start ()
    19.     {
    20.         StartCoroutine (Video_Prepare ());
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         if(playOnClick)
    26.         {
    27.             if(Input.GetKeyDown (KeyCode.Space) || Input.touchCount == 1 &&
    28.                 Input.GetTouch (0).phase == TouchPhase.Began)
    29.             {
    30.                 PlayVideo ();
    31.             }
    32.         }
    33.     }
    34.  
    35.     IEnumerator Video_Prepare ()
    36.     {
    37.         if(videoClip != null)
    38.         {
    39.             videoPlayer.clip = videoClip;
    40.         }
    41.         videoPlayer.Prepare ();
    42.         while (!videoPlayer.isPrepared)
    43.         {
    44.             Debug.Log ("Preparing Video");
    45.             yield return null;
    46.         }
    47.         if(rawImage != null)
    48.         {
    49.             rawImage.texture = videoPlayer.texture;
    50.         }
    51.         Debug.Log ("Video Prepared");
    52.     }
    53.  
    54.     public void PlayVideo ()
    55.     {
    56.         videoPlayer.Stop ();
    57.         videoPlayer.Play ();
    58.     }
    59. }
     
    Last edited: Jan 22, 2017