Unit fenêtre windows transparente

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
herve
Messages : 5
Inscription : 14 Juin 2018 16:01

Unit fenêtre windows transparente

Message par herve » 27 Oct 2020 09:53

Bonjour, j'utilise un code pour rendre la fenêtre Unity transparente et permettre les interactions avec de élément en arrière plan. Mon soucis c'est qu'une fois ce code activé je n'arrive pas a revenir sur une autre scène a un écran opaque sans interaction avec l'arrière plan et je sèche un peu avec l'api windows.

Merci d'avance pour vos retours

Voici le code :

Code : Tout sélectionner

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()
    {
#if !UNITY_EDITOR // You really don't want to enable this in the editor..

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);

#endif
    }

    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}


Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6219
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: Unit fenêtre windows transparente

Message par boubouk50 » 27 Oct 2020 10:03

Bonjour,

Peux-tu nous fournir le lien vers le site/l'endroit ou tu as recopié ce code, stp?

C'est plutôt une question Windows qu'Unity puisque tu intègres les propriétés des fenêtres Windows.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

herve
Messages : 5
Inscription : 14 Juin 2018 16:01

Re: Unit fenêtre windows transparente

Message par herve » 27 Oct 2020 10:41

Bonjour , c'était un sujet sur le forum Unity.
https://forum.unity.com/threads/solved- ... ey.323057/

Seulement ce code, une fois activé, impacte toutes les scènes, je regarde l'api Windows mais je ne trouve pas le moyen de faire le chemin inverse en rendant la fenêtre Unity opaque sans interaction avec l'arrière plan en changeant de scène.

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6219
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: Unit fenêtre windows transparente

Message par boubouk50 » 27 Oct 2020 11:51

Clairement, c'est de la configuration de fenêtrage Windows, il faut aller voir du côté de Microsoft pour trouver les paramètres par défaut.
Tu devrais poster un message directement sur le forum où tu as trouvé cette ressource. Ils seront un peu plus à même de trouver une solution.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Répondre

Revenir vers « Scripting »