Unity Engine Integration

Integrate KSS Dreamgate with your Unity game:

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Diagnostics;
using System.IO;
using System.Linq; // Required for LINQ extensions on arrays

/// <summary>
/// Handles command-line arguments on game startup and manages the connection 
/// to the external KSS SDK Tool (KSS Sdk.exe). This script should be attached 
/// to a main Game Object (like a GameManager) in your initial scene.
/// </summary>
public class GameLauncher : MonoBehaviour
{
    private string kssCallArgument = "";

    // -----------------------------------
    // CALL WHEN GAME STARTS (GDScript's _ready equivalent)
    // -----------------------------------
    void Start()
    {
        // 1. Get all command-line arguments
        // IMPORTANT: In C#, args[0] is the executable path itself. 
        // The first actual parameter is at index 1.
        string[] arguments = System.Environment.GetCommandLineArgs();
        
        // We look for arguments beyond the executable path.
        if (arguments.Length > 1)
        {
            // 2. Assign the first actual parameter (index 1) to the variable
            kssCallArgument = arguments[1];

            // 3. Use the received argument (e.g., check if it's "TRUE")
            if (kssCallArgument.ToUpper() == "TRUE")
            {
                // Your logic here when the argument is TRUE
                UnityEngine.Debug.Log("Successfully received the 'TRUE' flag. Performing special launch actions...");
                
                // 6️⃣ Continue with your main game initialization (e.g., load the main menu scene)
                // NOTE: Replace "MainMenuScene" with the actual name of your scene
                SceneManager.LoadScene("MainMenuScene");
            }
            else
            {
                UnityEngine.Debug.Log($"Argument received, but it was not 'TRUE'. Value: {kssCallArgument}");
                OnKSSDreamGateCall("FALSE");
                
                // If the game is starting normally, still load the main menu
                SceneManager.LoadScene("MainMenuScene");
            }
        }
        else
        {
            // This branch runs if the game was launched without any custom arguments
            UnityEngine.Debug.Log("No custom launch argument received. Running default startup.");
            OnKSSDreamGateCall("FALSE");
            
            // Continue with the rest of your game setup...
            SceneManager.LoadScene("MainMenuScene");
        }
    }

    /// <summary>
    /// Function to launch the KSS Sdk Tool and pass arguments.
    /// </summary>
    /// <param name="ksscall">The argument string to pass to the SDK.</param>
    public void OnKSSDreamGateCall(string ksscall)
    {
        // 1️⃣ Get current game PID
        int mainGamePid = Process.GetCurrentProcess().Id;

        // 2️⃣ Set your game ID 
        // NOTE: Use the exact string from your Godot code
        string gameId = "76881280464148176505";

        // 3️⃣ Build argument list for the SDK (string array in C#)
        // Note: The C# Process.Start method takes arguments as a single string, 
        // but we'll assemble the list first for clarity.
        string argumentsString = string.Join(" ", new string[] {
            gameId, 
            ksscall, 
            mainGamePid.ToString()
        });

        // 4️⃣ Compute the path to the SDK executable
        // We assume KSS Sdk.exe is in the same directory as the game executable.
        // AppDomain.CurrentDomain.BaseDirectory gives the executable's directory.
        string sdkDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
        string sdkPath = Path.Combine(sdkDirectory, "KSS Sdk.exe");

        // Check if the SDK path exists before attempting to launch
        if (!File.Exists(sdkPath))
        {
            UnityEngine.Debug.LogError($"FATAL ERROR: KSS Sdk Tool not found at expected location: {sdkPath}");
            return;
        }

        // 5️⃣ Launch SDK Tool in non-blocking mode
        try
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(sdkPath, argumentsString)
            {
                UseShellExecute = false, // Recommended for starting external executables
                CreateNoWindow = true    // Ensure no new console window pops up
            };

            Process.Start(startInfo);
            UnityEngine.Debug.Log($"KSS Sdk Tool launched successfully with args: {argumentsString}");
        }
        catch (System.Exception e)
        {
            // Launch failed
            UnityEngine.Debug.LogError($"FATAL ERROR: Failed to launch KSS Sdk Tool! Exception: {e.Message}");
        }
        
        // Optional: In Unity, you typically don't need 'yield' for a short delay 
        // after non-blocking process start, as the main game loop continues immediately.
    }
}

Log in to your Account

Join the Rebel Community of Gamers!

JOIN THE REBEL TRIBE!

Join the K-Magazine web tribe and receive occasional emails about our games, new products, promo codes and exiting news!

[newsletter_signup_form id=3]

By subscribing, you consent to receive emails from K-Magazine and K Storm Studio. You may revoke the consent at any time by clicking "unsubscribe" at the bottom of any email from us.

skull gamer k_newsletter popup k-magazine