Unity启动事件-监听:InitializeOnLoad

Posted Eam鳄鱼泪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. }





以上是关于Unity启动事件-监听:InitializeOnLoad的主要内容,如果未能解决你的问题,请参考以下文章

Unity中的事件监听

Unity C# 事件监听和广播

unity打包webgl程序和js键盘监听事件冲突的问题。

Unity中Button按钮的触发监听事件

#你好Unity3D#Hierarchy视图监听gameObject点击事件

高级C#信使(译) - Unity维基百科