GibTreaty

Photon: NetworkSerializationHandler usage example

Aug 10th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class FlashlightController : Photon.MonoBehaviour, INetworkHandler {
  4.  
  5.     public GameObject flashlight;
  6.  
  7.     public KeyCode toggleFlashlight = KeyCode.F;
  8.  
  9.     void OnEnable() {
  10.         GetComponent<NetworkSerializationHandler>().Hook(this);
  11.     }
  12.  
  13.     void OnDisable() {
  14.         GetComponent<NetworkSerializationHandler>().Unhook(this);
  15.     }
  16.  
  17.     void Update() {
  18.         if(photonView.isMine) {
  19.             if(Input.GetKeyDown(toggleFlashlight))
  20.                 flashlight.SetActive(!flashlight.activeSelf);
  21.  
  22.             RaycastHit hit;
  23.            
  24.             if(flashlight.activeSelf && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
  25.                 flashlight.transform.LookAt(hit.point);
  26.             else
  27.                 flashlight.transform.localRotation = Quaternion.identity;
  28.         }
  29.     }
  30.  
  31.     public void OnNetworkWrite(PhotonStream stream, PhotonMessageInfo info) {
  32.         stream.SendNext(flashlight.activeSelf);
  33.         stream.SendNext(flashlight.transform.rotation);
  34.     }
  35.  
  36.     public void OnNetworkRead(PhotonStream stream, PhotonMessageInfo info) {
  37.         flashlight.SetActive((bool)stream.ReceiveNext());
  38.         flashlight.transform.rotation = (Quaternion)stream.ReceiveNext();
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment