Steamworks Dev Tool

February 6, 2023 ยท 3 minutes read

Here’s sample of a small dev tool I created to test the Facepunch’s Steamworks wrapper.
It’s only used inside the editor, so it will never end up in the build.

๐Ÿ“:This is pretty much the first time I am dealing more with async calls. Which is more than welcome, since I want to get more into backend programming in future. I am aware of the theory when it comes to working with async, but haven’t had the opportunity to actually use them that much before.

The script just creates few buttons that ask for confirmations before actually sending out the requests.

Example of the documentation pop-up

You are required to have:

  • Steam client running
  • Steamworks DLLs inside your Unity project for this to work.

These can be found on the Facepunch’s Steamworks GitHub release.

#if UNITY_EDITOR
using System;
using Steamworks.Data;
using UnityEditor;
using UnityEngine;

namespace Steamworks
{
	[CustomEditor(typeof(SteamworksManager))]
	public class SteamManagerDevTool : Editor
	{
	private Achievement _ach;
	private GUIContent _resetAll;
	private GUIContent _resetStats;
	private GUIContent _getAllAchievements;
	private GUIContent _getAllPlayers;
	private GUIContent _unlockAchievement;
	public override void OnInspectorGUI()
	{
		//  This can be replaced with another way to check if Steam client is running.
		if (!SteamworksManager.IsInstantiated) return;
		DrawDefaultInspector();

		_getAllPlayers = new GUIContent("Get All Current Players Globally",
				"Gets the amount of players who are currently playing the game globally.");
		_resetStats = new GUIContent("RESET STATS ONLY", "Reset only the stats");
		_resetAll = new GUIContent("RESET EVERYTHING", "Reset all stats and achievements");
		_getAllAchievements = new GUIContent("Get All Achievements", "Gives all achievements");
		_unlockAchievement = new GUIContent("Unlock Achievement", "Testing call for achievement");

		ResetEverythingButton(_resetAll);
		ResetStatsOnlyButton(_resetStats);
		GetAllAchievements(_getAllAchievements);
		GetAllPlayers(_getAllPlayers);
		UnlockAchievement(_unlockAchievement);
	}

	/// <summary>
	/// Reset only stats.
	/// </summary>
	private static void ResetStatsOnlyButton(GUIContent resetStats)
	{
		if (!GUILayout.Button(resetStats)) return;
		if (EditorUtility.DisplayDialog("Confirm Steam Stats Reset",
				"Are you sure you want to reset all stats?", "Yes", "Cancel"))
		{
			SteamUserStats.ResetAll(false);
			Debug.Log("Reset stats only");
		}
	}

	/// <summary>
	/// Reset everything, including achievements.
	/// </summary>
	private static void ResetEverythingButton(GUIContent resetAll)
	{
		if (!GUILayout.Button(resetAll)) return;
		if (EditorUtility.DisplayDialog("Confirm Steam Data Reset",
				"Are you sure you want to reset all data?", "Yes", "Cancel"))
		{
			SteamUserStats.ResetAll(true);
			Debug.Log("Reset everything");
		}
	}

	/// <summary>
	/// Gives all possible achievements 
	/// </summary>
	private void GetAllAchievements(GUIContent getAllAchievements)
	{
		if (!GUILayout.Button(getAllAchievements)) return;
		if (EditorUtility.DisplayDialog("Confirm Get Achievements",
				"Are you sure you want to get all achievements?", "Yes", "Cancel"))
		{
			Debug.Log("Getting all achievements");
			foreach (var achievement in SteamUserStats.Achievements)
			{
				Debug.Log($"Name: {achievement.Identifier}");
				achievement.Trigger();
			}
			if (_ach.State)
			{
				Debug.Log($"{_ach.Name} WAS UNLOCKED!");
			}
		}
	}
	// This is mainly just for my own fun to see how many potential devs are running Spacewar with me
	private static void GetAllPlayers(GUIContent getAllPlayers)
	{
		var currentPlayers = SteamUserStats.PlayerCountAsync();
		if (!GUILayout.Button(getAllPlayers) || currentPlayers.IsCanceled) return;
		Debug.Log($"Amount of players: {currentPlayers.Result.ToString()}");
		if (currentPlayers.IsFaulted)
		{
			Debug.Log("Couldn't get current players.");
		}
		// We should be able to dispose after all this
		currentPlayers.Dispose();
	}
	private static void UnlockAchievement(GUIContent unlockAchievement)
	{
		if (!GUILayout.Button(unlockAchievement)) return;
		try
		{
			SteamAchievements.TriggerAchievement("ACH_TRAVEL_FAR_SINGLE");
		}
		catch (Exception e)
		{
			Console.WriteLine(e);
			throw;
		}
	}
}
}
 #endif

Info Circle Times Circle Terminal E-Mail Download GitHub Square Alternate GitHub Menu Check Circle Bar Chart Space Shuttle Steam Twitter Square Unity 3D angle-right Warning