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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Sync video on host and clients

Discussion in 'Multiplayer' started by only18days, Oct 2, 2023.

  1. only18days

    only18days

    Joined:
    Jun 24, 2022
    Posts:
    3
    Hi all,
    I am trying to synchronize the video on both the host and clients.
    And I tried the method supplied by Bruno in this post. However, I've run into an issue where I'm unable to call the RpcPlayVideo function. I've attempted to troubleshoot this problem, but I haven't been able to find a solution yet. Any kind of help will be highly appreciated.

    Sincerely,
    Chloe

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using UnityEngine.Video;
    6. using UnityEngine.SceneManagement;
    7. public class SyncVideo : NetworkBehaviour
    8. {
    9.     public VideoPlayer videoPlayer;
    10.     [SerializeField]
    11.     private bool isPlaying = false;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.     }
    16.     void Awake()
    17.     {
    18.         SceneManager.sceneLoaded += OnSceneLoaded;
    19.     }
    20.     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    21.     {
    22.         videoPlayer = GameObject.Find("Plane").GetComponent<VideoPlayer>();
    23.     }
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (Input.GetKeyDown(KeyCode.P))
    28.         {
    29.             Debug.Log("Pressed the key P to play the video.");
    30.             if (isServer){
    31.                 RpcPlayVideo();
    32.             }
    33.            
    34.             else{
    35.                 CmdPlayVideo();
    36.                 Debug.Log("CmdPlayVideo");
    37.             }
    38.              
    39.         }
    40.     }
    41.     [ClientRpc]
    42.     void RpcPlayVideo()//server calls this method
    43.     {
    44.         Debug.Log("RpcPlayVideo");
    45.         StartPlaying();
    46.     }
    47.     [Command]
    48.     void CmdPlayVideo()//clients call this method
    49.     {
    50.         RpcPlayVideo();
    51.     }
    52.     void StartPlaying()
    53.     {
    54.         Debug.Log("Start playing");
    55.         //play or pause video locally
    56.         if (!isPlaying)
    57.         {
    58.             videoPlayer.Play();
    59.             isPlaying = true;
    60.         }
    61.     }
    62. }
     
    Last edited: Oct 2, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,360
    What makes you unable to do so? I doubt you mean it literally, so do you get an error or does the function not execute? Also, what network tech do you use? It doesn't look like NGO due to the [Command] attribute.

    Note that you may not want BOTH the client and the server to react to the P key. The server should not handle input. If the server is a client-host, it's literally also a client that talks to itself (as the server) and should therefore not require the server to test for the P key being pressed as well. Only clients should start the video with a key input, not the server. Perhaps that is the issue.