Created
September 24, 2021 09:38
-
-
Save sathyarajshetigar/c72b9d968550d8864b0de1744702328a to your computer and use it in GitHub Desktop.
FloatVariable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FloatVariable : ScriptableObject, IOnEventCallback | |
{ | |
private float _value; | |
public float Value => _value; | |
private void OnEnable() | |
{ | |
PhotonNetwork.AddCallbackTarget(this); | |
} | |
private void OnDisable() | |
{ | |
PhotonNetwork.RemoveCallbackTarget(this); | |
} | |
public void SetValue(float value) | |
{ | |
float oldVal = _value; | |
_value = value; | |
OnValueChange?.Invoke(oldVal, _value); | |
RaiseEvent(PunEvents.ON_SYNC_FLOAT, _value, new RaiseEventOptions() { CachingOption = EventCaching.AddToRoomCache, Receivers = ReceiverGroup.Others }); | |
} | |
private void RaiseEvent(byte eventCode, object content, RaiseEventOptions options) | |
{ | |
byte[] bytes = Compression.ObjectToByteArray(content); | |
Hashtable hash = new Hashtable() { { "data", bytes }, { "time", DateTime.Now.ToString("HH:mm:ss.ffffff") } }; | |
bool success = PhotonNetwork.RaiseEvent(eventCode, hash, options, SendOptions.SendReliable); | |
PhotonNetwork.SendAllOutgoingCommands(); | |
} | |
public void OnEvent(EventData photonEvent) | |
{ | |
if (photonEvent.Code < 200)//events higher than 200 resevered for pun events | |
{ | |
Game.Log($"OnEvent Code {photonEvent.Code} CustomData {photonEvent.CustomData}"); | |
object usableContent = null; | |
if (photonEvent.CustomData != null && (((Hashtable)photonEvent.CustomData)["data"]) != null) | |
{ | |
byte[] byteArray = (byte[])((Hashtable)photonEvent.CustomData)["data"]; | |
usableContent = (byteArray != null) ? Compression.ByteArrayToObject(byteArray) : null; | |
} | |
if(photonEvent.Code == PunEvents.ON_SYNC_FLOAT) | |
{ | |
float oldVal = _value; | |
_value = (float)usableContent; | |
OnValueChange?.Invoke(oldVal, _value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment