Last active
September 12, 2018 09:10
-
-
Save marcelschmidtdev/834644c220736d89bac8 to your computer and use it in GitHub Desktop.
Shows GameSparks API key in Unity window title (Windows)
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
#if UNITY_EDITOR_WIN | |
using UnityEngine; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using UnityEditor; | |
using System; | |
namespace GameSparks.Editor | |
{ | |
[InitializeOnLoad] | |
public static class GameSparksHelper | |
{ | |
const string GS_SETTINGS_PATH = "Path/To/Your/GS/Settings"; | |
[DllImport( "user32.dll" )] | |
private static extern IntPtr GetForegroundWindow (); | |
[DllImport( "user32.dll" )] | |
private static extern int GetWindowTextLength (IntPtr hWnd); | |
[DllImport( "user32.dll" )] | |
private static extern int GetWindowText (IntPtr hWnd, StringBuilder lpString, int nMaxCount); | |
[DllImport( "user32.dll" )] | |
private static extern bool SetWindowText (IntPtr hWnd, string lpString); | |
private static string currentScene; | |
static GameSparksHelper () { | |
currentScene = EditorApplication.currentScene; | |
EditorApplication.hierarchyWindowChanged += UpdateWindowTitle; | |
} | |
private static void UpdateWindowTitle () { | |
if (currentScene != EditorApplication.currentScene || EditorApplication.isPlayingOrWillChangePlaymode) { | |
currentScene = EditorApplication.currentScene; | |
var gsSettingsInstance = AssetDatabase.LoadAssetAtPath<GameSparksSettings>( GS_SETTINGS_PATH ); | |
var fieldInfo = gsSettingsInstance.GetType().GetField( "apiKey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ); | |
string apiKey = (string)fieldInfo.GetValue( gsSettingsInstance ); | |
if (string.IsNullOrEmpty( apiKey )) { | |
apiKey = "None"; | |
} | |
DisplayGSApiKey( apiKey ); | |
} | |
} | |
public static void DisplayGSApiKey (string apiKey) { | |
string title = GetWindowText(); | |
if (title.Contains( "GS Stage:" )) { | |
int index = title.IndexOf( " - GS Stage:" ); | |
title = title.Remove( index, title.Length - index ); | |
} | |
title += string.Format( " - GS Stage: {0}", apiKey ); | |
if (title.Contains( "Unity" )) { | |
SetWindowText( title ); | |
} | |
} | |
private static string GetWindowText () { | |
IntPtr hWnd = GetForegroundWindow(); | |
int length = GetWindowTextLength( hWnd ); | |
StringBuilder text = new StringBuilder( length + 1 ); | |
GetWindowText( hWnd, text, text.Capacity ); | |
return text.ToString(); | |
} | |
private static void SetWindowText (string text) { | |
IntPtr hWnd = GetForegroundWindow(); | |
SetWindowText( hWnd, text ); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment