转载自:/andyhebear/article/details/47833383
using UnityEngine;using UnityEditor;using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;[CanEditMultipleObjects()][CustomEditor(typeof(Transform), true)]public class SelectObjShowCodeSnapEditor : Editor {// Use this for initializationbool btnclick, quickOpen = false;Vector2 scrollPos=new Vector2(0,0);public override void OnInspectorGUI(){base.DrawDefaultInspector();MonoBehaviour[] scripts=(target as Transform).gameObject.GetComponents<MonoBehaviour>();if (scripts != null && scripts.Length > 0) {//GUILayout.BeginVertical("Box");quickOpen = EditorGUILayout.Foldout(quickOpen, new GUIContent("--- Frequent functions ---"));if (quickOpen) {AddButton("Awake", "");AddButton("Start", "");AddButton("FixedUpdate ", "");AddButton("OnEnable / OnDisable", "");GUILayout.Space(20.0f);GUILayout.BeginHorizontal();GUILayout.BeginVertical(GUILayout.Width(145.0f));AddButton("OnCollisionEnter", "Collision collision");AddButton("OnCollisionStay", "Collision collision");AddButton("OnCollisionExit", "Collision collision");GUILayout.EndVertical();GUILayout.BeginVertical(GUILayout.Width(145.0f));AddButton("OnTriggerEnter", "Collider other");AddButton("OnTriggerStay", "Collider other");AddButton("OnTriggerExit", "Collider other");GUILayout.EndVertical();GUILayout.EndHorizontal();GUILayout.Space(20.0f);GUILayout.BeginHorizontal();GUILayout.BeginVertical(GUILayout.Width(145.0f));AddButton("OnMouseEnter");AddButton("OnMouseOver");AddButton("OnMouseExit");GUILayout.EndVertical();GUILayout.BeginVertical(GUILayout.Width(145.0f));AddButton("OnMouseDown");AddButton("OnMouseDrag");AddButton("OnMouseUp");GUILayout.EndVertical();GUILayout.EndHorizontal();GUILayout.Space(20f);AddButton("Update"); AddButton("LateUpdate");GUILayout.Space(20);AddButton("OnPreCull OnBecameVisible/OnBecameInvisible OnWillRenderObject");AddButton("OnPreRender OnRenderObject OnPostRender OnRenderImage ", "");AddButton("OnGUI");AddButton("OnDrawGizmos ");GUILayout.Space(10);AddButton("OnDestroy");}GUILayout.EndVertical();////-----//if (GUILayout.Button("--- code ---")) {btnclick = !btnclick;//btnclick = EditorGUILayout.Foldout(btnclick, new GUIContent("脚本列表"));}if (btnclick) {scrollPos =EditorGUILayout.BeginScrollView(scrollPos, GUILayout.MinWidth(400), GUILayout.MinHeight(400));EditorGUILayout.BeginVertical();int c = scripts.Length;for (int i = 0; i < c; i++) {string scriptName, scriptPath;scriptName = GetAssetName(scripts[i], out scriptPath);GUILayout.Label("[" + i.ToString() + "]: " + scriptName);if (GUILayout.Button("Open")) {Process.Start(Application.dataPath + "/../" + scriptPath);}GUILayout.Space(4);string scriptTxt = System.IO.File.ReadAllText(scriptPath);GUILayout.TextArea(scriptTxt);GUILayout.Space(4);}EditorGUILayout.EndVertical();EditorGUILayout.EndScrollView();}}}void AddButton(string add, string arg = "", string argType = "") {GUI.SetNextControlName("FocusFix");if (GUILayout.Button(add, "OL Plus", GUILayout.MinWidth(80.0f))) {//AddFunction(add, arg, argType);GUI.FocusControl("FocusFix");}}public string GetAssetName(UnityEngine.MonoBehaviour asset, out string assetPath) {assetPath = AssetDatabase.GetAssetPath(MonoScript.FromMonoBehaviour(asset));//assetPath = AssetDatabase.GetAssetPath(asset.GetInstanceID());string csname= assetPath.Remove(0, assetPath.IndexOf('/') + 1);return csname;}// Update is called once per frame}
原文:/andyhebear/article/details/47833383