Unity 全局游戏脚本的一种实现方法

来源:互联网 发布:网络启动 编辑:程序博客网 时间:2024/05/16 04:47
public class GlobalScript : MonoBehaviour {public static GlobalScript instance;void Awake() {if (instance == null) {// 判定 null 是保证场景跳转时不会出现重复的 GlobalScript 实例 (主要是跳转回上一个场景)// 在没有 GlobalScript 实例时才创建 GlobalScript 实例instance = this;DontDestroyOnLoad(gameObject);} else if (instance != this) {// 保证场景中只有唯一的 GlobalScript 实例,如果有多余的则销毁Destroy(gameObject);}}}

原创粉丝点击