-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Ezioleq/rpc
Fix and improve Discord Rich Presence (closes #12)
- Loading branch information
Showing
7 changed files
with
194 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!1 &5162814574346549301 | ||
GameObject: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
serializedVersion: 6 | ||
m_Component: | ||
- component: {fileID: 5162814574346549299} | ||
- component: {fileID: 5162814574346549298} | ||
m_Layer: 0 | ||
m_Name: DiscordController | ||
m_TagString: Untagged | ||
m_Icon: {fileID: 0} | ||
m_NavMeshLayer: 0 | ||
m_StaticEditorFlags: 0 | ||
m_IsActive: 1 | ||
--- !u!4 &5162814574346549299 | ||
Transform: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
m_GameObject: {fileID: 5162814574346549301} | ||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} | ||
m_LocalPosition: {x: 13.930546, y: -0.285594, z: -27.32897} | ||
m_LocalScale: {x: 1, y: 1, z: 1} | ||
m_Children: [] | ||
m_Father: {fileID: 0} | ||
m_RootOrder: 0 | ||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} | ||
--- !u!114 &5162814574346549298 | ||
MonoBehaviour: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
m_GameObject: {fileID: 5162814574346549301} | ||
m_Enabled: 1 | ||
m_EditorHideFlags: 0 | ||
m_Script: {fileID: 11500000, guid: 61a4011ebbf9e80b8aa9264a4e3f5ea1, type: 3} | ||
m_Name: | ||
m_EditorClassIdentifier: | ||
_clientId: 855876148675346442 | ||
_largeImage: icon | ||
_largeText: I'm drowning, help | ||
_randomStates: | ||
- Doing tests | ||
- Dying for science | ||
- Running with cubes | ||
- Where's the portal? | ||
- Thinking how to... | ||
- Searching for the cake |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,80 @@ | ||
using System; | ||
using UnityEngine; | ||
using Discord; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
public class DiscordController : MonoBehaviour { | ||
public Discord.Discord discord; | ||
private static DiscordController _instance; | ||
public static DiscordController Instance { get { return _instance; } } | ||
|
||
[SerializeField] private long _clientId; | ||
[SerializeField] private string _largeImage; | ||
[SerializeField] private string _largeText; | ||
[SerializeField] private string[] _randomStates; | ||
|
||
private Discord.Discord _discord; | ||
private Discord.ActivityManager _activityManager; | ||
private ActivityTimestamps _activityTimestamp; | ||
|
||
private ActivityAssets _activityAsset; | ||
|
||
void Awake() { | ||
DontDestroyOnLoad(this); | ||
if (FindObjectsOfType(GetType()).Length > 1) | ||
Destroy(this); | ||
private void Awake() { | ||
// Singleton | ||
if (_instance != null && _instance != this) | ||
Destroy(this.gameObject); | ||
else | ||
_instance = this; | ||
|
||
// Create new Discord instance | ||
_discord = new Discord.Discord(_clientId, (System.UInt64)Discord.CreateFlags.NoRequireDiscord); | ||
_activityManager = _discord.GetActivityManager(); | ||
|
||
// Set default activity assets (this big picture on Discord's RPC) | ||
_activityAsset = new ActivityAssets { | ||
LargeImage = _largeImage, | ||
LargeText = _largeText | ||
}; | ||
|
||
// Set start timestamp | ||
_activityTimestamp = new ActivityTimestamps { | ||
Start = System.DateTimeOffset.Now.ToUnixTimeSeconds() | ||
}; | ||
|
||
discord = new Discord.Discord(855876148675346442, (UInt64)Discord.CreateFlags.Default); | ||
var activityManager = discord.GetActivityManager(); | ||
// First activity | ||
var activity = new Activity { | ||
State = "Doing tests", | ||
Details = "Chamber 01", | ||
Assets = new ActivityAssets { | ||
LargeImage = "icon", | ||
LargeText = "I'm drowning, help" | ||
}, | ||
Timestamps = new ActivityTimestamps { | ||
Start = DateTimeOffset.Now.ToUnixTimeSeconds() | ||
} | ||
Assets = _activityAsset, | ||
Timestamps = _activityTimestamp | ||
}; | ||
activityManager.UpdateActivity(activity, (res) => { | ||
|
||
// Set first activity | ||
_activityManager.UpdateActivity(activity, (res) => { | ||
if (res == Discord.Result.Ok) | ||
Debug.Log("Discord RPC OK"); | ||
Debug.Log("Initialized Discord RPC"); | ||
}); | ||
|
||
// Listen to current scene changes | ||
SceneManager.activeSceneChanged += OnSceneChange; | ||
} | ||
|
||
void Update() { | ||
discord.RunCallbacks(); | ||
private void Update() { | ||
_discord.RunCallbacks(); | ||
} | ||
|
||
private void OnSceneChange(Scene current, Scene next) { | ||
var activity = new Activity { | ||
Details = next.name, | ||
// Get random state, just for variety | ||
State = _randomStates[Random.Range(0, _randomStates.Length-1)], | ||
Assets = _activityAsset, | ||
Timestamps = _activityTimestamp | ||
}; | ||
|
||
_activityManager.UpdateActivity(activity, (res) => { | ||
if (res == Discord.Result.Ok) | ||
Debug.Log("Scene changed, RPC Updated"); | ||
}); | ||
} | ||
|
||
private void OnApplicationQuit() { | ||
discord.Dispose(); | ||
_discord.Dispose(); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
m_EditorVersion: 2020.3.20f1 | ||
m_EditorVersionWithRevision: 2020.3.20f1 (41c4e627c95f) | ||
m_EditorVersion: 2020.3.11f1 | ||
m_EditorVersionWithRevision: 2020.3.11f1 (99c7afb366b3) |