抱歉,后续跟进晚了,但是这也是可能的。要获得此功能,您需要使用 user32.dll。请记住,这是基于 Windows 的。
因为这只会在独立的 windows 和统一的编辑器中才需要使用,所以值得使用
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
现在你想使用 user32.dll 中给定的窗口位置,所以首先你会从 dll 中导入这个函数
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
并将其绑定到变量
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfterint x, int Y, int cx, int cy, int wFlags);
您还想找到窗口,可以通过类似的方式完成。但不要忘记将当前的“WindowTitle”更改为您的实际窗口标题。
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(System.String className, System.String windowName);
现在剩下的就是一个要调用的函数,所以你可以实际设置窗口的位置。类似于以下内容
public static void SetPosition(int x, int y, int resX = 0, int resY = 0)
{
SetWindowPos(FindWindow(null, "WindowTitle"), 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}
当然,不要忘记结束你的#if
#endif
现在您可以像这样在 Awake/OnEnable 中调用 Setposition 函数
void Awake()
{
SetPosition(0,0);
}
调整大小等其他功能可能有点挑战,但并非不可能。您可以查看msdn windowFunctions 了解更多信息
以防万一。这是窗口位置的完整功能副本。只需将 WindowModifier 组件附加到游戏对象,将窗口标题更改为适当的标题,以及您想要的位置
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class WindowModifier: MonoBehaviour
{
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(System.String className, System.String windowName);
public static void SetPosition(int x, int y, int resX = 0, int resY = 0)
{
SetWindowPos(FindWindow(null, "My window title"), 0, x, y, resX, resY, resX * resY == 0 ? 1 : 0);
}
#endif
void Awake ()
{
SetPosition(0,0);
}
}