Unity啟動事件-監聽:InitializeOnLoad


[InitializeOnLoad] :在啟動Unity的時候運行編輯器腳本

 官方案例:
  1. using UnityEngine;
  2. using UnityEditor;
  3. [InitializeOnLoad]
  4. publicclassStartup{
  5. staticStartup()
  6. {
  7. Debug.Log("Up and running");
  8. }
  9. }
在啟動Unity的時候打印一句話。
 
e.g. siki:
  1. using UnityEditor;
  2. using UnityEngine;
  3. [InitializeOnLoad]
  4. publicclassAppload
  5. {
  6. staticAppload()
  7. {
  8. bool hasKey =PlayerPrefs.HasKey("sikiwelcomescreen");
  9. if(hasKey==false)
  10. {
  11. //EditorApplication.update += Update;
  12. PlayerPrefs.SetInt("sikiwelcomescreen",1);
  13. WelcomeScreen.ShowWindow();
  14. }
  15. }
  16. //static void Update()
  17. //{
  18. // bool isSuccess = EditorApplication.ExecuteMenuItem("Welcome Screen");
  19. // if (isSuccess) EditorApplication.update -= Update;
  20. //}
  21. }
  22. publicclassWelcomeScreen:EditorWindow
  23. {
  24. privateTexture mSamplesImage;
  25. privateRect imageRect =newRect(30f,90f,350f,350f);
  26. privateRect textRect =newRect(15f,15f,380f,100f);
  27. publicvoidOnEnable()
  28. {
  29. //this.mWelcomeScreenImage = EditorGUIUtility.Load("WelcomeScreenHeader.png") as Texture;
  30. //BehaviorDesignerUtility.LoadTexture("WelcomeScreenHeader.png", false, this);
  31. this.mSamplesImage =LoadTexture("wechat.jpg");
  32. }
  33. TextureLoadTexture(string name)
  34. {
  35. string path ="Assets/PlayMaker/Editor/";
  36. return(Texture)AssetDatabase.LoadAssetAtPath(path + name,typeof(Texture));
  37. }
  38. publicvoidOnGUI()
  39. {
  40. //GUI.DrawTexture(this.mWelcomeScreenImageRect, this.mWelcomeScreenImage);
  41. GUIStyle style =newGUIStyle();
  42. style.fontSize =14;
  43. style.normal.textColor =Color.white;
  44. GUI.Label(this.textRect,"歡迎掃一掃siki的微信,關注微信號\n我會在上面推送一套關於獨立游戲開發者的游戲視頻教程 免費的!\n時刻更新中!\n這個頁面只會顯示一次",style);
  45. GUI.DrawTexture(this.imageRect,this.mSamplesImage);
  46. }
  47. publicstaticvoidShowWindow()
  48. {
  49. WelcomeScreen window =EditorWindow.GetWindow<WelcomeScreen>(true,"Hello 你好 我是你們最親愛的siki老師");
  50. window.minSize = window.maxSize =newVector2(410f,470f);
  51. UnityEngine.Object.DontDestroyOnLoad(window);
  52. }
  53. }
 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM