码迷,mamicode.com
首页 > 编程语言 > 详细

UnityEditor 的title显示工作路径

时间:2016-06-23 22:25:14      阅读:629      评论:0      收藏:0      [点我收藏+]

标签:public   title   

什么也不说,效果就是:

技术分享


代码就是

#if UNITY_EDITOR_WIN

using UnityEngine;

using UnityEditor;

using System.Runtime.InteropServices;

using System.Diagnostics;

using UnityEditor.Callbacks;

using System;

using System.Text;


[InitializeOnLoad]

class UpdateUnityEditorTitle

{

    static UpdateUnityEditorTitle()

    {

        EditorApplication.hierarchyWindowItemOnGUI += DoUpdateTitleFunc;

    }


    static void DoUpdateTitleFunc(int instanceID, Rect selectionRect)

    {

        UpdateUnityEditorProcess.Instance.SetTitle();

    }

}

public class UpdateUnityEditorAssetHandler

{

    [OnOpenAssetAttribute(1)]

    public static bool AssetHandlerStep(int instanceID, int line)

    {

        UpdateUnityEditorProcess.lasttime = 0f;

        return false; // we did not handle the open

    }

}


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;


    public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);


    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 (strTitle.Length > 0 && !ss[0].Contains(UTitle))

            {

                SetWindowText(hwnd.ToInt32(), string.Format("{0} - {1}", UTitle, strTitle));

            }

        }

    }


    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)));

    }


    [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);


}



#endif


本文出自 “new一个” 博客,请务必保留此出处http://pureivan.blog.51cto.com/2035414/1792347

UnityEditor 的title显示工作路径

标签:public   title   

原文地址:http://pureivan.blog.51cto.com/2035414/1792347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!