⭐Unity 程序运行时默认出现在屏幕的左上角

using System.Runtime.InteropServices;
using UnityEngine;

public class WindowPosition : MonoBehaviour
{
    [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(string className, string windowName);

    void Start()
    {
        PositionWindow();
    }

    void PositionWindow()
    {
        // The window name can vary depending on the title you have set for your game window.
        IntPtr windowPtr = FindWindow(null, "Your Unity Application Name");
        if (windowPtr != IntPtr.Zero)
        {
            // Positions the window at the top-left corner of the screen with a size of 800x600.
            // You can adjust these values according to your needs.
            SetWindowPos(windowPtr, 0, 0, 0, 800, 600, 0x0040);
        }
    }
}

Unity自身并不直接提供设置窗口位置的API,但是你可以通过调用操作系统的API来达到目的。

首先,你需要在Unity项目中的C#脚本中声明对Windows API的调用。这通常涉及到使用DllImport来引入user32.dll库中的函数,比如SetWindowPos,这个函数可以用来设置窗口的位置和大小。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值