Photon Remote Procedure Calls
There could be the case were some events are trigger within the online collaborative space and all the users need to be aware of it. Photon helps you with this requirement using the Remote Procedure Calls (RPC) where you can assign a callback function to a network thread that will be executed on all clients.
This tutorial assumes you already know how to import Unity XR tool kits and Photon Engine in your project. If you don't, please follow this tutorial and come back when you have your Photon environment configured.
In this example will create a remote procedure to change the color of a cube in the online collaborative environment.
Create world space canvas UI and add a button.
2. Add a script component to the UI canvas and called it NetworkingUi. Also add Photon View Component
3. Open the NetworkingUi script in your code editor and copy-paste the following code.
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NetworkingUi : MonoBehaviourPun
{
[SerializeField] GameObject Remote_Cube;
// Start is called before the first frame update
void Start()
{
}
public void ChangeCubeColor()
{
// This is called when the local client uses the UI button
photonView.RPC("NetworkChangeCubeColor", RpcTarget.All, "jup", "and jup.");
}
[PunRPC]
// This method will be called on all the clients when the button in the UI is pressed
void NetworkChangeCubeColor(string a, string b, PhotonMessageInfo info)
{
if(Remote_Cube != null)
{
Remote_Cube.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f); // a random color will be assigned to the cube
}
}
}
4. In your scene add a 3D cube and place it where all the other users can observe it and add a photon view component to it
5. Assign the Cube to the NetworkingUi component in the Unity Editor.
8. From now on, if any of the online users press the UI button, the cube will change color on all the clients.