当前位置: 首页 > news >正文

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类似于桌面精灵的功能实现

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

Audio Over IP的PTP时钟初探

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

【加密社】深入理解TON智能合约 (FunC语法)

king: 摘要&#xff1a;在TON&#xff08;TheOpenNetwork&#xff09;区块链平台中&#xff0c;智能合约扮演着举足轻重的角色。本文将通过分析一段TON智能合约代码 带领读者学习dict&#xff08;字典&#xff09;和list&#xff08;列表&#xff09;在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 - …...

移动应用开发与测试赛题

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

Qt常用控件——QLineEdit

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

(postman)接口测试进阶实战

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

R语言统计分析——功效分析(比例、卡方检验)

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

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

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

ARCGIS PRO DSK MapTool

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

国网B接口 USC安防平台 海康摄像机配置

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

Win10安装.net FrameWork3.5失败解决方法

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

【pipenv】—— 虚拟环境管理工具近乎全面的总结

安装 ​pip install pipenv​ 使用和配置 设置虚拟环境文件创建在项目根目录 添加环境变量&#xff1a;WORKON_HOME​PIPENV_VENV_IN_PROJECT​ 创建虚拟环境时&#xff0c;自动换用指定的pip源 添加环境变量&#xff1a;PIPENV_TEST_INDEX​https://pypi.tuna.tsinghua.edu…...

windows C++-并行编程-并行算法(五) -选择排序算法

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

【系统架构设计师-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 标准库提供的算法。并行算法由并发运行时中的现有功能组成。 若要对数据源操作进行并行化&#xff0c;一个必要步骤是将源分区为可由多个线程同时访问的多个部分。 分区程序将指定并行算法应如何在线…...

下载 llama2-7b-hf 全流程【小白踩坑记录】

1、文件转换 在官网 https://ai.meta.com/llama/ 申请一个账号&#xff0c;选择要下载的模型&#xff0c;会收到一个邮件&#xff0c;邮件中介绍了下载方法 执行命令 git clone https://github.com/meta-llama/llama.git​ &#xff0c;然后执行 llama/download.sh&#xff0c…...

Codeforces practice C++ 2024/9/11 - 2024/9/13

D. Mathematical Problem Codeforces Round 954 (Div. 3) 原题链接&#xff1a;https://codeforces.com/contest/1986/problem/D 题目标签分类&#xff1a;brute force&#xff0c;dp&#xff0c;greedy&#xff0c;implementation&#xff0c;math&#xff0c;two pointers…...

RabbitMQ创建交换机和队列——配置类 注解

交换机的类型 Fanout&#xff1a;广播&#xff0c;将消息交给所有绑定到交换机的队列。 Direct&#xff1a;订阅&#xff0c;基于RoutingKey&#xff08;路由key&#xff09;发送给订阅了消息的队列。 Topic&#xff1a;通配符订阅&#xff0c;与Direct类似&#xff0c;只不…...

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)

目录 随时间反向传播 实践 模型的使用 脏数据 “未知”词条的处理 字符级建模&#xff08;英文&#xff09; 生成聊天文章 进一步生成文本 文本生成的问题&#xff1a;内容不受控 其他记忆机制 更深的网络 尽管在序列数据中&#xff0c;循环神经网络为对各种语言关系…...

【Go】使用Goland创建第一个Go项目

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…...

STM32学习笔记(一、使用DAP仿真器下载程序)

我们想要使用32单片机&#xff0c;总共包含四个步骤&#xff1a; 1、硬件连接 2、仿真器配置 3、编写程序 4、下载程序 一、第一个问题&#xff08;硬件连接&#xff09;&#xff1a;如何进行硬件连接&#xff0c;才能够启动32板子并能够下载程序呢&#xff1f; 答&#…...

储能运维管理云平台解决方案EMS能量管理系统

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

网络药理学:16、速通流程版

一、筛选疾病靶点 GeneCards 下载数据得到GeneCards-SearchResult.csv通过Relevance score≥1.0得到GeneCards.csv步骤2只保留Gene Symbol&#xff0c;即基因名这一列得到GeneCards_gene_names.csv OMIM 下载数据得到OMIM-Gene-Map-Retrieval.xlsx只保留Gene/Locus&#xf…...

P2515 [HAOI2010] 软件安装

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

51单片机快速入门之定时器和计数器

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

【计算机网络 - 基础问题】每日 3 题(一)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏&…...

Unity全面取消Runtime费用 安装游戏不再收版费

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

IDEA测试类启动报 “java: 常量字符串过长” 解决办法

目录标题 问题描述问题分析解决办法其他办法 问题描述 问题分析 字符串长度过长&#xff0c;导致 idea 默认使用的 javac 编译器编译不了。 查询资料发现&#xff0c;原因是javac在编译期间&#xff0c;常量字符串最大长度为65534。 解决办法 Javac 编译器改为 Eclipse 编译…...