(一)问题

项目开发中常会有开多个分支,同时启动多个 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

(三)备注

  1. [InitializeOnLoad] 的调用时机是在 Unity 初始化标题之前,所以直接在UpdateUnityEditorTitle() 中设置标题的话会被覆盖掉,这里利用EditorApplication.delayCall 调用标题修改
  2. 进入退出 GameMode 会触发 Unity 软件初始化标题,利用 EditorApplication.playmodeStateChanged 来调用自定义的标题修改

[Unity] 在软件标题栏显示工作路径的更多相关文章

  1. Linux显示工作路径

    Linux显示工作路径 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ pwd /home/youhaidong

  2. linux服务器上如何显示工作路径

    1. 修改PS环境变量 [root@linux-node01 ~]# vi /etc/bashrc [ "$PS1" = "\\s-\\v\\\$ " ] &a ...

  3. pwd 显示当前所在的工作路径

    1.功能说明 pwd命令是“print working directory ”首字母缩写,显示当前目录的绝对路径. 2.语法格式 pwd [option] pwd 选项 3.命令参数 参数 参数说明 ...

  4. C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖)

    C#.ASP.NET获取当前应用程序的绝对路径,获取程序工作路径   ============================================ 使用 Application.Start ...

  5. 关于source insight、添加.s和.S文件,显示全部路径、加入项目后闪屏幕

    1.source insight使用也有一年多时间了,今天出现建工程后添加文件“no files found” 百思不得姐: 后面发现是原工程命名时出现非法字符.重新命名就ok了. 切记切记 2.实用 ...

  6. Jupyter Notebook默认工作路径的修改

    相信每一个学习Python的童鞋,都尝试过Jupyter Notebook,所以我也就不多介绍,真的还不错哎这软件. 不过美中不足的,就是它的默认工作路径,每次打开都是系统盘的Administrato ...

  7. 【原创】os.chdir设置的工作路径和sys.path之间到底是个啥关系?

    转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9390455.html   直接放上测试后的结论(测试代码和截图过多,有兴趣的小伙伴可自己测试,未来看情况 ...

  8. Ubuntu 14 下,命令行终端显示短路径

    Ubuntu的终端命令行默认是长路径,即把路径深度全部显示出来,操作起来不是很方便,下面介绍命令行显示短路径的操作: $ vi ~/.bashrc 找到PS1=   的行,将\w(小写)改成\W(大写 ...

  9. BAT: Windows批处理更改当前工作路径

    最近项目上需要获取文件夹下所有文件信息,因为文件夹是在server上,所以想用批处理bat来获取该路径下所有文件信息,然后通过任务计划管理去每天自动运行bat去更新文件信息内容. 获取文件夹下所有文件 ...

随机推荐

  1. C++内嵌汇编 教程1

    注:本文的所有代码是在我自己的VS2008中测试的,由于环境的差别,不能保证能在所有的编译器上运行. 1.内嵌汇编介绍 在C++中,可以通过__asm关键字来嵌入汇编语言.例如 int main(){ ...

  2. C# 代码性能优化举例

    普通人如果说什么事情慢,指的是 5 分钟,10 分钟,或者 1 个小时.2 个小时.而程序员要说什么事情慢,他们指的是 2 纳秒. 每个纳秒对程序员来说都是非常宝贵的,所以,要对代码进行优化,优化,再 ...

  3. 学习AJAX必知必会(1)~Ajax

    一.ajax(Asynchronous JavaScript And XML,即异步的 JS 和 XML) 1.通过 AJAX 可以在浏览器中向服务器发送异步请求实现无刷新获取数据. 2.优势:无刷新 ...

  4. gin中HTML渲染

    package main import ( "github.com/gin-gonic/gin" "net/http" ) func login(ctx *gi ...

  5. Go 面向对象编程应用

    #### Go 面向对象编程应用前面学习了很多的基础知识,这一节来实际写一个小案例:涉及到的知识: 1. 数组的基本使用2. 结构体3. 切片 4. 方法5. 循环6. 函数返回值(命名返回值,普通返 ...

  6. java内部类细节

    1 package face_09; 2 /* 3 * 为什么内部类能直接访问外部类中的成员呢? 4 * 那是因为内部类持有了外部类的引用. 外部类名.this 5 * 6 */ 7 class Ou ...

  7. jsp加载css失效的解决方法

    问题: 有时候大家修改了css文件里的样式,但是刷新浏览器,利用工具看样式的时候,发现样式根本没加载或者说没更新, 其实这出现的问题就是缓存的问题 解决: 如何避免发生这种事,其实很简单,只需要每次请 ...

  8. iBooker AI+财务提升星球 2020.4 热门讨论

    比特币量化套利的心路历程(附python量化招聘)(分享自知- 如何选择一份好的工作? 你知道为什么大家都想去好公司吗? 不- #财务知识# 可转债套利 辉丰转债128012套利之三个知道- #财务知 ...

  9. 关于C++ scanf的一个小知识

    关于C++的scanf,其实在使用时有一个注意的点. 我们来看一个简单的例子. 对于输入的一行,如果这一行的开头需要输入一个字符,例如这样的输入: A 10 20 B 30 A 3 50 ... 我们 ...

  10. 根据指定手绘图纸照片行政区划自定义绘制对应区域边界生成geoJOSN的解决方案

    项目需求 指定某区域(非省市区县乡镇标准行政区划),做功能边界分区,实现Echarts地理坐标数据可视化展示. 提供信息 项目分析 要在Echarts上实现地理坐标可视化,必须使用geoJSON格式文 ...