Unity3D类似于桌面精灵的功能实现
前言:
由于最近在做游戏魔改,很多功能在游戏里面没法实现(没错,说的就是排行榜),所以准备用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 stylesconst int GWL_STYLE = -16;const uint WS_POPUP = 0x80000000;const uint WS_VISIBLE = 0x10000000;void Start(){//return;
#if !UNITY_EDITORvar margins = new MARGINS() { cxLeftWidth = -1 };// Get a handle to the windowvar hwnd = GetActiveWindow();// Set properties of the window// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspxSetWindowLong(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 replacementvoid 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 200CGPROGRAM#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 colourfloat 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 pixelif (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance){return float4(0.0f, 0.0f, 0.0f, 0.0f);}// Otherwise, return the regular colourreturn 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_EDITORMARGINS 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 200CGPROGRAM#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}}
}
相关文章:

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

Audio Over IP的PTP时钟初探
Audio Over IP的PTP时钟初探 这几天参加省局举办的技术能手比赛,第一次接触并了解AOIP(Audio Over IP)相关的理论和实践相关的知识。其中AoIP的时钟同步采用的是IEEE 1588 标准的PTP(Precision Time Protocol)基于网络…...

【加密社】深入理解TON智能合约 (FunC语法)
king: 摘要:在TON(TheOpenNetwork)区块链平台中,智能合约扮演着举足轻重的角色。本文将通过分析一段TON智能合约代码 带领读者学习dict(字典)和list(列表)在FunC语言中的用法&#x…...

笔试强训day11
游游的水果大礼包 #include <iostream> #define int long longusing namespace std; int n, m, a, b;signed main() {cin>>n>>m>>a>>b;int ret 0;for(int x 0; x < min(n / 2, m); x) // 枚举 1 号礼包的个数{int y min(n - x * 2, (m - …...

移动应用开发与测试赛题
引言 在现代车载系统开发中,UI设计和编程实现同样重要。本文将分别探讨车载系统的UI设计任务和相关的编程任务,帮助开发者全面了解车载系统开发的各个方面。 第一部分:UI设计任务 任务1:绘制"左转向视频显示"模块界面…...

Qt常用控件——QLineEdit
文章目录 QLineEdit核心属性和信号基本示例正则表达式约束验证输入密码是否一致密码显示状态切换 QLineEdit核心属性和信号 QLineEdit用来表示单行输入,可以输入一段文本,但是不能替换 核心属性: 属性说明text输入框中的文本inputMask输入…...

(postman)接口测试进阶实战
1.内置和自定义的动态参数 内置的动态参数有哪些? ---{{$}}--是内置动态参数的标志 //自定义的动态参数 此处date.now()的作用就相当于上面的timestamp 2.业务闭环及文件接口测试 返回的url地址可以在网页中查询得到。 3. 常规断言,动态参数断言…...

R语言统计分析——功效分析(比例、卡方检验)
参考资料:R语言实战【第2版】 1、比例检验 当比较两个比例时,可使用pwr.2p.test()函数进行功效分析。格式为: pwr.2p.test(h, n, sig.level, power, alternative) 其中,h是效应值,n是各相同的样本量。效应值h的定义如…...

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

ARCGIS PRO DSK MapTool
MapTool用于自定义地图操作工具,使用户能够在ArcGIS Pro中执行特定的地图交互操作。添加 打开MapTool1.vb文件,可以看到系统已经放出MapTool1类: Public Sub New()将 IsSketchTool 设置为 true 以使此属性生效IsSketchTool TrueSketchTyp…...

国网B接口 USC安防平台 海康摄像机配置
国网B接口海康摄像机配置介绍 如下以海康DS-NACN6432I-GLN摄像机为例,配置国网B接口设备接入流程,海康摄像机的固件版本为 V5.6.11 build 210109 210107。该设备为球机,支持国网B接口云台控制功能。图标编号可以对应二者的配置。 注意 同一…...

Win10安装.net FrameWork3.5失败解决方法
win10安装.net FrameWork3.5失败解决方法 已经好久没有来投稿了,实在最近业务缠身,忙的焦头烂额(呵~多么伟大的牛马) 但最近开发使用windows11实在是拉胯的不行,升级完就后悔,所以就一怒之下,重装了win10 可是,好家伙,我重装完遇到一个问题,就是在使用.Net Framework3.5,按照Mi…...

【pipenv】—— 虚拟环境管理工具近乎全面的总结
安装 pip install pipenv 使用和配置 设置虚拟环境文件创建在项目根目录 添加环境变量:WORKON_HOMEPIPENV_VENV_IN_PROJECT 创建虚拟环境时,自动换用指定的pip源 添加环境变量:PIPENV_TEST_INDEXhttps://pypi.tuna.tsinghua.edu…...

windows C++-并行编程-并行算法(五) -选择排序算法
并行模式库 (PPL) 提供了对数据集合并行地执行工作的算法。这些算法类似于 C 标准库提供的算法。并行算法由并发运行时中的现有功能组成。 在许多情况下,parallel_sort 会提供速度和内存性能的最佳平衡。 但是,当您增加数据集的大小、可用处理器的数量或…...

【系统架构设计师-2014年真题】案例分析-答案及详解
更多内容请见: 备考系统架构设计师-核心总结索引 文章目录 【材料1】问题1问题2【材料2】问题1问题2问题3【材料3】问题1问题2问题3【材料4】问题1问题2【材料5】问题1问题2问题3【材料1】 请详细阅读以下关于网络设备管理系统架构设计的说明,在答题纸上回答问题1和问题2。 …...

