1. Creation of UIType class
indexOf("/") : Returns the index position of the first occurrence of the "/" character in this instance. The subscript of the instance starts from 0. If it is not found, -1 is returned.
indexOf("/", 7) : Returns the position of the first occurrence of "/" starting from index 7 in this instance. If not found, returns -1.
indexOf is from left to right, and lastIndexOf is from right to left. Although the search directions are different, the character subscripts still increase by 1 from left to right, starting from 0.
lastIndexOf("/") : Returns the index position of the last occurrence of "/" in this instance. That is, search from right to left to find the position of the first occurrence of "/". If it is not found, -1 is returned.
lastIndexOf("/", 7) : Returns the position of the last occurrence of "/" in the substring starting from index 0 to ending at index 7 in this instance. That is, search from right to left to find the position of the first occurrence of "/". If it is not found, -1 is returned.
subString : intercept the string. subString(7,2) means starting from subscript 7, intercepting a string of length 2, subString(7) means starting from subscript 7, intercepting to the end of the string. Name = path.Substring(path.LastIndexOf('/') + 1); The subscript is intercepted from the first character after /, which is our path name.
This is a class for UI panels where the name is retrieved from the prefab by address. After we load the address through resources.load, we can use path.Substring(path.LastIndexOf('/')+1) to perfectly solve the problem of clone after the name (the preset generated through instantiation has a clone)
/// <summary>
/// 存储单个Ui的信息,包括名字和路径
/// </summary>
public class UIType {
/// <summary>
/// UI名字
/// </summary>
public string Name { get; private set; }
/// <summary>
/// UI路径
/// </summary>
public string Path { get; private set; }
//构造函数获取路径
public UIType(string path) {
Path = path;
//找最后一个斜杠索引的位置, + 1为/后的位置,名字开头的第一个字符
Name = path.Substring(path.LastIndexOf('/') + 1);
}
}
2. Creation of BasePanel class
/// <summary>
/// 所有UI面板的父类,包含UI面板的状态信息
/// </summary>
public abstract class BasePanel
{
/// <summary>
/// UI信息
/// </summary>
public UIType UIType { get; private set; }
/// <summary>
/// UI进行时所进行的操作,虚方法(方便继承),只会执行一次
/// </summary>
public virtual void OnEnter() {
}
/// <summary>
/// UI暂停时执行的操作
/// </summary>
public virtual void OnPuse() { }
/// <summary>
/// UI重新开始时执行的操作
/// </summary>
public virtual void OnResume() { }
/// <summary>
/// UI退出时执行的操作
/// </summary>
public virtual void OnExit() { }
}
3.Creation of UIManger class
public class UIManager
{
/// <summary>
/// 存储所有UI信息的字典,每一个UI信息都会对应一个gameObj
/// </summary>
private Dictionary<UIType, GameObject> dicUI;
//构造函数对其进行初始化
public UIManager(){
dicUI = new Dictionary<UIType, GameObject>();
}
/// <summary>
/// 获取一个UI对象,传一个UUItype类,命名为type
/// </summary>
/// <param name="type">UI信息</param>
/// <returns></returns>
public GameObject GetSingleUI(UIType type) {
//设置一个游戏物体父对象,查找游戏画布的名字
GameObject parent = GameObject.Find("Canvas");
//判断是否为空物体
if(!parent){
Debug.LogError("画布不存在,请仔细查找有无这个对象,返回为空");
return null;
}
//单例:检查是否有,有就返回,没有就创建
if (dicUI.ContainsKey(type))
return dicUI[type];
GameObject ui = GameObject.Instantiate(Resources.Load<GameObject>(type.Path), parent.transform);
//则不带clone后缀(实例化生成会clone)
ui.name = type.Name;
return ui;
}
/// <summary>
/// 销毁一个UI对象
/// </summary>
/// <param name="type">UI信息</param>
public void DestroyUI(UIType type) {
//有问题,递归了,容易造成死循环,若不递归,则下列两行代码位置互换
if (dicUI.ContainsKey(type)) {
DestroyUI(type);
dicUI.Remove(type); }
}
}
(Note, there is a recursive loop bug)
Note : The C# language supports recursion , that is, a function can call itself . When using recursion , you need to define a condition for exiting from the function, otherwise it will enter an infinite loop .
Reference article: