Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Stop VideoPlayer(s) running in background

Discussion in 'Scripting' started by stipla, Mar 9, 2021.

  1. stipla

    stipla

    Joined:
    Mar 3, 2021
    Posts:
    5
    Here everyone : )

    Question from Paris in France
    Today is a really beautiful day here with a fantastic blue sky
    hope its cool in your town too

    i have multiples objects with the same script.
    i start playing a video everytime i click on one of this objects
    the problem is : when i click on a new object i want to stop all others videos playing
    i tried lot of things but my code lvl is come to an end : (

    Thank you <3

    Code (CSharp):
    1. public class PlayMovieVP : MonoBehaviour
    2. {
    3.     public UnityEngine.Video.VideoClip videoClip;
    4.     void Start()
    5.     {
    6.         var videoPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
    7.         var audioSource = gameObject.AddComponent<AudioSource>();
    8.         videoPlayer.playOnAwake = false;
    9.         videoPlayer.clip = videoClip;
    10.         videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.MaterialOverride;
    11.         videoPlayer.targetMaterialRenderer = GetComponent<Renderer>();
    12.         videoPlayer.targetMaterialProperty = "_MainTex";
    13.         videoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.Direct;
    14.         videoPlayer.SetTargetAudioSource(0, audioSource);
    15.     }
    16.     void OnMouseDown()
    17.     {
    18.         var vp = GetComponent<UnityEngine.Video.VideoPlayer>();
    19.         if (vp.isPlaying)
    20.         {
    21.             vp.Pause();
    22.         }
    23.         else
    24.         {
    25.             vp.Play();
    26.         }
    27.     }
    28. }
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    I´m not sure if it is the best way, but you could "collect" all your objects with

     GameObject.FindGameObjectsWithTag("yourTag") 

    (you have to set your objects a tag before, obviously) and loop them in your "OnMouseDown". Then you can get you script for/from each object and stop the animation that is running
     
    stipla likes this.
  3. stipla

    stipla

    Joined:
    Mar 3, 2021
    Posts:
    5
    Thank you for your quick response : )

    Interesting.

    But if i do that i cant keep the same script for all objects i think
    Same script cant create/detect different videos and tags

    So i have to create one script per each video ? (i have 37 objects/videos)
     
  4. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    Sure you can add the same script for each object (you even have to, if you wanna do this). With each gameobject you create a "copy" of the script for that object, so each object has its own script, but yes the code is the same.

    if each object plays a different video you can add the video for each object in the inspector/editor, with that each object has his own video, but the script can work on the same way for all objects. Therefore you need a field in your script which is marked as [SerializeField]. BUt i´m not sure what the type of the field has to be, maybe Animation or maybe there is a different one for videos.

    To get each object with "GameObject.FindGameObjectsWithTag("yourTag")" all you objects you need have to have the same tag.
     
  5. stipla

    stipla

    Joined:
    Mar 3, 2021
    Posts:
    5
    we are agree
    thank you
    i will try it tonight and give a response
     
  6. stipla

    stipla

    Joined:
    Mar 3, 2021
    Posts:
    5
    it works very well !
    THANKS <3