When player touch the sample cube and press B, center cube turn blue.
When player touch the sample cube and press R, center cube turn red.
Use photon to synchronize
Step 1: Make Player Move
Add MoveCube to Player
using UnityEngine;using System.Collections;using Photon.Pun;// This script moves the character controller forward// and sideways based on the arrow keys.// It also jumps when pressing space.// Make sure to attach a character controller to the same game object.// It is recommended that you make only one call to Move or SimpleMove per frame.publicclassMoveCube:MonoBehaviourPun{CharacterController characterController;publicfloat speed =6.0f;publicfloat jumpSpeed =8.0f;publicfloat gravity =20.0f;privateVector3 moveDirection = Vector3.zero;voidStart(){
characterController =GetComponent<CharacterController>();}voidUpdate(){if(characterController.isGrounded&&
PhotonNetwork.IsConnected&&//check whether connect
photonView.IsMine)//check whether is mine{// We are grounded, so recalculate// move direction directly from axes
moveDirection =newVector3(-Input.GetAxis("Vertical"),0.0f, Input.GetAxis("Horizontal"));
moveDirection *= speed;if(Input.GetButton("Jump")){
moveDirection.y = jumpSpeed;}}// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied// as an acceleration (ms^-2)
moveDirection.y -= gravity * Time.deltaTime;// Move the controller
characterController.Move(moveDirection * Time.deltaTime);}}
3.1. Create an empty gameobject and name it as NetworkLauncher
3.2. Attach the NetworkLauncher script to the NetworkLauncehr
using System.Collections;using System.Collections.Generic;using UnityEngine;using Photon.Pun;publicclassNetworkLauncher:MonoBehaviourPunCallbacks{// Start is called before the first frame updatevoidStart(){
PhotonNetwork.ConnectUsingSettings();}publicoverridevoidOnConnectedToMaster(){base.OnConnectedToMaster();
Debug.Log("Welcome");
PhotonNetwork.JoinOrCreateRoom("Room",newPhoton.Realtime.RoomOptions(){ MaxPlayers =2},default);}publicoverridevoidOnJoinedRoom(){base.OnJoinedRoom();GameObject obj = PhotonNetwork.Instantiate("Player",newVector3(2.84f,1f,-4.79f),
Quaternion.identity,0);
obj.tag ="Player";}}
3.2. Drag the Player to the resource directory in photon
3.3. Add photon component to the Player
Remember drag the “Photon Transform View” to the Observed Components.
Remember to override the prefab.
3.4. Delete the Player in hierarchy
4. Run the project to check whether generate the player
4.1. Build another window to check sychronize
We find that the player position is synchronized but the center cube color is not sychronze.
5. Add this two component to the center cube
5.1. Write code in PhotonColorNumView
using Photon.Pun;using System.Collections;using System.Collections.Generic;using UnityEngine;using Object = System.Object;[RequireComponent(typeof(PhotonView))][RequireComponent(typeof(CenterCube))][AddComponentMenu("Photon Networking/Photon ColorNum View")]publicclass photonColorNumView : MonoBehaviour, IPunObservable
{[SerializeField]bool m_SynchronizeColorNum =true;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}publicvoidOnPhotonSerializeView(PhotonStream stream,PhotonMessageInfo info){if(stream.IsWriting){if(m_SynchronizeColorNum){int colorNum =GetComponent<CenterCube>().curMaterial;
stream.SendNext(colorNum);}}else{if(m_SynchronizeColorNum){GetComponent<MeshRenderer>().material =GetComponent<CenterCube>().material[(int)stream.ReceiveNext()];}}}}
Drag the script to the photon view
6. Build the project
Only the window build first can control the color
6.1. Add this line to Center Cube, make sure the player touch the object is the master client
using Photon.Pun;using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassCenterCube:MonoBehaviourPun{public Material[] material;publicint curMaterial;privatevoidAwake(){
curMaterial =2;}privatevoidOnTriggerStay(Collider other){//Add this line
PhotonNetwork.SetMasterClient(PhotonNetwork.LocalPlayer);if(other.CompareTag("Player")){
Debug.Log("In");if(Input.GetKeyDown(KeyCode.B)){GetComponent<MeshRenderer>().material = material[0];
curMaterial =0;}elseif(Input.GetKeyDown(KeyCode.R)){GetComponent<MeshRenderer>().material = material[1];
curMaterial =1;}}}}