您的位置:首页 > 产品设计 > UI/UE

转载蛮牛上很好的UI管理脚本

2016-06-23 20:36 513 查看
1.UIManager管理类

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class UIManager
{
public enum EUI
{
UILogin,

Num,
}

private static Dictionary<EUI, UIBase> dUIs = new Dictionary<EUI, UIBase>();
public static UIBase GetUI(EUI eUI)
{
if (dUIs.ContainsKey(eUI))
{
return dUIs[eUI];
}
return null;
}

public static UIBase CreateUI(EUI eUI)
{
if (dUIs.ContainsKey(eUI))
{
if (dUIs[eUI] != null)
{
return dUIs[eUI];
}
else
{
dUIs.Remove(eUI);
}
}

Transform tr = Resources.Load<Transform>("UI/" + eUI);
Transform uiTr = null;
if (tr == null)
{
//加载AssetBundle
}
else
{
uiTr = GameObject.Instantiate(tr) as Transform;
}
uiTr.name = eUI.ToString();

UIBase ui = uiTr.GetComponent<UIBase>();
if (ui == null)
{
ui = uiTr.gameObject.AddComponent(Type.GetType(eUI.ToString())) as UIBase;
}
ui.transform.localPosition = Vector3.zero;
dUIs.Add(eUI, ui);
return ui;
}
}


2.UIBase基类

using UnityEngine;
using System.Collections;

public abstract class UIBase : MonoBehaviour
{
public virtual UIManager.EUI eUI
{
get
{
return UIManager.EUI.Num;
}
}

protected bool bInited;
void Awake()
{
InitComponents();
bInited = true;
}

protected virtual void InitComponents()
{

}

public virtual void OnCloseUI()
{
Destroy(this.gameObject);
}
}


3.递归查找子物体的类

using UnityEngine;
using System.Collections;

public static class UStaticFuncs
{
public static Transform FindChild(Transform tr, string childName)
{
for (int i = 0;i<tr.childCount;i++)
{
if (tr.GetChild(i).name == childName)
{
Transform t = tr.GetChild(i);
if (t != null)
{
return t;
}
}
else
{
Transform t = FindChild(tr.GetChild(i), childName);
if (t != null)
{
return t;
}
}
}
return null;
}
public static T FindChildComponent<T>(Transform tr, string childName)
{
Transform t = FindChild(tr, childName);
return t.GetComponent<T>();
}
}


4.资源池的管理类

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UResourcePool
{
private static UResourcePool s_instance;
private static GameObject poolParent;
public static UResourcePool Instance
{
get
{
if (s_instance == null)
{
s_instance = new UResourcePool();

poolParent = new GameObject();
poolParent.name = "PoolParent";
poolParent.SetActive(false);
}
return s_instance;
}
}

private Dictionary<string, List<GameObject>> dPool = new Dictionary<string, List<GameObject>>();
public GameObject OnGetResource(string sResource, string path)
{
if (!path.Contains("/"))
{
path = path + "/";
}
if (!dPool.ContainsKey(sResource) || dPool[sResource].Count == 0)
{
GameObject go = Resources.Load<GameObject>(path + sResource);
if (go == null)
{
Debug.LogError("Cannot find resourece " + path + sResource);
}
GameObject result = GameObject.Instantiate(go) as GameObject;
result.name = sResource;
return result;
}
else
{
GameObject result = dPool[sResource][0];
dPool[sResource].RemoveAt(0);
result.transform.localPosition = Vector3.zero;
result.transform.localRotation = Quaternion.Euler(Vector3.zero);
if (result.transform.parent != poolParent.transform)
{
Debug.LogError("get resource out of pool");
}
return result;
}
}
public T OnGetResource<T>(string sResource, string path)
where T : Object
{
if (!path.Contains("/"))
{
path = path + "/";
}
if (!dPool.ContainsKey(sResource) || dPool[sResource].Count == 0)
{
T go = Resources.Load<T>(path + sResource);
if (go == null)
{
Debug.LogError("Cannot find resourece " + path + sResource);
}
T result = GameObject.Instantiate(go) as T;
result.name = sResource;
return result;
}
return null;
}
public void OnReturnResource(GameObject go)
{
if (go.transform.parent == poolParent.transform)
{
return;
}
if (!dPool.ContainsKey(go.name))
{
dPool.Add(go.name, new List<GameObject>());
}
go.transform.parent = poolParent.transform;
dPool[go.name].Add(go);
}

public void DoDestroy()
{       dPool.Clear();
s_instance = null;
GameObject.Destroy(poolParent.gameObject);
}
}


5.原文地址

http://www.manew.com/forum.php?mod=viewthread&tid=90695&page=0

http://www.manew.com/thread-90727-1-1.html

真的非常好用,我也是收藏着,项目中用起来很方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ui 管理 脚本