添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

参考连接: https://blog.csdn.net/ldy597321444/article/details/86595786

Unity 发布的PC 端程序怎么实现隐藏任务栏,窗口置顶,隐藏标题? 如果单单使用Unity的api 能否实现我不知道,反正查了很多 但是都没查到。那么,我想到了借助windows的编程库来实现,反正Unity 是可以调用 c++ 和C# 库函数的。

using System.Runtime.InteropServices;
//control the task bar hide or show
//liuyanlei
public class ToolControlTaskBar
    [DllImport("user32.dll")]   //这里是引入 user32.dll 库, 这个库是windows系统自带的。
    public static extern int ShowWindow(int hwnd, int nCmdShow); //这是显示任务栏
    [DllImport("user32.dll")] 
    public static extern int FindWindow(string lpClassName, string lpWindowName); //这是隐藏任务栏
    private const int SW_HIDE = 0;  //hied task bar
    private const int SW_RESTORE = 9;//show task bar
    // Use this for initialization
    /// <summary>
    /// show TaskBar
    /// </summary>
    public static void ShowTaskBar()
        ShowWindow(FindWindow("Shell_TrayWnd", null), SW_RESTORE);
    /// <summary>
    /// Hide TaskBar
    /// </summary>
    public static void HideTaskBar()
        ShowWindow(FindWindow("Shell_TrayWnd", null), SW_HIDE);

将这个脚本加入Unity工程,然后,只需要调用 ShowTaskBar(),和HideTaskBar() 就可以控制任务栏显示和隐藏了。

