using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
internal class SelectionHelper
{
[InitializeOnLoadMethod]
private static void Start()
{
//在Hierarchy面板按空格键相当于开关GameObject
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
//在Project面板按空格键相当于Show In Explorer
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
}
private static void ProjectWindowItemOnGUI(string guid, Rect selectionRect)
{
if (Event.current.type == EventType.KeyDown
&& Event.current.keyCode == KeyCode.Space
&& selectionRect.Contains(Event.current.mousePosition))
{
string strPath = AssetDatabase.GUIDToAssetPath(guid);
if (Path.GetExtension(strPath) == string.Empty) //文件夹
{
Process.Start(Path.GetFullPath(strPath));
}
else //文件
{
Process.Start("explorer.exe", "/select," + Path.GetFullPath(strPath));
}
Event.current.Use();
}
}
private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
Event e = Event.current;
if (e.type == EventType.KeyDown)
{
switch (e.keyCode)
{
case KeyCode.Space:
ToggleGameObjcetActiveSelf();
e.Use();
break;
}
}
else if(e.type == EventType.MouseDown && e.button == 2)
{
SetAllActive();
e.Use();
}
}
internal static void ToggleGameObjcetActiveSelf()
{
Undo.RecordObjects(Selection.gameObjects, "Active");
foreach (var go in Selection.gameObjects)
{
go.SetActive(!go.activeSelf);
}
}
//按鼠标中键,将Root节点下的所有子物体显示出来
static void SetAllActive()
{
var children = Selection.activeGameObject.GetComponentsInChildren<Transform>(true);
foreach (var child in children)
{
var gameObj = child.gameObject;
Undo.RecordObject(gameObj, "SetActive");
gameObj.SetActive(true);
}
}
}
Knowledge points:
string s1 = Path.GetExtension("D:\\dir\\asp.net\\readme.txt"); // .txt
string s2 = Path.GetExtension("D:\\dir\\asp.net\\readme."); // zero-length string
string s3 = Path.GetExtension("D:\\dir\\asp.net\\readme"); // zero-length string
string s4 = Path.GetExtension("D:\\dir\\asp.net\\readme\\"); // zero-length string
string s5 = Path.GetExtension("D:\\"); // zero-length string
string s6 = Path.GetExtension("D:"); // zero-length string
string s1 = Path.GetFileName("D:\\dir\\asp.net\\readme.txt"); // readme.text
string s2 = Path.GetFileName("D:\\dir\\asp.net\\readme."); // readme.
string s3 = Path.GetFileName("D:\\dir\\asp.net\\readme"); // readme
string s4 = Path.GetFileName("D:\\dir\\asp.net\\readme\\"); // zero-length string
string s5 = Path.GetFileName("D:\\"); // zero-length string
string s6 = Path.GetFileName("D:"); // zero-length string
As long as it does not end with \ or /, it is treated as a file (except drive letter)
string s1 = Path.GetDirectoryName("D:\\dir\\asp.net/readme.txt"); // D:\dir\asp.net
string s2 = Path.GetDirectoryName("D:\\dir\\asp.net/readme."); // D:\dir\asp.net
string s3 = Path.GetDirectoryName("D:\\dir\\asp.net/readme"); // D:\dir\asp.net
string s4 = Path.GetDirectoryName("D:\\dir\\asp.net/readme/"); // D:\dir\asp.net\readme
string s5 = Path.GetDirectoryName("D:\\"); // null, note that it is null
string s6 = Path.GetDirectoryName("D:"); // null, note that it is null
What can System.Diagnostics.Process.Start(); do? It mainly has the following functions:
1. Open a link URL (popup window).
2. Locate and open a file directory.
3. Open the system special folder, such as "Control Panel".
(1) publicbool Start ()
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "iexplore.exe"; //IE browser, can be replaced
process.StartInfo.Arguments = "http://www.baidu.com";
process.Start();
(2) publicstaticProcessStart (ProcessStartInfostartInfo)
System.Diagnostics.ProcessStartInfo processStartInfo = new
System.Diagnostics.ProcessStartInfo();
processStartInfo.FileName = "explorer.exe"; //Explorer
processStartInfo.Arguments = @"D:\";
System.Diagnostics.Process.Start(processStartInfo);
(3) publicstaticProcessStart (stringfileName)
System.Diagnostics.Process.Start(@"D:\Program Files\Tencent\QQ\Bin\QQ.exe"); //直接
call to open the file
(4) Process.Start (StringfileName, Stringarguments)
System.Diagnostics.Process.Start("explorer.exe", "D:\\Readme.txt"); //Open the file directly
Readme.txt
The method of opening a website with process.start() has been exemplified above. Now let’s talk about how to locate a file with processs.star()
method.
This positioning method is similar to the search target on the shortcut: //explorer /select,"D:\Program Files\Tencent
\QQ\Bin\QQ.exe"
String path = @"D:\Program Files\Tencent\QQ\Bin\QQ.exe";
System.Diagnostics.Process.Start("explorer.exe", "/select," +path); //Locate and open D:\Program
Files\Tencent\QQ\Bin file directory and selected QQ.exe
Note: There is a comma (,) after /select.
[InitializeOnLoad]
[InitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
[RuntimeInitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
BeforeRuntime、Awake、OnRuntime、AfterRuntime、Start、Update
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class LoadOrderTest
{
static LoadOrderTest()
{
MyOnStart();
EditorApplication.update = MyUpdate;
}
static void MyOnStart()
{
Debug.Log("MyOnStart");
}
static void MyUpdate()
{
//if(Application.isFocused)
//{
// Debug.Log("鼠标点击进入了Game窗口");
//}
//else
//{
// Debug.Log("鼠标移除Game窗口,并点击了别的地方");
//}
if (Application.isPlaying)
{
Debug.Log("点击了 play 按钮 进入运行时");
}
else
{
Debug.Log("点击 play 按钮 进入 editor模式");
}
}
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
Debug.Log("InitializeOnLoadMethod");
}
}
This script is placed under the Editor folder. After the InitializeOnLoad feature is added, its construction method will be executed automatically. The test result is that every time the content of this class is modified, the construction method will be re-executed. You can perform some operations in the constructor to control the code execution in Editor mode.
Here I define an OnStart, and assign it to EditorApplication.update, the effect is similar to Mono's Start and Update methods.
The method defined by the InitializeOnLoadMethod attribute will also be executed after the construction method.