【问题标题】:Access to Unity3D-created application window's code访问 Unity3D 创建的应用程序窗口的代码
【发布时间】:2014-12-29 21:59:31
【问题描述】:

我是否可以访问创建 Unity 应用程序窗口的代码? 例如,在 WPF 中,您可以设置窗口的调整大小模式、最顶层、窗口启动状态等。 MainWindow.cs 继承自 System.Window 命名空间中的 Window 类,允许您设置所有这些内容。 我正在尝试找到允许我在 Unity3D 中做同样事情的类似类。

在 WPF 中

Unity3D 创建的应用程序窗口

【问题讨论】:

  • 是的,我没有尝试调整大小。你正在尝试修改整个窗口。就像我在示例中提到的,设置调整大小模式、窗口启动位置等。

标签: c# unity3d


【解决方案1】:

抱歉,后续跟进晚了,但是这也是可能的。要获得此功能,您需要使用 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);
    }
}

【讨论】:

  • 非常感谢,这是我一直在寻找的部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 2020-12-19
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多