然后再说说游戏窗口的置顶,和游戏的窗口隐藏。

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
public class WindowMod : MonoBehaviour
    [DllImport("user32.dll")]
    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", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("User32.dll", EntryPoint = "SetWindowLong")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [DllImport("User32.dll", EntryPoint = "GetWindowLong")]
    private static extern int GetWindowLong(IntPtr hWnd, int dwNewLong);
    [DllImport("User32.dll", EntryPoint = "MoveWindow")]
    private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
    [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
    public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP);
    [DllImport("user32.dll", EntryPoint = "SetParent", CharSet = CharSet.Auto)]
    public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent);
    [DllImport("user32.dll", EntryPoint = "GetParent", CharSet = CharSet.Auto)]
    public static extern IntPtr GetParent(IntPtr hChild);
    [DllImport("User32.dll", EntryPoint = "GetSystemMetrics")]
    public static extern IntPtr GetSystemMetrics(int nIndex);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
    public enum appStyle
        FullScreen = 0,
        WindowedFullScreen = 1,
        Windowed = 2,
        WindowedWithoutBorder = 3,
    public appStyle AppWindowStyle = appStyle.WindowedFullScreen;
    public enum zDepth
        Normal = 0,
        Top = 1,
        TopMost = 2,
    public zDepth ScreenDepth = zDepth.Normal;
    public int windowLeft = 0;
    public int windowTop = 0;
    private int windowWidth = Screen.width;
    private int windowHeight = Screen.height;
    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    private Rect screenPosition;
    private const int GWL_EXSTYLE = (-20);
    private const int WS_CAPTION = 0xC00000;
    private const int WS_POPUP = 0x800000;
    IntPtr HWND_TOP = new IntPtr(0);
    IntPtr HWND_TOPMOST = new IntPtr(-1);
    IntPtr HWND_NORMAL = new IntPtr(-2);
    private const int SM_CXSCREEN = 0x00000000;
    private const int SM_CYSCREEN = 0x00000001;
    int Xscreen;
    int Yscreen;
    //add 2015.4.21
    public bool StartAuto = false;
    public enum ScreenDirection
        defaultDirection,
        horizontal,
        vertical,
    public ScreenDirection CurDirection = ScreenDirection.defaultDirection;
    void Awake()
        Xscreen = (int)GetSystemMetrics(SM_CXSCREEN);
        Yscreen = (int)GetSystemMetrics(SM_CYSCREEN);
        if (!StartAuto)
            if (Xscreen > Yscreen)
                windowWidth = 1920;
                windowHeight = 1080;
                // Global.CurDictiion = Global.EnumDiction.Horizontal;
                windowWidth = 1080;
                windowHeight = 1920;
                //Global.CurDictiion = Global.EnumDiction.Vertical;
            if (CurDirection == ScreenDirection.horizontal)
                windowWidth = 1920;
                windowHeight = 1080;
                // Global.CurDictiion = Global.EnumDiction.Horizontal;
            else if (CurDirection == ScreenDirection.vertical)
                windowWidth = 1080;
                windowHeight = 1920;
                //Global.CurDictiion = Global.EnumDiction.Vertical;
        if ((int)AppWindowStyle == 0)
            Screen.SetResolution(Xscreen, Yscreen, true);
        if ((int)AppWindowStyle == 1)
            //Screen.SetResolution(Xscreen - 1, Yscreen - 1, false);
            //screenPosition = new Rect(0, 0, Xscreen - 1, Yscreen - 1);
            Screen.SetResolution(windowWidth, windowHeight, false);
            screenPosition = new Rect(0, 0, windowWidth, windowHeight);
        if ((int)AppWindowStyle == 2)
            Screen.SetResolution(windowWidth, windowWidth, false);
        if ((int)AppWindowStyle == 3)
            Screen.SetResolution(windowWidth, windowWidth, false);
            screenPosition = new Rect(windowLeft, windowTop, windowWidth, windowWidth);
    void Start()
        InvokeRepeating("LaunchProjectile", 1, 0.5f);
    void LaunchProjectile()
        print("hello");
    int i = 0;
    void Update()
        IntPtr hWnd = FindWindow(null, "udpeffect");  //<span style="font-size: 9pt; line-height: 25.2px;">udpeffect 这个是我unity 打包发布后的 窗口名字。这里注意设置。</span><br>
        //if(hWnd != IntPtr.Zero)
        //    System.IO.Directory.CreateDirectory("d:\\ttt");
        SetForegroundWindow(hWnd);
        if (i < 30)
            if ((int)AppWindowStyle == 1)
                SetWindowLong(hWnd, -16, 369164288);
                if ((int)ScreenDepth == 0)
                    SetWindowPos(hWnd, HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                if ((int)ScreenDepth == 1)
                    SetWindowPos(hWnd, HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                if ((int)ScreenDepth == 2)
                    SetWindowPos(hWnd, HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                //ShowWindow(GetForegroundWindow(), 3);
            if ((int)AppWindowStyle == 2)
                if ((int)ScreenDepth == 0)
                    SetWindowPos(hWnd, HWND_NORMAL, 0, 0, 0, 0, 0x0001 | 0x0002);
                    SetWindowPos(hWnd, HWND_NORMAL, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020);
                if ((int)ScreenDepth == 1)
                    SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, 0x0001 | 0x0002);
                    SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020);
                if ((int)ScreenDepth == 2)
                    SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002);
                    SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0020);
            if ((int)AppWindowStyle == 3)
                SetWindowLong(hWnd, -16, 369164288);
                if ((int)ScreenDepth == 0)
                    SetWindowPos(hWnd, HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                if ((int)ScreenDepth == 1)
                    SetWindowPos(hWnd, HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
                if ((int)ScreenDepth == 2)
                    SetWindowPos(hWnd, HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);

这个脚本挂到场景照相机上就好了, 其实总结起来就是, 通过windows 编程的api 找到当前窗口的名字,  IntPtr hWnd = FindWindow(null, "udpeffect"); //udpeffect 这个是我unity 打包发布后的 窗口名字。这里注意设置。 只有找到这个窗口,才会把这个窗口置顶。 然后设置无边框。这个脚本在编辑器下是没有效果的, 因为 只有打包后才可以,找到这个窗口。

好了,我这里只是把一些功能集中,其实这些都可以在widnows 编程里面找到。

两个程序Unity一个程序,winform一个程序 winform程序用来生成托盘图标,并且控制Unity程序的最大、最小化及关闭 Unity程序需要监听到鼠标点击标题栏右上角最小化和关闭事件 winform程序需要单例运行(同一时间只允许一个程序允许) Unity程序启动时,同时启动winform程序 2.2 实现 2.2.1 Unity程序监听最小化和关闭事 在这篇文章中,我们实现了点击最小化和关闭菜单将程序隐藏任务栏的功能,但是这篇文章需要额外一个winform程序来处理任务栏的功能,有没有方法可以不需要依赖其他程序也能实现这个需求呢?当然有的,使用Windows系统提供的API就行了。 我们先来看看完全依靠调用Windows提供的API实现的效果。 public class ToolControlTaskBar [DllImport("user32.dll")] //这里是引入 user32.dll 库, 这个库是windows系统自带的。 pub...
Windows系统unity程序最小化,隐藏桌面和任务栏 [DllImport(“user32.dll”)] public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("User32.dll", EntryPoint = "FindWindow")] public exter
Unity打包Android程序并与PC通过USB进行Socket通信 前段时间,有个使用Unity打包做的Android项目,要求是:通信不能使用WIFI,电脑通过USB与手机想连接,进行通信。可能我搜索的关键字不对,搜了好久才找到一些,后来通过搜索Android相关的,才搜到一些,这里写一些作为纪录。 这篇主要将PC和Android程序通过ADB 手动配置连接,讲的稍微详细点,可能会比较啰嗦。 一、下载ADB工具 这里使用google android 开发中的SDK里面的与Android交互的工具A