[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去更新文件信息内容. 获取文件夹下所有文件 ...
随机推荐
- K8s中的volumes-容器数据存放类型及位置
学习对象:kubectl explain pod.spec.volumes.pod.spec.containers.image.volumeMounts 介绍Volumes 容器内部也有自己的空间,但 ...
- [Anti-AV] 从攻防对抗辩证性分析jsp免杀(一)
从攻防对抗辩证性分析jsp免杀 从最早的最朴素木马 <%@ page import="java.io.InputStream" %> <%@ page impor ...
- Android 12(S) 图形显示系统 - 应用建立和SurfaceFlinger的沟通桥梁(三)
1 前言 上一篇文章中我们已经创建了一个Native示例应用,从使用者的角度了解了图形显示系统API的基本使用,从这篇文章开始我们将基于这个示例应用深入图形显示系统API的内部实现逻辑,分析运作流程. ...
- IoC容器-Bean管理XML方式(注入外部bean)
注入属性-外部bean (1)创建两个类service类和dao类 (2)在service调用dao里面的方法 (3)在spring配置文件中进行配置
- MySQL开发篇,存储引擎的选择真的很重要吗?
前言 谁说MySQL查询千万级别的数据很拉跨?我今天就要好好的和你拉拉家常,畅谈到深夜,一起过除夕!这篇文章也是年前的最后一篇,希望能带给大家些许收获,不知不觉查找文档和参考实体书籍就写了这么多,自己 ...
- 新年好 takoyaki,期待再次与你相见
一.序 今天是中国农历一年的最后一天,往年都叫年三十,今年没有三十,最后一天是二十九.厨房的柴火味.窗外的鞭炮声还有不远处传来的说笑声,一切都是熟悉味道,新年到了,家乡热闹起来了.平常左邻右舍都是看不 ...
- 创建一个python类 ,self init相关参数的简单介绍
一 创建 ''' 一 使用python 语法 创建一个类, 探究self 是干啥的 1 创建一个对象 car 2 写入两个行参 3 定义两个方法 ''' class Car(): ''' 二 init ...
- jsp 中的绝对路径和相对路径 ./ 和 ../的区别?
原文地址! https://www.cnblogs.com/brucetie/p/4109913.html 1. 相对路径 相对路径,当前的文件,以根目录为基准,相对于另一个文件的位置. 2.绝对路径 ...
- docker常用命令、镜像命令、容器命令、数据卷,使用dockerFile创建镜像,dockefile的语法规则。
一.docker常用命令? 1. 常用帮助命令 1.1 docker的信息以及版本号 /* docker info 查看docker的信息 images2 docker本身就是一个镜像. docker ...
- Atcoder ARC-070
A 可以发现的是,次数的下界一定是使得 \(\frac{n(n + 1)}{2} \ge X\) 最小的 \(n\). 稍加思考可以发现,只需要在某一时刻停一下一定能在下界的次数内跳到恰好 \(X\) ...