Created
August 28, 2018 06:15
-
-
Save ZeroByter/7a4f2987c9307dbd3b9835c12b6c55bd to your computer and use it in GitHub Desktop.
Fixing multiple steam clients issue
This file contains hidden or 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
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| // | |
| // This class takes care of a lot of stuff for you. | |
| // | |
| // 1. It initializes steam on startup. | |
| // 2. It calls Update so you don't have to. | |
| // 3. It disposes and shuts down Steam on close. | |
| // | |
| // You don't need to reference this class anywhere or access the client via it. | |
| // To access the client use Facepunch.Steamworks.Client.Instance, see SteamAvatar | |
| // for an example of doing this in a nice way. | |
| // | |
| public class SteamClient : MonoBehaviour | |
| { | |
| public uint AppId; | |
| private Facepunch.Steamworks.Client client; | |
| private static bool Instantiated; | |
| void Awake () | |
| { | |
| if (Instantiated) { //if a steam client was already instantied, we destroy our gameobject and stop the code | |
| Destroy(gameObject); | |
| return; | |
| } | |
| // keep us around until the game closes | |
| DontDestroyOnLoad(gameObject); | |
| //we mark in a static bool that we already have one steam client instantied | |
| Instantiated = true; | |
| if (AppId == 0) | |
| throw new System.Exception("You need to set the AppId to your game"); | |
| // | |
| // Configure us for this unity platform | |
| // | |
| Facepunch.Steamworks.Config.ForUnity( Application.platform.ToString() ); | |
| // | |
| // Create a steam_appid.txt (this seems greasy as fuck, but this is exactly | |
| // what UE's Steamworks plugin does, so fuck it. | |
| // | |
| try | |
| { | |
| System.IO.File.WriteAllText("steam_appid.txt", AppId.ToString()); | |
| } | |
| catch ( System.Exception e ) | |
| { | |
| Debug.LogWarning("Couldn't write steam_appid.txt: " + e.Message ); | |
| } | |
| // Create the client | |
| client = new Facepunch.Steamworks.Client( AppId ); | |
| if ( !client.IsValid ) | |
| { | |
| client = null; | |
| Debug.LogWarning("Couldn't initialize Steam"); | |
| return; | |
| } | |
| Debug.Log( "Steam Initialized: " + client.Username + " / " + client.SteamId ); | |
| } | |
| void Update() | |
| { | |
| if (client == null) | |
| return; | |
| try | |
| { | |
| UnityEngine.Profiling.Profiler.BeginSample("Steam Update"); | |
| client.Update(); | |
| } | |
| finally | |
| { | |
| UnityEngine.Profiling.Profiler.EndSample(); | |
| } | |
| } | |
| private void OnDestroy() | |
| { | |
| if (client != null) | |
| { | |
| client.Dispose(); | |
| client = null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment