首页 > 代码库 > Unity启动事件-监听:InitializeOnLoad
Unity启动事件-监听:InitializeOnLoad
[InitializeOnLoad] :在启动Unity的时候运行编辑器脚本
官方案例:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class Startup {
static Startup()
{
Debug.Log("Up and running");
}
}
在启动Unity的时候打印一句话。
e.g. siki:
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class Appload
{
static Appload()
{
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;
//}
}
public class WelcomeScreen : EditorWindow
{
private Texture mSamplesImage;
private Rect imageRect = new Rect(30f, 90f, 350f, 350f);
private Rect textRect = new Rect(15f, 15f, 380f, 100f);
public void OnEnable()
{
//this.mWelcomeScreenImage = EditorGUIUtility.Load("WelcomeScreenHeader.png") as Texture;
//BehaviorDesignerUtility.LoadTexture("WelcomeScreenHeader.png", false, this);
this.mSamplesImage = LoadTexture("wechat.jpg");
}
Texture LoadTexture(string name)
{
string path = "Assets/PlayMaker/Editor/";
return (Texture)AssetDatabase.LoadAssetAtPath(path + name, typeof(Texture));
}
public void OnGUI()
{
//GUI.DrawTexture(this.mWelcomeScreenImageRect, this.mWelcomeScreenImage);
GUIStyle style = new GUIStyle();
style.fontSize = 14;
style.normal.textColor = Color.white;
GUI.Label(this.textRect, "欢迎扫一扫siki的微信,关注微信号\n我会在上面推送一套关于独立游戏开发者的游戏视频教程 免费的!\n时刻更新中!\n这个页面只会显示一次",style);
GUI.DrawTexture(this.imageRect, this.mSamplesImage);
}
public static void ShowWindow()
{
WelcomeScreen window = EditorWindow.GetWindow<WelcomeScreen>(true, "Hello 你好 我是你们最亲爱的siki老师");
window.minSize = window.maxSize = new Vector2(410f, 470f);
UnityEngine.Object.DontDestroyOnLoad(window);
}
}
来自为知笔记(Wiz)
Unity启动事件-监听:InitializeOnLoad
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。