Unity 游戏启动器

游戏不贵,不建议玩盗版游戏哟

下载游戏 "中国式相亲"(ChineseDating),解压后发现启动程序直接被defender给删除了,看了一下:ENIGMA(5.X)壳加了启动密码。
其实Unity 游戏主要逻辑在Managed/Assembly-CSharp.dll,启动程序主要通过加载UnityPlayer.dll调用UnityMain实现。
ok,让ai给出代码:


using System.Runtime.InteropServices;

public class UnityGameLauncher
{
    // Unity启动参数结构
    [StructLayout(LayoutKind.Sequential)]
    public struct UnityStartupArgs
    {
        public int version;
        public IntPtr gfxDevice;
        public int argc;
        public IntPtr argv;
        public IntPtr exitCallback;
    }

    [DllImport("UnityPlayer.dll", EntryPoint = "UnityInit")]
    private static extern int UnityInit(ref UnityStartupArgs args);

    [DllImport("UnityPlayer.dll", EntryPoint = "UnityMain")]
    private static extern int UnityMain(IntPtr hInstance, IntPtr hPrevInstance, string lpCmdLine, int nShowCmd);

    public static void LaunchGame(string gamePath)
    {
        // 保存当前目录
        string originalDir = Directory.GetCurrentDirectory();

        try
        {
            // 切换到游戏目录
            Directory.SetCurrentDirectory(gamePath);

            // 检查必要文件
            if (!File.Exists("UnityPlayer.dll"))
            {
                Console.WriteLine("UnityPlayer.dll not found!");
                return;
            }

            // 构建命令行参数
            string[] unityArgs = {
                "-batchmode",          // 无头模式(可选)
                "-nographics",         // 无图形界面(可选)
                "-force-d3d11",        // 强制使用DX11
                //"-screen-fullscreen",  // 全屏
                "-screen-width", "1920",
                "-screen-height", "1080",
                "-screen-quality", "Fantastic"
            };

            // 方法1: 简单调用UnityMain
            string cmdLine = string.Join(" ", unityArgs);
            int result = UnityMain(IntPtr.Zero, IntPtr.Zero, cmdLine, 1);

            // 方法2: 使用UnityInit(更底层控制)
            // 如果需要更细粒度的控制,可以使用UnityInit

            Console.WriteLine($"Game process ended with code: {result}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Launch failed: {ex.Message}");
        }
        finally
        {
            // 恢复原始目录
            Directory.SetCurrentDirectory(originalDir);
        }
    }

    static void Main(string[] args)
    {
        string gamePath = args.Length > 0 ? args[0] : AppDomain.CurrentDomain.BaseDirectory;

        if (!Directory.Exists(gamePath))
        {
            Console.WriteLine("Game directory not found!");
            return;
        }

        Console.WriteLine($"Launching game from: {gamePath}");
        LaunchGame(gamePath);
    }
}
posted @ 2025-12-11 20:17  DirWangK  阅读(15)  评论(0)    收藏  举报