//脚本放在Editor文件夹中 using UnityEngine; using UnityEditor; public class Script_03_03 { [InitializeOnLoadMethod] //Unity编译后立刻执行这个方法,监听事件 static void InitializeOnloadMethod() { Debug.LogError("编译后立刻执行"); EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect) { if (Selection.activeObject && guid == AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject))) { float width = 50f; selectionRect.x += (selectionRect.width - width) / 2; selectionRect.y = selectionRect.height / 4; selectionRect.width = width; selectionRect.height /= 2; GUI.color = Color.green; if (GUI.Button(selectionRect, "click")) { Debug.LogError("click:" + Selection.activeObject.name); } GUI.color = Color.white; } }; } }
当你在Project中选中一个物体时,在会该物体的区域中绘制一个按钮,并且可以点击,点击后打印该物体的名字。