I need help with this script. I need for everyone to see a Canva SlideShow play in real time using Photon Fusion from a web browser. However everyone can’t see the slideshow in real time.
using UnityEngine;
using Vuplex.WebView;
using System.Collections;
using Fusion;
public class AutoPlay : NetworkBehaviour
{
public CanvasWebViewPrefab canvasWebViewPrefab;
public GameObject quad;
private double networkStartTime;
void Start()
{
RPC_SyncWithServerTime();
// Enable autoplay for videos with audio
Web.SetAutoplayEnabled(true);
if (canvasWebViewPrefab != null && quad != null)
{
// Ensure the WebViewPrefab is initialized and loaded
canvasWebViewPrefab.Initialized += (sender, e) => {
// Create a material from the WebView
Material webViewMaterial = canvasWebViewPrefab.WebView.CreateMaterial();
// Apply the material to the Quad's renderer
quad.GetComponent<Renderer>().material = webViewMaterial;
// Load the specific Canva URL
canvasWebViewPrefab.WebView.LoadUrl("https://www.canva.com/design/DAFXbvStASQ/HYqWLkH25um-jjqfNq19AA/edit");
// Wait for the page to load completely, then click the play button
canvasWebViewPrefab.WebView.LoadProgressChanged += (s, ev) => {
if (ev.Progress == 1.0f) // Page load complete
{
// Optionally, we can execute JavaScript to click the play button
canvasWebViewPrefab.WebView.ExecuteJavaScript(@"
document.querySelector('[aria-label=""Play""]').click();
");
// Set the network start time using UnityEngine.Time.time
networkStartTime = Time.timeAsDouble;
RPC_SetNetworkStartTime(networkStartTime);
}
};
};
}
else
{
Debug.LogError("CanvasWebViewPrefab or Quad is not assigned.");
}
}
void Update()
{
// Check for VR input to trigger the play button
if (OVRInput.GetDown(OVRInput.Button.One))
{
// Call the RPC method to click the play button for all clients
RPC_ClickPlayButton();
}
// Check for VR input to trigger the video button
if (OVRInput.GetDown(OVRInput.Button.Two))
{
// Call the RPC method to click the video button for all clients
RPC_ClickVideoButton();
}
}
[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
public void RPC_ClickPlayButton()
{
StartCoroutine(ClickPlayButton());
}
[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
private void RPC_ClickVideoButton()
{
StartCoroutine(ClickVideoButton());
}
[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
public void RPC_SetNetworkStartTime(double startTime)
{
networkStartTime = startTime;
}
[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
public void RPC_SyncWithServerTime()
{
StartCoroutine(SyncWithServerTime());
}
IEnumerator ClickPlayButton()
{
// Wait for the WebView to be ready
while (!canvasWebViewPrefab.WebView.IsInitialized)
{
yield return null;
}
// Execute JavaScript to click the play button
canvasWebViewPrefab.WebView.ExecuteJavaScript(@"
document.querySelector('[aria-label=""Play""]').click();
");
}
IEnumerator ClickVideoButton()
{
// Wait for the WebView to be ready
while (!canvasWebViewPrefab.WebView.IsInitialized)
{
yield return null;
}
// Execute JavaScript to click the video button
canvasWebViewPrefab.WebView.ExecuteJavaScript(@"
document.querySelector('.jXCxjw .NI12hw .FLDMzw').click();
");
}
IEnumerator SyncWithServerTime()
{
// Wait for the WebView to be ready
while (!canvasWebViewPrefab.WebView.IsInitialized)
{
yield return null;
}
// Synchronize the playback with the network start time
double timeDifference = Time.timeAsDouble - networkStartTime;
canvasWebViewPrefab.WebView.ExecuteJavaScript($@"
var video = document.querySelector('video');
if (video) {{
video.currentTime += {timeDifference};
video.play();
}}
");
}
}