Skip to content

Commit

Permalink
Merge pull request #23 from Ezioleq/rpc
Browse files Browse the repository at this point in the history
Fix and improve Discord Rich Presence (closes #12)
  • Loading branch information
ezioleq authored Oct 30, 2021
2 parents ecc6eec + 0480d95 commit f6386e9
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 70 deletions.
8 changes: 8 additions & 0 deletions Assets/Prefabs/Misc.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Assets/Prefabs/Misc/DiscordController.prefab
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
7 changes: 7 additions & 0 deletions Assets/Prefabs/Misc/DiscordController.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 57 additions & 44 deletions Assets/Scenes/Playground.unity
Original file line number Diff line number Diff line change
Expand Up @@ -948,49 +948,6 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 525302916}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &528520211
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 528520213}
- component: {fileID: 528520212}
m_Layer: 0
m_Name: DiscordController
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &528520212
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 528520211}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 61a4011ebbf9e80b8aa9264a4e3f5ea1, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &528520213
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 528520211}
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: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &558114238 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 4064790407114525993, guid: c330c6f4e0ef7e13aad222fd90fa490e, type: 3}
Expand Down Expand Up @@ -3236,7 +3193,6 @@ MonoBehaviour:
timeToHide: 2
flySpeed: 20
_launched: 0
_letHimFly: 0
_player: {fileID: 0}
hitPoint: {fileID: 922222242}
--- !u!4 &3018731287005278770 stripped
Expand Down Expand Up @@ -3509,6 +3465,63 @@ PrefabInstance:
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: e25180c3d83bbe2f481063e3c76e18d8, type: 3}
--- !u!1001 &5162814574573005862
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_RootOrder
value: 10
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalPosition.x
value: 13.930546
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalPosition.y
value: -0.285594
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalPosition.z
value: -27.32897
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549299, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5162814574346549301, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
propertyPath: m_Name
value: DiscordController
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 817d3f3ecc75733e2a12c35a497ed400, type: 3}
--- !u!1001 &8178442731781502288
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
87 changes: 64 additions & 23 deletions Assets/Scripts/DiscordController.cs
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();
}
}
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ PlayerSettings:
additionalCompilerArguments: {}
platformArchitecture: {}
scriptingBackend:
Standalone: 1
Standalone: 0
il2cppCompilerConfiguration:
Standalone: 2
managedStrippingLevel:
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
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)

0 comments on commit f6386e9

Please sign in to comment.