首页 > 代码库 > Unity启动事件-监听:InitializeOnLoad

Unity启动事件-监听:InitializeOnLoad

[InitializeOnLoad] :在启动Unity的时候运行编辑器脚本
 官方案例:
  1. using UnityEngine;
  2. using UnityEditor;
  3. [InitializeOnLoad]
  4. public class Startup {
  5. static Startup()
  6. {
  7. Debug.Log("Up and running");
  8. }
  9. }
在启动Unity的时候打印一句话。

e.g. siki:
  1. using UnityEditor;
  2. using UnityEngine;
  3. [InitializeOnLoad]
  4. public class Appload
  5. {
  6. static Appload()
  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. public class WelcomeScreen : EditorWindow
  23. {
  24. private Texture mSamplesImage;
  25. private Rect imageRect = new Rect(30f, 90f, 350f, 350f);
  26. private Rect textRect = new Rect(15f, 15f, 380f, 100f);
  27. public void OnEnable()
  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. Texture LoadTexture(string name)
  34. {
  35. string path = "Assets/PlayMaker/Editor/";
  36. return (Texture)AssetDatabase.LoadAssetAtPath(path + name, typeof(Texture));
  37. }
  38. public void OnGUI()
  39. {
  40. //GUI.DrawTexture(this.mWelcomeScreenImageRect, this.mWelcomeScreenImage);
  41. GUIStyle style = new GUIStyle();
  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. public static void ShowWindow()
  48. {
  49. WelcomeScreen window = EditorWindow.GetWindow<WelcomeScreen>(true, "Hello 你好 我是你们最亲爱的siki老师");
  50. window.minSize = window.maxSize = new Vector2(410f, 470f);
  51. UnityEngine.Object.DontDestroyOnLoad(window);
  52. }
  53. }



来自为知笔记(Wiz)


Unity启动事件-监听:InitializeOnLoad