unity3d程序最前显示

一,方法一

参考链接
在这里插入图片描述

#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
 
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
 
 
public class AlwaysOnTop : MonoBehaviour {
    #region WIN32API
 
    public static readonly System.IntPtr HWND_TOPMOST = new System.IntPtr(-1);
    public static readonly System.IntPtr HWND_NOT_TOPMOST = new System.IntPtr(-2);
    const System.UInt32 SWP_SHOWWINDOW = 0x0040;
 
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT {
        public int Left, Top, Right, Bottom;
 
        public RECT(int left, int top, int right, int bottom) {
            Left = left;
            Top = top;
            Right = right;
            Bottom = bottom;
        }
 
        public RECT(System.Drawing.Rectangle r)
            : this(r.Left, r.Top, r.Right, r.Bottom) {
        }
 
        public int X {
            get {
                return Left;
            }
            set {
                Right -= (Left - value);
                Left = value;
            }
        }
 
        public int Y {
            get {
                return Top;
            }
            set {
                Bottom -= (Top - value);
                Top = value;
            }
        }
 
        public int Height {
            get {
                return Bottom - Top;
            }
            set {
                Bottom = value + Top;
            }
        }
 
        public int Width {
            get {
                return Right - Left;
            }
            set {
                Right = value + Left;
            }
        }
 
        public static implicit operator System.Drawing.Rectangle(RECT r) {
            return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
        }
 
        public static implicit operator RECT(System.Drawing.Rectangle r) {
            return new RECT(r);
        }
    }
 
