discord-rpc/examples/button-clicker/Assets/DiscordController.cs

130 lines
3.5 KiB
C#
Raw Normal View History

using UnityEngine;
2017-10-17 22:33:12 +02:00
[System.Serializable]
public class DiscordJoinEvent : UnityEngine.Events.UnityEvent<string> { }
[System.Serializable]
public class DiscordSpectateEvent : UnityEngine.Events.UnityEvent<string> { }
[System.Serializable]
public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent<DiscordRpc.JoinRequest> { }
2017-10-14 12:21:35 +02:00
public class DiscordController : MonoBehaviour
{
public DiscordRpc.RichPresence presence;
public string applicationId;
public string optionalSteamId;
public int callbackCalls;
public int clickCounter;
2017-12-05 00:16:59 +01:00
public DiscordRpc.JoinRequest joinRequest;
2017-08-03 18:52:43 +02:00
public UnityEngine.Events.UnityEvent onConnect;
public UnityEngine.Events.UnityEvent onDisconnect;
2017-12-05 00:16:59 +01:00
public UnityEngine.Events.UnityEvent hasResponded;
2017-10-17 22:33:12 +02:00
public DiscordJoinEvent onJoin;
public DiscordJoinEvent onSpectate;
public DiscordJoinRequestEvent onJoinRequest;
2017-08-03 02:52:20 +02:00
DiscordRpc.EventHandlers handlers;
public void OnClick()
{
Debug.Log("Discord: on click!");
clickCounter++;
presence.details = string.Format("Button clicked {0} times", clickCounter);
DiscordRpc.UpdatePresence(ref presence);
}
2017-12-05 00:16:59 +01:00
public void RequestRespondYes()
{
Debug.Log("Discord: responding yes to Ask to Join request");
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.Yes);
hasResponded.Invoke();
}
public void RequestRespondNo()
{
Debug.Log("Discord: responding no to Ask to Join request");
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.No);
hasResponded.Invoke();
}
public void ReadyCallback()
{
++callbackCalls;
Debug.Log("Discord: ready");
2017-08-03 18:52:43 +02:00
onConnect.Invoke();
}
public void DisconnectedCallback(int errorCode, string message)
{
++callbackCalls;
Debug.Log(string.Format("Discord: disconnect {0}: {1}", errorCode, message));
2017-08-03 18:52:43 +02:00
onDisconnect.Invoke();
}
public void ErrorCallback(int errorCode, string message)
{
++callbackCalls;
Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message));
}
public void JoinCallback(string secret)
{
++callbackCalls;
Debug.Log(string.Format("Discord: join ({0})", secret));
2017-10-17 22:33:12 +02:00
onJoin.Invoke(secret);
}
public void SpectateCallback(string secret)
{
++callbackCalls;
Debug.Log(string.Format("Discord: spectate ({0})", secret));
2017-10-17 22:33:12 +02:00
onSpectate.Invoke(secret);
2017-10-14 12:21:35 +02:00
}
2017-10-14 12:12:03 +02:00
2017-11-30 21:23:40 +01:00
public void RequestCallback(ref DiscordRpc.JoinRequest request)
2017-10-14 12:21:35 +02:00
{
++callbackCalls;
Debug.Log(string.Format("Discord: join request {0}#{1}: {2}", request.username, request.discriminator, request.userId));
2017-12-05 00:16:59 +01:00
joinRequest = request;
2017-10-17 22:33:12 +02:00
onJoinRequest.Invoke(request);
2017-10-14 12:21:35 +02:00
}
2017-10-14 12:21:35 +02:00
void Start()
{
}
2017-10-14 12:21:35 +02:00
void Update()
{
2017-08-03 02:52:20 +02:00
DiscordRpc.RunCallbacks();
}
2017-10-14 12:21:35 +02:00
void OnEnable()
{
2017-08-03 02:52:20 +02:00
Debug.Log("Discord: init");
callbackCalls = 0;
2017-08-03 02:52:20 +02:00
handlers = new DiscordRpc.EventHandlers();
handlers.readyCallback = ReadyCallback;
handlers.disconnectedCallback += DisconnectedCallback;
handlers.errorCallback += ErrorCallback;
handlers.joinCallback += JoinCallback;
handlers.spectateCallback += SpectateCallback;
2017-10-14 12:21:35 +02:00
handlers.requestCallback += RequestCallback;
DiscordRpc.Initialize(applicationId, ref handlers, true, optionalSteamId);
}
void OnDisable()
{
Debug.Log("Discord: shutdown");
DiscordRpc.Shutdown();
}
void OnDestroy()
{
2017-10-14 12:21:35 +02:00
}
}