[Unity] 在软件标题栏显示工作路径
(一)问题
项目开发中常会有开多个分支,同时启动多个 Unity 程序的情况,来回切换的时候就容易混淆,有时候还需要用 Show In Explorer 或者其他标志来确认当前使用的是哪个分支。
于是想在标题栏上直接显示出当前的工作目录:
修改前:

原本的标题栏由项目名、场景、工作平台等文本组成
修改后:

额外显示工作路径
(二)代码
1. 声明要调用的系统接口
using System;
using System.Runtime.InteropServices;
using System.Text;
public partial class UpdateUnityEditorProcess
{
public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(HandleRef hWnd);
[DllImport("user32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private extern static int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Auto)]
public extern static int SetWindowText(int hwnd, string lpString);
}
2. 工具类
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
public partial class UpdateUnityEditorProcess
{
public IntPtr hwnd = IntPtr.Zero;
private bool haveMainWindow = false;
private IntPtr mainWindowHandle = IntPtr.Zero;
private int processId = 0;
private IntPtr hwCurr = IntPtr.Zero;
private static StringBuilder sbtitle = new StringBuilder(255);
private static string UTitle = Application.dataPath;
public static float lasttime = 0;
private static UpdateUnityEditorProcess _instance;
public static UpdateUnityEditorProcess Instance
{
get
{
if (_instance == null)
{
_instance = new UpdateUnityEditorProcess();
_instance.hwnd = _instance.GetMainWindowHandle(Process.GetCurrentProcess().Id);
}
return _instance;
}
}
public void SetTitle()
{
//UnityEngine.Debug.Log(string.Format("{0} - {1}", Time.realtimeSinceStartup, lasttime));
if (Time.realtimeSinceStartup > lasttime)
{
sbtitle.Length = 0;
lasttime = Time.realtimeSinceStartup + 2f;
int length = GetWindowTextLength(hwnd);
GetWindowText(hwnd.ToInt32(), sbtitle, 255);
string strTitle = sbtitle.ToString();
string[] ss = strTitle.Split('-');
if (ss.Length > 0 && !strTitle.Contains(UTitle))
{
SetWindowText(hwnd.ToInt32(), string.Format("{0} - {1}", UTitle, strTitle));
UnityEngine.Debug.Log("Current Unity Title: " + UTitle);
}
}
}
public IntPtr GetMainWindowHandle(int processId)
{
if (!this.haveMainWindow)
{
this.mainWindowHandle = IntPtr.Zero;
this.processId = processId;
EnumThreadWindowsCallback callback = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
EnumWindows(callback, IntPtr.Zero);
GC.KeepAlive(callback);
this.haveMainWindow = true;
}
return this.mainWindowHandle;
}
private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
{
int num;
GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if ((num == this.processId) && this.IsMainWindow(handle))
{
this.mainWindowHandle = handle;
return false;
}
return true;
}
private bool IsMainWindow(IntPtr handle)
{
return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
}
}
3. Editor 回调设置
#if UNITY_EDITOR_WIN
using UnityEditor;
[InitializeOnLoad]
class UpdateUnityEditorTitle
{
private static bool isInGame = false;
static UpdateUnityEditorTitle()
{
EditorApplication.delayCall += DoUpdateTitleFunc;
EditorApplication.playmodeStateChanged += OnPlaymodeStateChanged;
}
static void OnPlaymodeStateChanged()
{
if (EditorApplication.isPlaying == isInGame) return;
isInGame = EditorApplication.isPlaying;
UpdateUnityEditorProcess.lasttime = 0;
DoUpdateTitleFunc();
}
static void DoUpdateTitleFunc()
{
//UnityEngine.Debug.Log("DoUpdateTitleFunc");
UpdateUnityEditorProcess.Instance.SetTitle();
}
}
#endif
(三)备注
[InitializeOnLoad]的调用时机是在 Unity 初始化标题之前,所以直接在UpdateUnityEditorTitle()中设置标题的话会被覆盖掉,这里利用EditorApplication.delayCall 调用标题修改- 进入退出 GameMode 会触发 Unity 软件初始化标题,利用
EditorApplication.playmodeStateChanged来调用自定义的标题修改
[Unity] 在软件标题栏显示工作路径的更多相关文章
- Linux显示工作路径
Linux显示工作路径 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ pwd /home/youhaidong
- linux服务器上如何显示工作路径
1. 修改PS环境变量 [root@linux-node01 ~]# vi /etc/bashrc [ "$PS1" = "\\s-\\v\\\$ " ] &a ...
- pwd 显示当前所在的工作路径
1.功能说明 pwd命令是“print working directory ”首字母缩写,显示当前目录的绝对路径. 2.语法格式 pwd [option] pwd 选项 3.命令参数 参数 参数说明 ...
- C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)
C#.ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 ============================================ 使用 Application.Start ...
- 关于source insight、添加.s和.S文件,显示全部路径、加入项目后闪屏幕
1.source insight使用也有一年多时间了,今天出现建工程后添加文件“no files found” 百思不得姐: 后面发现是原工程命名时出现非法字符.重新命名就ok了. 切记切记 2.实用 ...
- Jupyter Notebook默认工作路径的修改
相信每一个学习Python的童鞋,都尝试过Jupyter Notebook,所以我也就不多介绍,真的还不错哎这软件. 不过美中不足的,就是它的默认工作路径,每次打开都是系统盘的Administrato ...
- 【原创】os.chdir设置的工作路径和sys.path之间到底是个啥关系?
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9390455.html 直接放上测试后的结论(测试代码和截图过多,有兴趣的小伙伴可自己测试,未来看情况 ...
- Ubuntu 14 下,命令行终端显示短路径
Ubuntu的终端命令行默认是长路径,即把路径深度全部显示出来,操作起来不是很方便,下面介绍命令行显示短路径的操作: $ vi ~/.bashrc 找到PS1= 的行,将\w(小写)改成\W(大写 ...
- BAT: Windows批处理更改当前工作路径
最近项目上需要获取文件夹下所有文件信息,因为文件夹是在server上,所以想用批处理bat来获取该路径下所有文件信息,然后通过任务计划管理去每天自动运行bat去更新文件信息内容. 获取文件夹下所有文件 ...
随机推荐
- 论文翻译:2019_TCNN: Temporal convolutional neural network for real-time speech enhancement in the time domain
论文地址:TCNN:时域卷积神经网络用于实时语音增强 论文代码:https://github.com/LXP-Never/TCNN(非官方复现) 引用格式:Pandey A, Wang D L. TC ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 『无为则无心』Python函数 — 39、Python中异常的传播
目录 1.异常的传播 2.如何处理异常 1.异常的传播 当在函数中出现异常时,如果在函数中对异常进行了处理,则异常不会再继续传播.如果函数中没有对异常进行处理,则异常会继续向函数调用者传播.如果函数调 ...
- linux安装第三方软件 python3
一:linux安装python3 安装第三方软件的目录 进入目录 /usr/local 下载rpm安装包 安装pyton yum安装python : yum install python3 查看pyt ...
- python03day
回顾 pycharm简单使用 while循环 结构 pass while实现打印1-2+3-4+--+99 格式化输出:针对str,让字符串中某些位置变为动态可传入的 % s str d digist ...
- 泛型编程与 OI——modint
博客链接. 在 OI 中,有大量的题目要求对一些数字取模,这便是本文写作的背景. 背景介绍 这些题目要么是因为答案太大,不方便输出结果,例如许多计数 dp:要么是因为答案是浮点数,出题人不愿意写一个确 ...
- JavaScript之Promise实现原理(手写简易版本 MPromise)
手写 Promise 实现 Promise的基本使用 Promise定义及用法详情文档:Promise MAD文档 function testPromise(param) { return new P ...
- react组件中的类调用construcor、super方法你知道多少?
constructor:在类中作为一个钩子函数,有constructor钩子函数的时候,可以定义state,如果用户不定义state的话,有无constructor钩子函数没啥区别: super:
- 浅谈VPC (转)
来源于知乎:https://zhuanlan.zhihu.com/p/33658624 VPC全称是Virtual Private Cloud,翻译成中文是虚拟私有云.但是在有些场合也被翻译成私有网络 ...
- Html 项目使用自定义字体文件问题
感谢大佬:https://zhidao.baidu.com/question/652711582735059245.html 1.首先在项目过程中新建文件夹fonts将准备好的ttf字体文件复制该文件 ...