简单的ui框架的整理与思考

    xiaoxiao2022-07-14  144

    经过这段时间对一个UI框架的学习,现在对这段时间的学习做一个总结。

    首先是关于工具类的整理

           工具类的脚本主要是用来对预制体的整体记录,将游戏内所有的预制体,遍历,登记到一个字典里。生成一个脚本。然后,在需要加载预制体的时候,在通过生成的脚本,获得预制体的路径,生成预制体。

    //通过读取流,读取文本中的所有的内容//using 语法,代码完成释放资源,析构? using (StreamReader sr = File.OpenText(filePath)) { connect = sr.ReadToEnd(); } //创建文件 File.Create(filePath); //写入文件内容 using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(content.ToString()); } //追加文件内容 File.AppendAllText(filePath, contents); //移动文件夹 Directory.Move(sourceDirName, destDirName); //获得某个文件夹下的所有文件 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles();

    MonoSingleton   脚本

             全局单例类:DontDestoryOnLoad

                     继承该类的脚本,会在此物体上挂在脚本,作为一个整个游戏的变量,记录游戏的所有的内容

                     

    public class MonoSingleton<T> : MonoBehaviour where T:MonoBehaviour { private static T instance; public static T Instance { get { if (instance == null) { if (GameObject.Find("ComSingleton") == null) { GameObject go=new GameObject("ComSingleton"); DontDestroyOnLoad(go); } if(GameObject.Find("ComSingleton") != null) { if (GameObject.Find("ComSingleton").GetComponent<T>()!=null) { instance = GameObject.Find("ComSingleton").GetComponent<T>(); } else { instance = GameObject.Find("ComSingleton").AddComponent<T>(); } } } return instance; } } }

    UIMgr脚本

          对游戏的UI的整体的操控,创建两个空物体,来进行游戏的预制体的打开显示,和关闭显示的操作。

    字典用来记录当前游戏中的预制体 通过堆栈记录对UI界面的操作。工作站显示当前游戏中的物体的界面,回收站将关闭的界面隐藏放入,每次的关闭,检查回收站的数量,超过一定的数量,移除部分的UI,另外,也可以通过堆栈,来关闭当前界面,打开上个关闭的界面。

    Dictionary<UIPrefab,GameObject> windowDic = new Dictionary<UIPrefab, GameObject>(); UIPrefab currentWindow; public Transform workstation; //工作站:打开的UI窗口 public Transform recyclePool; //回收站:关闭的UI窗口 public int recyclePoolMaxCount = 10; Stack<UIPrefab> recycleStack = new Stack<UIPrefab>(); //栈结构,存储所有关闭的窗口

     

    最新回复(0)