Lachee / discord-rpc-csharp

C# custom implementation for Discord Rich Presence. Not deprecated and still available!
MIT License
560 stars 94 forks source link

[Unity] Run in Edit Mode #133

Closed youknowedo closed 3 years ago

youknowedo commented 3 years ago

Hey, trying to make an Editor Rich Presence thing and I've downloaded the Unity Package. I need a [InitializeOnLoad] class so I cant use the DiscordManager for this (plus, it has a lot of gameObject and "only run in Play mode" stuff I don't wanna reprogram), so I tried to just use the library using DiscordRPC; and just made a script with your instructions in the docs but when I run it, nothing happens in discord. Am I doing something wrong or doesn't the Unity Package support using Standard C# Library code?

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEditor;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using DiscordRPC;

namespace ERPC
{
    [InitializeOnLoad]
    public class ERPC : MonoBehaviour
    {
        public static DiscordRpcClient Client { get; private set; }

        public static string projectName;
        public static string sceneName;

        public static string applicationID = "821629735229980702";

        public static string details;
        public static string state;
        public static string largeImageKey = "ab";
        public static string largeImageText = "Arthur Blue";
        public static string smallImageKey = "Unity";
        public static string smallImageText = "Made with Unity";

        public static bool showProjectName;
        public static bool showSceneName;
        public static bool resetOnSceneChange;

        static ERPC()
        {

            projectName = Application.productName;
            sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
            Client = new DiscordRpcClient(applicationID);  //Creates the client
            Client.Initialize();                            //Connects the client
            Debug.Log("[ERP] Initialized Client");
        }

        void Update()
        {
            projectName = Application.productName;
            sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;

            if (!ERPCWindow.customDetailsState)
            {
                details = projectName;
                state = sceneName;
            }
        }

        public static void UpdateActivity()
        {
            //Set Presence
            Client.SetPresence(new RichPresence()
            {
                Details = details,
                State = state,
                Assets = new Assets()
                {
                    LargeImageKey = largeImageKey,
                    LargeImageText = largeImageText,
                    SmallImageKey = smallImageKey,
                    SmallImageText = smallImageText
                }
            });
            Debug.Log("[ERP] Updated Presence");
        }
    }
}
#endif

All variables are updated in the custom editor ERPCWindow so I know that it isn't due to the lack of data

Lachee commented 3 years ago

Its likely you need to use the Native Pipe implementation to get it to work with Unity3D.

youknowedo commented 3 years ago

ok thanks

youknowedo commented 3 years ago

so i dont really get what u mean. i looked in the files for the unity package and it already includes the library for rpc. what do i need to do?

Lachee commented 3 years ago

so i dont really get what u mean. i looked in the files for the unity package and it already includes the library for rpc. what do i need to do?

you need to tell it to use the UnityNamedPipe instead of the default system. Look at how the client is initialised in the sample unity plugin.

youknowedo commented 3 years ago

oh ok thanks