在编辑器下运行协同(IEnumerator)函数,只需在函数头上加一个[ExecuteInEditMode]:
1 2 3 4 5 6 7 8 9 10 11 |
[ExecuteInEditMode] protected void Test() { Debug.Log("Call in Test Mode"); StartCoroutine(Post()); } protected IEnumerator Post() { //you code } |
在编辑器下快速调用某个函数:
1 2 3 4 5 6 7 8 9 10 11 |
[ContextMenu("MzDebug")] public void MzDebug() { //you code } [ContextMenu("MzEditor")] public void MzEditor() { //you code } |
对应截图:(右键你的脚本)
在添加脚本时,实例你的序列化变量:
1 2 3 4 5 6 7 8 9 10 11 12 |
using UnityEngine; public class UIDailyPanel : MonoBehaviour { [SerializeField] private UIDailyItem[] items = null; private void Start(){} private void Reset() { items = GetComponentsInChildren<UIDailyItem>(); } } |
效果示意图(如果是已经加好的脚本,可以右键脚本 -> Reset):
在只知道脚本类型的情况下,取一个类的Path
1 2 3 4 5 |
//https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerfilepathattribute?view=netcore-3.1 static public string GetPath([System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "") { return sourceFilePath; } |
比较神奇的一个类,应该归属于.net,在制作尝试的编辑器时或会用到,当然也可以Debug.Log(“”) 的方式去截取:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
public List<string> listStr = new List<string>(); private bool hasEvent = false; public void AddEvent() { if (!hasEvent) { hasEvent = true; Application.logMessageReceived += HandleLog; } } public void RemoveEvent() { if (hasEvent) { hasEvent = false; Application.logMessageReceived -= HandleLog; } } public List<Key_Value> GetaPaths() { paths = new List<Key_Value>(); for (int i = 0; i < listStr.Count; i++) { Key_Value kv = new Key_Value(); var strs = listStr[i].Split(':'); kv.className = strs[0]; kv.path = strs[1].Split(' ')[2]; kv.path = kv.path.Substring(6); paths.Add(kv); } return paths; } private void HandleLog(string condition, string stackTrace, LogType type) { System.IO.StringReader sr = new System.IO.StringReader(stackTrace); sr.ReadLine(); string line = sr.ReadLine(); listStr.Add(line); } //listStr 则为得到的字符串,其中包括Path private void Test() { AddEvent(); Debug.Log(""); RemoveEvent(); } |
稍微麻烦点。