• Unity 透明窗体的创建


    原文地址:https://alastaira.wordpress.com/2015/06/15/creating-windowless-unity-applications/

    蛮牛地址:http://www.manew.com/thread-43230-1-1.html

    该效果只针对Windows平台。结合摄像机的OnRenderImage事件和一些Windows底层API调用来实现该效果。

    首先需要一个透明的Shader,我这直接使用系统的。

    然后创建一个C#的脚本 TransparentWindow

     1 using UnityEngine;
     2 using System.Collections;
     3 using System.Runtime.InteropServices;
     4 using System;
     5 
     6 public class TransparentWindow : MonoBehaviour
     7 {
     8     [SerializeField]
     9     private Material m_Material;
    10     private struct MARGINS
    11     {
    12         public int cxLeftWidth;
    13         public int cxRightWidth;
    14         public int cyTopHeight;
    15         public int cyBottomHeight;
    16     }
    17     // Define function signatures to import from Windows APIs
    18     [DllImport("user32.dll")]
    19     private static extern IntPtr GetActiveWindow();
    20     [DllImport("user32.dll")]
    21     private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
    22     [DllImport("Dwmapi.dll")]
    23     private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
    24     // Definitions of window styles
    25     const int GWL_STYLE = -16;
    26     const uint WS_POPUP = 0x80000000;
    27     const uint WS_VISIBLE = 0x10000000;
    28     void Start()
    29     {
    30 #if !UNITY_EDITOR
    31         var margins =new MARGINS() { cxLeftWidth = -1 };
    32 // Get a handle to the window
    33 var hwnd = GetActiveWindow();
    34 // Set properties of the window
    35 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
    36 SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
    37 // Extend the window into the client area
    38 //See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx
    39 DwmExtendFrameIntoClientArea(hwnd,ref margins);
    40 #endif
    41     }
    42     // Pass the output of the camera to the custom material
    43     // for chroma replacement
    44     void OnRenderImage(RenderTexture from, RenderTexture to)
    45     {
    46         Graphics.Blit(from, to, m_Material);
    47     }
    48 
    49 }

    上面的代码用到了InterOpServices命名空间,以便调用一些Windows底层API从而改变Unity应用在运行时的窗口属性。然后使用OnRenderImage事件将摄像机的输出应用到RenderTexture。将使用Shader的材质赋给脚本中的m_Material字段,这样我们就可以开始抠图了。

    然后,重点来了:将摄像机的背景颜色改为与透明材质中_TransparentColourKey属性一样。

    你可以随便设置该颜色,但两边必须一致。

    最后就是生成和运行啦。因为用到了RenderTexture,所以如果使用Unity4.x必须为Pro版,而5.x任意版本均可。

    效果:

  • 相关阅读:
    python中True,False,数值之间用<,>,==进行比较规则
    分析python代码性能的程序分析包cProfile
    /bin/bash^M: bad interpreter: No such file or directory问题
    架构图以及vue的简介
    重写简易的confirm函数
    CSS绝对定位元素居中的几种方法
    Linux 部署java web 项目,验证码图片不显示文字问题
    log4j升级到log4j2
    openssh一键升级脚本(只升级openssh,其它已有环境不变,解决root登录问题)
    openssh一键升级脚本(测试成功)
  • 原文地址:https://www.cnblogs.com/UnrealEra/p/5979338.html
Copyright © 2020-2023  润新知