感觉开发多人游戏,启动多窗口进行测试很常见,默认情况一次双击只能启动一个窗口,然后还得给各个窗口拖拽调整位置,很麻烦。
研究了一下,这个过程可以自动化,一键启动多个窗口,并且位置不互相遮挡。
需要一个bat脚本
@ECHO OFF
START Release/Pc/Ram.exe 0_kk
ping 127.0.0.1 -n 2
START Release/Pc/Ram.exe 1_kkk
ping 127.0.0.1 -n 2
START Release/Pc/Ram.exe 2_kkkk
ping 127.0.0.1 -n 2
START Release/Pc/Ram.exe 3_kkkkk
exit
unity内一个c#
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
#if UNITY_STANDALONE_WIN
using System;
using System.Runtime.InteropServices;
#endif
public class ResetScreenSize : MonoBehaviour
{
public bool fullScreen = false;
private static string fullScreenKey = "Full Screen";
public int minDisplaySize = 384;
private static int monitorWidth;
private static int monitorHeight;
#if UNITY_STANDALONE_WIN
[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string text);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string className, string windowName);
public static IEnumerator SetWindowPosition(int x, int y)
{
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
SetWindowPos(FindWindow(null, "Ram" + WindowIndex), 0, x, y, 0, 0, 5);
}
public static int WindowIndex = -1;
public int WindowWidth = 512;
public float WindowRate = 9 / 16.0f;
public int WindowHeight = 512;
public int WindowPosX = 0;
public int WindowPosY = 0;
#endif
void Awake()
{
string[] args = System.Environment.GetCommandLineArgs();
// List<string> argList = new List<string>();
// argList.Add("-window 2_asdfasdf");
// argList.Add("-window 2|dasasdff");
// argList.Add("-window 3|asdf");
// argList.Add("-window 4|asdf");
// argList.Add("-winasfsdfdow asdfsdaf");
// string[] args = argList.ToArray();
WindowIndex = -1;
for (int i = 0; i < args.Length; i++)
{
// Debug.LogError(args[i]);
MatchCollection matche = Regex.Matches(args[i], @"\d_\w*");
foreach (Match item in matche)
{
string[] paramSplits = item.Value.Split('_');
WindowIndex = int.Parse(paramSplits[0]);
PlayerPrefs.SetString("LastAcount", paramSplits[1]);
}
}
WindowHeight = (int) (WindowWidth * WindowRate);
WindowPosX = WindowIndex % 2 * WindowWidth;
WindowPosY = WindowIndex / 2 * (WindowHeight + 20);
if (WindowIndex > -1)
{
SetWindowText(FindWindow(null, "Ram"), "Ram" + WindowIndex);
CheckAndSet();
}
}
void Start()
{
monitorWidth = Screen.resolutions[Screen.resolutions.Length - 1].width;
monitorHeight = Screen.resolutions[Screen.resolutions.Length - 1].height;
}
private void CheckAndSet()
{
// if (PlayerPrefs.GetInt(fullScreenKey, 0) >= 1)
// {
// SetFullScreen();
// }
// else
// {
SetWindowed();
// }
}
public void SetFullScreen()
{
Screen.SetResolution(monitorWidth, monitorHeight, true);
fullScreen = true;
PlayerPrefs.SetInt(fullScreenKey, 1);
PlayerPrefs.Save();
}
public void SetWindowed()
{
SetWindowResolution();
fullScreen = false;
PlayerPrefs.SetInt(fullScreenKey, 0);
PlayerPrefs.Save();
}
private void SetWindowResolution()
{
int multiplier = 1;
if (monitorWidth >= monitorHeight)
{
multiplier = monitorHeight / minDisplaySize;
if ((monitorHeight % minDisplaySize) == 0)
{
multiplier--;
}
}
else
{
multiplier = monitorWidth / minDisplaySize;
if ((monitorWidth % minDisplaySize) == 0)
{
multiplier--;
}
}
int size = minDisplaySize * multiplier;
if (size < minDisplaySize)
{
size = minDisplaySize;
}
Screen.SetResolution(WindowWidth, WindowHeight, false);
#if UNITY_STANDALONE_WIN
int x = monitorWidth / 2;
x -= size / 2;
int y = monitorHeight / 2;
y -= size / 2;
StartCoroutine(SetWindowPosition(WindowPosX, WindowPosY));
// Debug.LogError($"{WindowWidth} {WindowHeight} {WindowPosX} {WindowPosY}");
#endif
}
}
再加一个一键关闭所有的Bat
@ECHO OFF
taskkill /f /im Ram.exe
exit
这是一个初步的大致版本。拱有缘人讨论,是否有更好的方案。
后续调整完再来更新和添加使用说明。
(萌新每日一贴