Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question "On desktop like" script is messing up please can you help me fixing it?

Discussion in 'Scripting' started by khalilm376447, Sep 26, 2023.

  1. khalilm376447

    khalilm376447

    Joined:
    Mar 2, 2023
    Posts:
    25
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using UnityEngine;
    public class CameraSetup : MonoBehaviour
    {
    [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", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    static extern int SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, uint dwFlags);
    private struct MARGINS {
    public int cxLeftWidth;
    public int cxRightWidth;
    public int cxTopHeight;
    public int cxBottomHeight;
    }
    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntiClientArea(IntPtr hWnd, ref MARGINS margins);
    const int GWL_EXSTYLE = -20;
    const uint WS_EX_LAYERED = 0x00080000;
    const uint WS_EX_TRANSPARENT = 0x00000020;
    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    const uint LWA_COLORKEY = 0x00000001;
    void Start()
    {
    #if !UNITY_EDITOR_
    IntPtr hWnd = GetActiveWindow();
    MARGINS margins = new MARGINS { cxLeftWidth = -1 };
    DwmExtendFrameIntiClientArea(hWnd, ref margins);
    SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
    SetLayeredWindowAttributes(hWnd, 0, 0, LWA_COLORKEY);
    SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0);
    #endif
    }
    }


    this script is supposed to make the current scene background transparent (only if the script is on a gameobject on the scene) and let's the player and other gameobject on the desktop
    but the problem is when
    1-i load that scene while being in an another one, the background isn't tranparent and all the screen frames are staked
    2-if i load the scene but that's the only one from the build, it freeze/crash the game


    please i need your help so i can finish what i started
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)


    It appears almost everything about this script is tailored directly to the Win32 User32.dll and its internal endpoints. Don't forget to check that documentation as well. Win32 User32.dll has NOTHING to do with Unity.


    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  3. khalilm376447

    khalilm376447

    Joined:
    Mar 2, 2023
    Posts:
    25
    user32 is because it have something with windows api so i took it

    i mean code monkey did the same
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    Might be a better question for code monkey then. I don't see a Unity question here.

    If you do see a Unity question, use the steps above to describe it.
     
  5. khalilm376447

    khalilm376447

    Joined:
    Mar 2, 2023
    Posts:
    25
    nevermind i will just wait answers from other peoples
     
    Kurt-Dekker likes this.