    [DllImport("user32.dll", SetLastError = true)]
    private static extern System.IntPtr FindWindow(String lpClassName, String lpWindowName);
 
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
 
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetWindowPos(System.IntPtr hWnd, System.IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
 
    #endregion
 
 
    // Use this for initialization
    void Start() {
        AssignTopmostWindow(CONSTANT_WINDOW_TITLE_FROM_GAME, true);
    }
 
    public bool AssignTopmostWindow(string WindowTitle, bool MakeTopmost) {
        UnityEngine.Debug.Log("Assigning top most flag to window of title: " + WindowTitle);
 
        System.IntPtr hWnd = FindWindow((string)null, WindowTitle);
 
        RECT rect = new RECT();
        GetWindowRect(new HandleRef(this, hWnd), out rect);
 
        return SetWindowPos(hWnd, MakeTopmost ? HWND_TOPMOST : HWND_NOT_TOPMOST, rect.X, rect.Y, rect.Width, rect.Height, SWP_SHOWWINDOW);
    }
 
    private string[] GetWindowTitles() {
        List<string> WindowList = new List<string>();
 
        Process[] ProcessArray = Process.GetProcesses();
        foreach (Process p in ProcessArray) {
            if (!IsNullOrWhitespace(p.MainWindowTitle)) {
                WindowList.Add(p.MainWindowTitle);
            }
        }
 
        return WindowList.ToArray();
    }
 
    public bool IsNullOrWhitespace(string Str) {
        if (Str.Equals("null")) {
            return true;
        }
        foreach (char c in Str) {
            if (c != ' ') {
                return false;
            }
        }
        return true;
    }
}
#endif
System.Drawing.dll 在这个位置找到:

Program Files\Unity542p4\Editor\Data\Mono\lib\mono\2.0

CONSTANT_WINDOW_TITLE_FROM_GAME

指代:
在这里插入图片描述

二,方法二

参考链接

using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class WindowMod : MonoBehaviour
{
    public enum appStyle
    {
        FullScreen,
        WindowedFullScreen,
        Windowed,
        WindowedWithoutBorder
    }
    public enum zDepth
    {
        Normal,
        Top,
        TopMost
    }
    private const uint SWP_SHOWWINDOW = 64u;
    private const int GWL_STYLE = -16;
    private const int WS_BORDER = 1;
    private const int GWL_EXSTYLE = -20;
    private const int WS_CAPTION = 12582912;
    private const int WS_POPUP = 8388608;
    private const int SM_CXSCREEN = 0;
    private const int SM_CYSCREEN = 1;
    public WindowMod.appStyle AppWindowStyle = WindowMod.appStyle.WindowedFullScreen;
    public WindowMod.zDepth ScreenDepth;
    private int windowLeft = 0;
    private int windowTop = 0;
    private int windowWidth = 1008;
    private int windowHeight = 567;
    private Rect screenPosition;
    private IntPtr HWND_TOP = new IntPtr(0);
    private IntPtr HWND_TOPMOST = new IntPtr(-1);
    private IntPtr HWND_NORMAL = new IntPtr(-2);
    private int Xscreen;
    private int Yscreen;
    private int i;
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags);
    [DllImport("User32.dll")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("User32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [DllImport("User32.dll")]
    private static extern int GetWindowLong(IntPtr hWnd, int dwNewLong);
    [DllImport("User32.dll")]
    private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetParent(IntPtr hChild);
    [DllImport("User32.dll")]
    public static extern IntPtr GetSystemMetrics(int nIndex);

    private void Start()
    {
        this.Xscreen = (int)WindowMod.GetSystemMetrics(0);
        this.Yscreen = (int)WindowMod.GetSystemMetrics(1);
        if (this.AppWindowStyle == WindowMod.appStyle.FullScreen)
        {
            Screen.SetResolution(this.Xscreen, this.Yscreen, true);
        }
        if (this.AppWindowStyle == WindowMod.appStyle.WindowedFullScreen)
        {
            Screen.SetResolution(this.Xscreen - 1, this.Yscreen - 1, false);
            this.screenPosition = new Rect(0f, 0f, (float)(this.Xscreen - 1), (float)(this.Yscreen - 1));
        }
        if (this.AppWindowStyle == WindowMod.appStyle.Windowed)
        {
            Screen.SetResolution(this.windowWidth, this.windowWidth, false);
        }
        if (this.AppWindowStyle == WindowMod.appStyle.WindowedWithoutBorder)
        {
            Screen.SetResolution(this.windowWidth, this.windowWidth, false);
            this.screenPosition = new Rect((float)this.windowLeft, (float)this.windowTop, (float)this.windowWidth, (float)this.windowHeight);
            //this.screenPosition = new Rect(300,180,(float)this.windowWidth,(float)this.windowHeight);
        }
    }
    private void Update()
    {
        if (this.i < 5)
        {
            if (this.AppWindowStyle == WindowMod.appStyle.WindowedFullScreen)
            {
                WindowMod.SetWindowLong(WindowMod.GetForegroundWindow(), -16, 369164288);
                if (this.ScreenDepth == WindowMod.zDepth.Normal)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
                }
                if (this.ScreenDepth == WindowMod.zDepth.Top)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
                }
                if (this.ScreenDepth == WindowMod.zDepth.TopMost)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
                }
                WindowMod.ShowWindow(WindowMod.GetForegroundWindow(), 3);
            }
            if (this.AppWindowStyle == WindowMod.appStyle.Windowed)
            {
                if (this.ScreenDepth == WindowMod.zDepth.Normal)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, 0, 0, 0, 0, 3u);
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, 0, 0, 0, 0, 35u);
                }
                if (this.ScreenDepth == WindowMod.zDepth.Top)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, 0, 0, 0, 0, 3u);
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, 0, 0, 0, 0, 35u);
                }
                if (this.ScreenDepth == WindowMod.zDepth.TopMost)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, 0, 0, 0, 0, 3u);
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, 0, 0, 0, 0, 35u);
                }
            }
            if (this.AppWindowStyle == WindowMod.appStyle.WindowedWithoutBorder)
            {
                WindowMod.SetWindowLong(WindowMod.GetForegroundWindow(), -16, 369164288);
                if (this.ScreenDepth == WindowMod.zDepth.Normal)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_NORMAL, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
                }
                if (this.ScreenDepth == WindowMod.zDepth.Top)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOP, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
                }
                if (this.ScreenDepth == WindowMod.zDepth.TopMost)
                {
                    WindowMod.SetWindowPos(WindowMod.GetForegroundWindow(), this.HWND_TOPMOST, (int)this.screenPosition.x, (int)this.screenPosition.y, (int)this.screenPosition.width, (int)this.screenPosition.height, 64u);
                }
            }
        }
        this.i++;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值