把这个脚本直接挂到摄像机就OK了,发布之后可以看效果,编辑模式下看不到效果
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() 就可以控制任务栏显示和隐藏了。

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

[code]csharpcode:

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");  //udpeffect 这个是我unity 打包发布后的 窗口名字。这里注意设置。
        //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 编程里面找到。

public class ToolControlTaskBar [DllImport("user32.dll")] //这里是引入 user32.dll 库, 这个库是windows系统自带的。 pub... public class WindowAPI : MonoBehaviour [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr F.
做项目遇到一个需求,需要是unity打包出来的程序在运行的时候窗口最大化,并且保持在最上层,最难的是要时刻保持交互,不然输入控制会失效,网上百般查询和亲自验证后,利用window自带的方法实现窗口置顶,然后程序使用鼠标点击屏幕某个位置实现保持交互(这个不完美,但是想不到其它方法实现,有大佬可以指教一下)。 重点:一定要将Update中FindWindow()方法里的程序名称换成自己打包出来的名字。 下面代码: using System; using System.Collections; using
这两天玩了玩QQ拖拉机,总觉得记牌没有真打的记得准,所以今天用VB做了一个记牌器。本身没什么难度,最后要用到AlwaysOnTop功能,以前没做过,所以查了一下资料:调用一些API函数即可Private Const SWP_NOSIZE = &H1 Private Const