Unity控制窗口全屏显示

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liuyanlei1990/article/details/90693511

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
public class MainScriptfull : MonoBehaviour
{
public Rect screenPosition;
[DllImport(“user32.dll”)]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
[DllImport(“user32.dll”)]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport(“kernel32”, CharSet = CharSet.Auto)]
public static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
[DllImport(“user32.dll”)]
static extern IntPtr GetActiveWindow();
const uint SWP_SHOWWINDOW = 0x0040;
const int GWL_STYLE = -16;
const int WS_BORDER = 1;
private int i = 0;
void Awake()
{
if (ReadScreenWidth() == 0 || ReadScreenHeight() == 0)
{
int width = Screen.currentResolution.width;
int height = Screen.currentResolution.height;
Debug.Log(width);
Debug.Log(height);
//写入配置文件
ToolReadInConfig.WriteIniContent(“version”, “UnityWindowWidth”, width.ToString());
ToolReadInConfig.WriteIniContent(“version”, “UnityWindowHeight”, height.ToString());
screenPosition.width = width;
screenPosition.height = height;
}
else
{
screenPosition.width = ReadScreenWidth();
screenPosition.height = ReadScreenHeight();
}
Screen.SetResolution(Screen.width, Screen.height, false);
}
// 初始化串口
void Start()
{
SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
SetWindowPos(GetActiveWindow(), -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
}

// Update is called once per frame
void Update()
{
    i++;
    if (i < 5)
    {
        SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
        SetWindowPos(GetActiveWindow(), -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
    }
}

public static int ReadScreenWidth()
{
    int ret = GetPrivateProfileInt("version", "UnityWindowWidth", 0, Application.streamingAssetsPath + "/config.ini");
    return ret;
}
public static int ReadScreenHeight()
{
    int ret = GetPrivateProfileInt("version", "UnityWindowHeight", 0, Application.streamingAssetsPath + "/config.ini");
    return ret;
}

}

展开阅读全文

没有更多推荐了,返回首页