上面的文章中写到了单例,我们发现代码都是大同小异。所以我们抽调相应的代码块,来帮助我们搞定单例。
using UnityEngine; using System.Collections; // 单例类:只要继承这个类,那么继承者就是一个单例类 // 限定T是一个class类型,可以new public abstract class Singleton<T> where T : class,new() { protected static T _instance = null; public static T Instance { get { if (null == _instance) { _instance = new T(); } return _instance; } } protected Singleton() { if (null != _instance) { throw new SingletonException("this" + (typeof(T)).ToString() + "singleton Instance is not null"); Init(); } } public virtual void Init() { } }那以后写单例就简单了
直接就可以Instance.
1、怎么创建一个不销毁的对象?制作好单例,我们要在项目的面板中制作一个对象让其不销毁
因为我们的对象继承MonoBehaviour,所以不能通过new这种方式获取,只能通过下面的脚本来达到切换场景不销毁。
using UnityEngine; using System.Collections; // 跨场景不销毁的单例 // 继承一个MonoBehaviour,但是T 类型是继承SYSingleton的子类 public abstract class SYSingleton<T>:MonoBehaviour where T : SYSingleton<T>{ protected static T _instance = null; public static T Instance { get { if (null == _instance) { // 去寻找一个对象 GameObject go = GameObject.Find("SYGameObject"); if (null == go) { go = new GameObject("SYGameObject"); DontDestroyOnLoad(go); // 切换场景不销毁 } // _instance = go.GetOrAdd<T>(); 将协成管理类的方法掉用 // 另一种方法: _instance = go.GetComponent<T>(); if (_instance == null) { _instance = go.AddComponent<T>(); } return _instance; } return _instance; } } private void OnApplicationQuit() { _instance = null; } } // 实际打开面板的代码都在这里面 private void OpenPanal(bool isCloseOther, Type[] uitypes, params object[] args) { if (isCloseOther) { CloseOtherPanal(); } int length = uitypes.Length; for (int i = 0; i < length; i++) { Type _uitype = uitypes[i]; GameObject panal; if (OpenPanaldic.TryGetValue(_uitype, out panal) && panal != null) { // 显示面板 } else { // 将面板数据模型Push进去 stackModels.Push(new UIModel(_uitype, "", args)); } // 如果栈的长度 > 0; if (stackModels.Count > 0) { CoroutineController.Instance.StartCoroutine(""); } } }(http://upload-images.jianshu.io/upload_images/642887-194e44ea489862ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
即便我们以后在面板中创建一个对象并起名字SYGameObjct,也是可以直接运行的,脚本也会直接挂载的。
脚本一览:
(http://upload-images.jianshu.io/upload_images/642887-82fc01f218bd290b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2、异步加载资源:UI界面数据: (http://upload-images.jianshu.io/upload_images/642887-e5dab6014275cb1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
将数据扔进栈中,判断栈中的长度来调用。
上面里面使用了单例类,来携程异步加载资源,所以我们要提供方法来进行相关的开发
(http://upload-images.jianshu.io/upload_images/642887-816d9a47abc38690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#######2-1:给预制物添加组件 解决上述问题:就是给系统的Monobehaviour 添加扩展方法 添加组件,使用下满方法搞定
#######2-2:继续扩展,给预制物下面的某一个对象进行扩展
(http://upload-images.jianshu.io/upload_images/642887-4824990f770b9530.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
同理将所有的扩展都进行相应的制作
在我们的UImananger中就可以这样去添加控件
3、基类Panal创建一个基类Panal:
在UI管理器中:
那么我们所有的UI,数据就在SetUIWhenOpening方法中实现 例如:背包格子
这个时候就可以先测试一下使用了:例如每秒做件事
需要路径:我们就将路径扔进去,但是考虑扩展性,所以直接来一个常量类专门做这件事情。
3-1 常量类制作好常量类,那么就可以搞定很多事情
3、Unity - Vs调试工具https://marketplace.visualstudio.com/items?itemName=SebastienLebreton.VisualStudio2015ToolsforUnity
安装完成就可以选择并启用调试了。 (http://upload-images.jianshu.io/upload_images/642887-246bf5f67859fc22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
---来自腾讯云社区的---雷潮
微信扫一扫打赏
支付宝扫一扫打赏