windows C++-并行编程-并行算法(三)-分区工作
并行模式库 (PPL) 提供了对数据集合并行地执行工作的算法。这些算法类似于 C 标准库提供的算法。并行算法由并发运行时中的现有功能组成。 若要对数据源操作进行并行化,一个必要步骤是将源分区为可由多个线程同时访问的多个部分。 分区程序将指定并行算法应如何在线…...

下载 llama2-7b-hf 全流程【小白踩坑记录】
1、文件转换 在官网 https://ai.meta.com/llama/ 申请一个账号,选择要下载的模型,会收到一个邮件,邮件中介绍了下载方法 执行命令 git clone https://github.com/meta-llama/llama.git ,然后执行 llama/download.sh,…...

Codeforces practice C++ 2024/9/11 - 2024/9/13
D. Mathematical Problem Codeforces Round 954 (Div. 3) 原题链接:https://codeforces.com/contest/1986/problem/D 题目标签分类:brute force,dp,greedy,implementation,math,two pointers…...

RabbitMQ创建交换机和队列——配置类 注解
交换机的类型 Fanout:广播,将消息交给所有绑定到交换机的队列。 Direct:订阅,基于RoutingKey(路由key)发送给订阅了消息的队列。 Topic:通配符订阅,与Direct类似,只不…...

proteus+51单片机+AD/DA学习5
目录 1.DA转换原理 1.1基本概念 1.1.1DA的简介 1.1.2DA0832芯片 1.1.3PCF8591芯片 1.2代码 1.2.1DAC8053的代码 1.2.2PCF8951的代码 1.3仿真 1.3.1DAC0832的仿真 1.3.2PFC8951的仿真 2.AD转换原理 2.1AD的基本概念 2.1.1AD的简介 2.1.2ADC0809的介绍 2.1.3XPT2…...

【Python机器学习】长短期记忆网络(LSTM)
目录 随时间反向传播 实践 模型的使用 脏数据 “未知”词条的处理 字符级建模(英文) 生成聊天文章 进一步生成文本 文本生成的问题:内容不受控 其他记忆机制 更深的网络 尽管在序列数据中,循环神经网络为对各种语言关系…...

【Go】使用Goland创建第一个Go项目
✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,…...

STM32学习笔记(一、使用DAP仿真器下载程序)
我们想要使用32单片机,总共包含四个步骤: 1、硬件连接 2、仿真器配置 3、编写程序 4、下载程序 一、第一个问题(硬件连接):如何进行硬件连接,才能够启动32板子并能够下载程序呢? 答&#…...

储能运维管理云平台解决方案EMS能量管理系统
在储能行业蓬勃发展的今天,储能运维管理的重要性日益凸显。而储能运维管理云平台的出现,正为储能系统的稳定运行和高效管理注入了新的活力。 一、储能运维管理面临的挑战 传统的储能运维管理方式往往依赖人工巡检和现场操作,存在诸多问题。比…...

网络药理学:16、速通流程版
一、筛选疾病靶点 GeneCards 下载数据得到GeneCards-SearchResult.csv通过Relevance score≥1.0得到GeneCards.csv步骤2只保留Gene Symbol,即基因名这一列得到GeneCards_gene_names.csv OMIM 下载数据得到OMIM-Gene-Map-Retrieval.xlsx只保留Gene/Locus…...

P2515 [HAOI2010] 软件安装
~~~~~ P2515 [HAOI2010] 软件安装 ~~~~~ 总题单链接 思路 ~~~~~ 发现构成的图是一个森林和一些环。 ~~~~~ 对于森林,建一个虚点然后树形 D P DP DP 即可。 ~~~~~ 对于环,发现要么把这个环上的每一个点都选了,要么每一个都不选。所以可以先缩…...

51单片机快速入门之定时器和计数器
51单片机快速入门之定时器 断开外部输入 晶振振荡 假设为 12MHz 12分频之后,为1MHz 当其从0-65536 时,需要65536μs 微秒 也就是65.536ms 毫秒 溢出(值>65536 时)>中断>执行中断操作 假设需要1ms后产生溢出,则需要设置初始值为64536 此时定时器会从 64536 开始计…...

【计算机网络 - 基础问题】每日 3 题(一)
✍个人博客:Pandaconda-CSDN博客 📣专栏地址:http://t.csdnimg.cn/fYaBd 📚专栏简介:在这个专栏中,我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话,欢迎点赞👍收藏&…...

Unity全面取消Runtime费用 安装游戏不再收版费
Unity宣布他们已经废除了争议性的Runtime费用,该费用于2023年9月引入,定于1月1日开始收取。Runtime费用起初是打算根据使用Unity引擎安装游戏的次数收取版权费。2023年9月晚些时候,该公司部分收回了计划,称Runtime费用只适用于订阅…...

IDEA测试类启动报 “java: 常量字符串过长” 解决办法
目录标题 问题描述问题分析解决办法其他办法 问题描述 问题分析 字符串长度过长,导致 idea 默认使用的 javac 编译器编译不了。 查询资料发现,原因是javac在编译期间,常量字符串最大长度为65534。 解决办法 Javac 编译器改为 Eclipse 编译…...