Creation of Unity Transparent forms

Source: Internet
Author: User

Original address: https://alastaira.wordpress.com/2015/06/15/creating-windowless-unity-applications/

Bull Address: http://www.manew.com/thread-43230-1-1.html

This effect is only for Windows platforms. This effect is implemented in conjunction with the camera's Onrenderimage event and some Windows underlying API calls.

First, a transparent shader is needed, and I use the system directly.

Then create a C # script Transparentwindow

1 usingUnityengine;2 usingSystem.Collections;3 usingSystem.Runtime.InteropServices;4 usingSystem;5 6  Public classTransparentwindow:monobehaviour7 {8 [Serializefield]9     PrivateMaterial m_material;Ten     Private structMARGINS One     { A          Public intCxleftwidth; -          Public intCxrightwidth; -          Public intCytopheight; the          Public intCybottomheight; -     } -     //Define function signatures to import from Windows APIs -[DllImport ("user32.dll")] +     Private Static externIntPtr GetActiveWindow (); -[DllImport ("user32.dll")] +     Private Static extern intSetWindowLong (IntPtr hWnd,intNIndex,UINTDwnewlong); A[DllImport ("Dwmapi.dll")] at     Private Static extern UINTDwmExtendFrameIntoClientArea (IntPtr hWnd,refMARGINS MARGINS); -     //Definitions of window styles -     Const intGwl_style =- -; -     Const UINTWs_popup =0x80000000; -     Const UINTWs_visible =0x10000000; -     voidStart () in     { - #if! Unity_editor to         varmargins =NewMARGINS () {cxleftwidth =-1 }; + //Get A handle to the window - varhwnd =GetActiveWindow (); the //Set Properties of the window * //See :https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx $SetWindowLong (hwnd, Gwl_style, Ws_popup |ws_visible);Panax Notoginseng //Extend the window into the client area - //See :https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx theDwmExtendFrameIntoClientArea (hwnd,refmargins); + #endif A     } the     //Pass the output of the camera to the custom material +     //For chroma Replacement -     voidOnrenderimage (rendertexture from, rendertexture to) $     { $Graphics.blit ( from, to, m_material); -     } -  the}

The code above uses the InteropServices namespace to invoke some of the windows underlying APIs to change the window properties of the Unity app at run time. Then use the Onrenderimage event to apply the camera's output to the rendertexture. Assign the material using shader to the M_material field in the script so that we can start keying the graph.

Then, the point is: Change the camera's background color to the same as the _transparentcolourkey property in the transparent material.

You can set the color casually, but both sides must be the same.

The end result is build and run. Because of the use of rendertexture, if the use of unity4.x must be pro version, and 5.x any version can be.

Effect:

Creation of Unity Transparent forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.