Unity3D类似于桌面精灵的功能实现

news2024/9/20 5:55:39

前言:

由于最近在做游戏魔改,很多功能在游戏里面没法实现(没错,说的就是排行榜),所以准备用Unity3D开发一个类似于桌面精灵的功能部件,实现效果如下:

PS:有需要定制的老板请私信联系

要实现这个效果,需要分两步:

1,背景透明

2,程序始终在前面

一,背景透明实现核心代码

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

    // Define function signatures to import from Windows APIs

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

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

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


    // Definitions of window styles
    const int GWL_STYLE = -16;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;

    void Start()
    {
        //return;
#if !UNITY_EDITOR
        var margins = new MARGINS() { cxLeftWidth = -1 };

        // Get a handle to the window
        var hwnd = GetActiveWindow();

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

        // Extend the window into the client area
        //See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx 
        DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif

    }

    // Pass the output of the camera to the custom material
    // for chroma replacement
    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }



   
}

shader代码如下:

Shader "Custom/ChromakeyTransparent" {
    Properties{
        _MainTex("Base (RGB)", 2D) = "white" {}
        _TransparentColourKey("Transparent Colour Key", Color) = (0,0,0,1)
        _TransparencyTolerance("Transparency Tolerance", Float) = 0.01
    }

    SubShader{
        Pass{
            Tags{ "RenderType" = "Opaque" }
            LOD 200

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct a2v
            {
                float4 pos : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            v2f vert(a2v input)
            {
                v2f output;
                output.pos = UnityObjectToClipPos(input.pos);
                output.uv = input.uv;
                return output;
            }

            sampler2D _MainTex;
            float3 _TransparentColourKey;
            float _TransparencyTolerance;

            float4 frag(v2f input) : SV_Target
            {
                // What is the colour that *would* be rendered here?
                float4 colour = tex2D(_MainTex, input.uv);

                // Calculate the different in each component from the chosen transparency colour
                float deltaR = abs(colour.r - _TransparentColourKey.r);
                float deltaG = abs(colour.g - _TransparentColourKey.g);
                float deltaB = abs(colour.b - _TransparentColourKey.b);

                // If colour is within tolerance, write a transparent pixel
                if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance)
                {
                    return float4(0.0f, 0.0f, 0.0f, 0.0f);
                }

                // Otherwise, return the regular colour
                return colour;
            }
            ENDCG
        }
    }
}

将脚本绑定在摄像机上,并用该shader创建材质A,放到脚本下。

摄像机渲染模式改为只渲染颜色,颜色和材质A一样。

二,程序始终在前面核心代码

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class C
{
    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetParent(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

    [DllImport("kernel32.dll")]
    public static extern void SetLastError(uint dwErrCode);

    public static IntPtr GetProcessWnd()
    {
        IntPtr ptrWnd = IntPtr.Zero;
        uint pid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID  

        bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
        {
            uint id = 0;
            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref id);
                if (id == lParam)    // 找到进程对应的主窗口句柄  
                {
                    ptrWnd = hwnd;   // 把句柄缓存起来  
                    SetLastError(0);    // 设置无错误  
                    return false;   // 返回 false 以终止枚举窗口  
                }
            }

            return true;

        }), pid);

        return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
    }
}
  [DllImport("User32.dll")]
  extern static bool SetForegroundWindow(IntPtr hWnd);

  [DllImport("User32.dll")]
  extern static bool ShowWindow(IntPtr hWnd, short State);

  [DllImport("user32.dll ")]
  public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
  const UInt32 SWP_NOSIZE = 0x0001;
  const UInt32 SWP_NOMOVE = 0x0002;
  IntPtr hWnd;

  //public float Wait = 0;//延迟执行
  //public float Rate = 1;//更新频率
  public bool KeepForeground = true;//保持最前


  /// <summary>
  /// 激活窗口
  /// </summary>
  void Active()
  {
      if (KeepForeground)
      {
          ShowWindow(hWnd, 1);
          SetForegroundWindow(hWnd);
          SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
      }
  }
  hWnd = C.GetProcessWnd();
  Active();

三,打包设置如下

四,优化扩展

步骤和道理同上

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("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);

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

    [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")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    const int GWL_STYLE = -16;
    const int GWL_EXSTYLE = -20;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;

    const uint WS_EX_TOPMOST = 0x00000008;
    const uint WS_EX_LAYERED = 0x00080000;
    const uint WS_EX_TRANSPARENT = 0x00000020;

    const int SWP_FRAMECHANGED = 0x0020;
    const int SWP_SHOWWINDOW = 0x0040;
    const int LWA_ALPHA = 2;

    private IntPtr HWND_TOPMOST = new IntPtr(-1);

    private IntPtr _hwnd;

    void Start()
    {
//#if !UNITY_EDITOR
    MARGINS margins = new MARGINS() { cxLeftWidth = -1 };
    _hwnd = GetActiveWindow();
    int fWidth = Screen.width;
    int fHeight = Screen.height;
        SetWindowLong(_hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
        SetWindowLong(_hwnd, GWL_EXSTYLE, WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT);//若想鼠标穿透,则将这个注释恢复即可
        DwmExtendFrameIntoClientArea(_hwnd, ref margins);
        SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, SWP_FRAMECHANGED | SWP_SHOWWINDOW); 
        ShowWindowAsync(_hwnd, 3); //Forces window to show in case of unresponsive app    // SW_SHOWMAXIMIZED(3)
//#endif
    }

    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}
Shader "Custom/MakeTransparent" {
  Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _TransparentColorKey ("Transparent Color Key", Color) = (0,1,0,1)
    _TransparencyMargin ("Transparency Margin", Float) = 0.01 
  }
  SubShader {
    Pass {
      Tags { "RenderType"="Opaque" }
      LOD 200
    
      CGPROGRAM

      #pragma vertex VertexShaderFunction
      #pragma fragment PixelShaderFunction
    
      #include "UnityCG.cginc"

      struct VertexData
      {
        float4 position : POSITION;
        float2 uv : TEXCOORD0;
      };

      struct VertexToPixelData
      {
        float4 position : SV_POSITION;
        float2 uv : TEXCOORD0;
      };

      VertexToPixelData VertexShaderFunction(VertexData input)
      {
        VertexToPixelData output;
        output.position = UnityObjectToClipPos (input.position);
        output.uv = input.uv;
        return output;
      }
    
      sampler2D _MainTex;
      float3 _TransparentColorKey;
      float _TransparencyMargin;

      float4 PixelShaderFunction(VertexToPixelData input) : SV_Target
      {
        float4 color = tex2D(_MainTex, input.uv);
      
        float deltaR = abs(color.r - _TransparentColorKey.r);
        float deltaG = abs(color.g - _TransparentColorKey.g);
        float deltaB = abs(color.b - _TransparentColorKey.b);

        if (deltaR < _TransparencyMargin && deltaG < _TransparencyMargin && deltaB < _TransparencyMargin)
        {
          return float4(0.0f, 0.0f, 0.0f, 0.0f);
        }

        return color;
      }
      ENDCG
    }
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2136391.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MySQL聚合统计

【数据库】MySQL聚合统计 王笃笃-CSDN博客https://blog.csdn.net/wangduduniubi?typeblog显示平均工资低于2000的部门和它的平均工资 mysql> select deptno,avg(sal) deptavg from emp group by deptno; --------------------- | deptno | deptavg | --------------…

ESKF学习笔记

参考资料 https://zhuanlan.zhihu.com/p/441182819 惯性导航(三)-基于流型的ESKF及代码实现_eskf和ekf-CSDN博客 用ESKF实现IMU/GNSS组合导航&#xff08;学习记录&#xff09;_eskf imu-CSDN博客 0.ESKF与KF的区别以及总体流程 0.1卡尔曼滤波过程 卡尔曼滤波的流程按照1…

微信阅读小程序的设计与实现(lw+演示+源码+运行)

题目&#xff1a;微信阅读小程序的设计与实现 摘 要 由于APP软件在开发以及运营上面所需成本较高&#xff0c;而用户手机需要安装各种APP软件&#xff0c;因此占用用户过多的手机存储空间&#xff0c;导致用户手机运行缓慢&#xff0c;体验度比较差&#xff0c;进而导致用户会…

学习之IDEA集成GitHub

一、配置github账号 使用token登录 二、分享代码到github 2.1 当pycharm中找不到VCS时 2.2 分享代码到github 2.3 push推送代码 2.4pull拉取代码 2.5 克隆代码 三、github打开双重身份认证

Docker构建Java镜像及使用

前言 最近在项目中使用到了一些docker技术&#xff0c;因为没有专业的运维人员&#xff0c;所以在部署版本应用的时候&#xff0c;都是自己在进行打包部署。经过一段时间的操作和使用&#xff0c;在这里和大家分享一下自己的心得&#xff0c;也算是做一下总结吧。 准备工作 首…

【Unity学习心得】如何制作俯视角射击游戏

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、导入素材二、制作流程 1.制作地图2.实现人物动画和移动脚本3.制作单例模式和对象池4.制作手枪pistol和子弹bullet和子弹壳bulletShell5.制作散弹枪shotgun总…

AIDA64 Extreme(硬件检测工具)v7.20绿色不需要安装版,供大家学习研究参考

一款综合性的系统硬件检测工具,号称最权威的电脑硬件检测,监控与测试软件,这款专业的硬件检测大师也是每位高手玩家,菜鸟用户必备的硬件检测利器.AIDA64不仅提供诸如协助超频,硬件侦错,压力测试和传感器监测等多种功能,而且还可以对处理器,系统内存和磁盘驱动器性能进行全面评估…

家具行业短视频矩阵获客,轻松实现百万曝光!

当前家具行业的营销正面临一个新阶段&#xff0c;但同时也遭遇了增长的瓶颈&#xff0c;主要问题在于营销策略过于雷同&#xff0c;这导致产品难以在市场中获得足够关注&#xff0c;从而在品牌和消费者之间形成了隔阂。 同时在这样一个信息爆炸的时代&#xff0c;就算是最优秀…

pandas 生成excel多级表头

使用pandas导出excel 表格时类似这种 其中含有多级表头的情况也就是涉及到表头需要合并单元格&#xff08;横向及纵向&#xff09; 1、表头设置 columns [("xx公司路产月报表","序号","序号"),("xx公司路产月报表","单位"…

【树形dp】P2014 [CTSC1997] 选课 题解

题目描述 在大学里每个学生&#xff0c;为了达到一定的学分&#xff0c;必须从很多课程里选择一些课程来学习&#xff0c;在课程里有些课程必须在某些课程之前学习&#xff0c;如高等数学总是在其它课程之前学习。现在有 N ( 1 ≤ N ≤ 300 ) N(1\leq N \leq 300) N(1≤N≤30…

Leetcode 每日一题:Longest Increasing Path in a Matrix

写在前面&#xff1a; 今天我们继续看一道 图论和遍历 相关的题目。这道题目的背景是在一个矩阵当中找寻最长的递增数列长度。思路上非常好想&#xff0c;绝对和 DFS 相关&#xff0c;但是题目的优化要求非常高&#xff0c;对于语言和内存特性的考察特别丰富&#xff0c;如果是…

【Python报错已解决】ERROR: No matching distribution found for PIL

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 文章目录 前言一、问题描述1.1 报错示例1.2 报错分析1.3 解决思路 二、解决方法2.1 方法一&#xff1a;安装Pillow2.2 步骤二&a…

CentOS安装Hadoop系列

安装Hadoop 1、安装SDK 2、Wget下载安装包到指定目录 3、选择hadoop的配置模式&#xff0c;单机模式&#xff0c;伪集群模式&#xff0c;集群模式 1、查找APACHE下载官网&#xff0c;搜索hadoop,进入目录&#xff0c;找到common目录&#xff0c;下载对应版本 国内&#xff0c;…

uniapp 原生插件开发 UI

前言&#xff1a; 在集成某些特定 原生SDK的时候&#xff0c;它本身是带UI控件的。当我们使用 uniapp 开发app的时候实是 可以使使用 nvue 页面&#xff0c;以 weex 的方式嵌入原生的UI控件。 我这边的场景是 接入连连app的支付&#xff0c;它有个自己的密码键盘 控件是原生的页…

好用的电脑录屏软件有哪些?推荐4款专业工具。

不同系统的电脑上面带有的录屏功能不一样&#xff0c;比如win10上面有Xbox game bar,Mac系统则用的是QuickTime Player&#xff0c;或者是使用快捷键“CommandShift5”。但更方便的&#xff0c;我自己认为是使用一些专业的录屏软件&#xff0c;他门的录制模式多&#xff0c;兼容…

python将这多个sheet页的数据合并在一起

有如下数据&#xff0c;需要将excel多个sheet页中的数据&#xff0c;合并在一起。 数据样例&#xff1a;&#x1f447; import pandas as pd import os# 读Excel文件 file_path D:/project/Excelimport/簿4.xlsx# 创建空的DataFrame用于存储合并后的数据 all_data pd.Data…

软件项目管理

1. 软件项目管理概述 1.1 软件项目管理 软件项目管理的提出是在20世纪70年代中期&#xff0c;当时美国国防部专门研究了软件开发不能按时提交、预算超支和质量达不到用户要求的原因&#xff0c;发现70%的项目是因为管理不善引起的&#xff0c;而非技术原因。 软件项目管理和…

9月17–20日Sui新加坡参会指南,期待与您相聚

&#x1f4e7;叮&#xff0c;您有一份Sui新加坡参会指南待领取&#xff5e; 随着新加坡Token2049大会的临近&#xff0c;我们为即将前往新加坡参会的朋友们准备了一份指南&#xff0c;帮助你快速了解Sui团队的活动安排&#xff0c;并与Sui团队来个线下面对面的交流。 9月17日…

解码消费医疗机构:如何通过全场景AI运营实现营销破局?

在当今的医美市场中&#xff0c;“流量困境”已成为众多消费医疗机构面临的严峻挑战。传播日益碎片化&#xff0c;各大机构对流量争夺也愈演愈烈&#xff0c;不少机构面临着新客获取难、老客户留存转化难、运营成本高昂等困境。 那么&#xff0c;面对这一挑战&#xff0c;消费…

Leetcode面试经典150题-79.搜索单词

题目比较简单&#xff0c;回溯最基础的题&#xff0c;记得除非覆盖&#xff0c;否则一定要恢复现场就行 解法都在代码里&#xff0c;不懂就留言或者私信 class Solution {public boolean exist(char[][] board, String word) {int m board.length; int n board[0].length;i…