Skip to content

Instantly share code, notes, and snippets.

@nekomimi-daimao
Last active February 12, 2019 13:55
Show Gist options
  • Save nekomimi-daimao/919c7996424186a7c88ef93c377d1ab2 to your computer and use it in GitHub Desktop.
Save nekomimi-daimao/919c7996424186a7c88ef93c377d1ab2 to your computer and use it in GitHub Desktop.
PUN2のRaiseEventのラッパー.
using System;
using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
/// <summary>
/// RaiseEventのラッパー.
///
/// イベントの足し方
/// ① enumを追加
/// ② Actionを追加
/// ③ RaiseEvent受信時のイベントをenumに従って振り分け
/// ④ RESに送信メソッドを追加
/// </summary>
// RaiseEventReceiver
public class RER : SingletonMonoBehaviour<RER>
{
#region lifecycle
public void OnEnable()
{
PhotonNetwork.NetworkingClient.EventReceived += OnEvent;
}
public void OnDisable()
{
PhotonNetwork.NetworkingClient.EventReceived -= OnEvent;
}
#endregion
// ①
// eventCode. 0~199。0は特殊な扱いのため1から始める
public enum RaiseEventType : byte
{
SampleEvent = 1,
}
// ②
public Action<string> OnSampleEvent;
// ③
public void OnEvent(EventData photonEvent)
{
var type = (RaiseEventType) Enum.ToObject(typeof(RaiseEventType), photonEvent.Code);
Debug.Log("RaiseEvent Received. Type = " + type);
switch (type)
{
case RaiseEventType.SampleEvent:
OnSampleEvent?.Invoke(photonEvent.CustomData as string);
break;
default:
return;
}
}
}
// RaiseEventSender
public static class RES
{
// ④
public static void SendSampleEvent(string message)
{
var raiseEventOptions = new RaiseEventOptions
{
Receivers = ReceiverGroup.All,
CachingOption = EventCaching.AddToRoomCache,
};
PhotonNetwork.RaiseEvent((byte) RER.RaiseEventType.SampleEvent, message, raiseEventOptions, SendOptions.SendReliable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment