Unity启动事件-监听:InitializeOnLoad

时间:2024-11-18 12:05:07

[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. }