[InitializeOnLoad] :在啟動Unity的時候運行編輯器腳本
官方案例:
using UnityEngine;using UnityEditor;[InitializeOnLoad]publicclassStartup{staticStartup(){Debug.Log("Up and running");}}
在啟動Unity的時候打印一句話。
e.g. siki:
using UnityEditor;using UnityEngine;[InitializeOnLoad]publicclassAppload{staticAppload(){bool hasKey =PlayerPrefs.HasKey("sikiwelcomescreen");if(hasKey==false){//EditorApplication.update += Update;PlayerPrefs.SetInt("sikiwelcomescreen",1);WelcomeScreen.ShowWindow();}}//static void Update()//{// bool isSuccess = EditorApplication.ExecuteMenuItem("Welcome Screen");// if (isSuccess) EditorApplication.update -= Update;//}}publicclassWelcomeScreen:EditorWindow{privateTexture mSamplesImage;privateRect imageRect =newRect(30f,90f,350f,350f);privateRect textRect =newRect(15f,15f,380f,100f);publicvoidOnEnable(){//this.mWelcomeScreenImage = EditorGUIUtility.Load("WelcomeScreenHeader.png") as Texture;//BehaviorDesignerUtility.LoadTexture("WelcomeScreenHeader.png", false, this);this.mSamplesImage =LoadTexture("wechat.jpg");}TextureLoadTexture(string name){string path ="Assets/PlayMaker/Editor/";return(Texture)AssetDatabase.LoadAssetAtPath(path + name,typeof(Texture));}publicvoidOnGUI(){//GUI.DrawTexture(this.mWelcomeScreenImageRect, this.mWelcomeScreenImage);GUIStyle style =newGUIStyle();style.fontSize =14;style.normal.textColor =Color.white;GUI.Label(this.textRect,"歡迎掃一掃siki的微信,關注微信號\n我會在上面推送一套關於獨立游戲開發者的游戲視頻教程 免費的!\n時刻更新中!\n這個頁面只會顯示一次",style);GUI.DrawTexture(this.imageRect,this.mSamplesImage);}publicstaticvoidShowWindow(){WelcomeScreen window =EditorWindow.GetWindow<WelcomeScreen>(true,"Hello 你好 我是你們最親愛的siki老師");window.minSize = window.maxSize =newVector2(410f,470f);UnityEngine.Object.DontDestroyOnLoad(window);}}
