Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使背景透明_C#_Unity3d - Fatal编程技术网

C# 使背景透明

C# 使背景透明,c#,unity3d,C#,Unity3d,我正在尝试为Windows10制作一个临时的屏幕键盘,并且需要背景透明,以方便用户(按键已经是透明的)。一、 但是,我不知道如何使背景透明 任何帮助都将不胜感激 我相信我本质上是在寻找下面显示的代码的更新版本: using System; using System.Runtime.InteropServices; using UnityEngine; public class TransparentWindow : MonoBehaviour { [SerializeField]

我正在尝试为Windows10制作一个临时的屏幕键盘,并且需要背景透明,以方便用户(按键已经是透明的)。一、 但是,我不知道如何使背景透明

任何帮助都将不胜感激

我相信我本质上是在寻找下面显示的代码的更新版本:

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class TransparentWindow : MonoBehaviour
{
    [SerializeField]
    private Material m_Material;

    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);

    [DllImport("user32.dll")]
    static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
    static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);

    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);

    const int GWL_STYLE = -16;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;
    const int HWND_TOPMOST = -1;

    void Start()
    {
 // You really don't want to enable this in the editor, but it works there..

    int fWidth = Screen.width;
    int fHeight = Screen.height;
    var margins = new MARGINS() { cxLeftWidth = -1 };
    var hwnd = GetActiveWindow();

    SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);

    // Transparent windows with click through
    SetWindowLong(hwnd, -20, 524288 | 32);//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
    SetLayeredWindowAttributes(hwnd, 0, 255, 2);// Transparency=51=20%, LWA_ALPHA=2
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
    DwmExtendFrameIntoClientArea(hwnd, ref margins);


    }

    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}
给出的代码不起作用,所以我认为它已经过时了。我不知道如何更新它自己,因为它是一个有点超出我的技能集。当我将代码上传到Unity时,它只是说代码中有错误,并且它不是有效的脚本。但是,当我打开脚本时,不会出现任何错误

我希望能够对键盘后面的东西有一个相对较好的视图,比如我的桌面,但实际上我只看到一个黑色的平面

更新:
很明显,错误消息是由我的脚本与我的类没有相同的名称引起的。我昨天花了4个多小时试图修复该错误消息,此次命名事件就是原因:(.谢谢Ruzihm。不管怎样,现在错误信息已经消失了,当我运行或构建程序时,我的透明窗口材质出现了:深粉色。然后我将Unity版本改回2018.2.16f1,但没有成功。然后我删除了#if!Unity编辑器行,以使透明度在编辑器中完美工作,但在构建它时不会。注意,当我构建它和在编辑器中运行它时,单击通过确实起作用。

正如在注释中发现的,当相机的清除标志设置为“纯色”时,问题得到了解决,并且粉红色透明窗口材质被替换为白色透明材质。

正如在注释中发现的,问题是修正了当相机的清晰标志设置为“纯色”时,粉红色透明窗口材质被白色透明材质替换为a。

@CodyVollarth他们在问题末尾添加了一个模糊“当我将代码上传到Unity时,它只是说代码中有错误,并且它不是有效的脚本。但是,当我打开脚本时,没有出现错误。"@KileMaze是否为脚本命名以匹配类名。例如,
TransparentWindow
?@Ruzihm R:255g:255b:255a:0@Ruzihm我很抱歉。我应该意识到我使用的不是同一个代码。我的代码现在就在那里。@Ruzihm再次表示,我很抱歉对我使用的谁的代码感到困惑。我应该指定wh恩,我发布了这个问题:我想我忘了有多人发布了他们自己版本的代码。@CodyVolrath他们在问题的末尾添加了一个简介“当我将代码上载到Unity时,它只是说代码中有错误,并且它不是有效的脚本。但是,当我打开脚本时,没有出现错误。”@KileMaze是否为脚本命名以匹配类名。例如,
TransparentWindow
?@Ruzihm R:255g:255b:255a:0@Ruzihm我很抱歉。我应该意识到我使用的不是同一个代码。我的代码现在就在那里。@Ruzihm再次表示,我很抱歉对我使用的谁的代码感到困惑。我应该指定wh嗯,我发布了这个问题:我想我忘了有很多人发布了他们自己版本的代码。