文件夹结构:
MyScripts:
Common:EnumDefine.cs
ResourceManager.cs
Core:
SingleTon.cs
UISceneCtrBase.cs
UIWindowCtrBase.cs
Tools:
GameTools.cs
UICtr:
LoginSceneCtr.cs
UILoginSceneCtr.cs
UILoginWindowCtr.cs
UIMgr
UISceneManager.cs
UIWindowManager.cs
LoginSceneCtr:挂载摄像机上
using UnityEngine;
using System.Collections;
public class LoginSceneCtr : MonoBehaviour {
// Use this for initialization
void Start () {
UISceneManager.Instance.OpenUIScene (UISceneType.Login);
}
}
UISceneManager:初始化处场景并保存在currentUISceneCtr中
using UnityEngine;
using System.Collections;
public class UISceneManager : SingleTon<UISceneManager> {
//每个场景都存在一个UISceneCtrBase的脚本,表示当前场景UI控制器
public UISceneCtrBase currentUISceneCtr;
public void OpenUIScene(UISceneType sceneType){
GameObject sceneUI = null;
switch (sceneType) {
case UISceneType.Login:
sceneUI = ResourceManager.Instance.Load ("UI Root_LoginScene", resType: ResourceType.UIScene);
break;
case UISceneType.Battle:
sceneUI = ResourceManager.Instance.Load ("UI Root_BattleScene", resType: ResourceType.UIScene);
break;
default:
break;
}
currentUISceneCtr = sceneUI.GetComponent<UISceneCtrBase> ();
}
}
UISceneCtrBase :挂载"UI Root_LoginScene"预设体上,给锚点Container牵线
using UnityEngine;
using System.Collections;
public class UISceneCtrBase : MonoBehaviour {
//挂点位置信息
//挂点 中间
public Transform centerContainer;
//挂点 左上
public Transform leftTopContainer;
//左下
public Transform leftBottomContainer;
public Transform rightTopContainer;
public Transform rightBottomContainer;
public Transform rightCenterContainer;
// Use this for initialization
void Start () {
OnStart ();
}
// Update is called once per frame
void Update () {
OnUpdate ();
}
public virtual void OnStart(){}
public virtual void OnUpdate(){}
}
UILoginSceneCtr :挂载"UI Root_LoginScene"预设体上
using UnityEngine;
using System.Collections;
public class UILoginSceneCtr : UISceneCtrBase {
public override void OnStart(){
StartCoroutine (OpenWindow());
}
IEnumerator OpenWindow(){
yield return new WaitForSeconds (0.2f);
UIWindowManager.Instance.OpenUIWindow (UIWindowType.Login);
}
}
using System.Collections;
using System.Collections.Generic;
public class UIWindowManager : SingleTon<UIWindowManager> {
//用于存储被打开窗口UI的字典<key,Value>
private Dictionary<UIWindowType,GameObject> m_Dic_UIWindow = new Dictionary<UIWindowType,GameObject> ();
public void OpenUIWindow(UIWindowType windowType){
if(m_Dic_UIWindow.ContainsKey(windowType)){
return;
}
GameObject windowUI=null;
switch(windowType){
case UIWindowType.Login:
windowUI = ResourceManager.Instance.Load ("UIWindow_Login", resType: ResourceType.UIWindow);
break;
case UIWindowType.Register:
windowUI = ResourceManager.Instance.Load ("UIWindow_Register", resType: ResourceType.UIWindow);
break;
case UIWindowType.Header:
windowUI = ResourceManager.Instance.Load ("UIWindow_Header", resType: ResourceType.UIWindow);
break;
case UIWindowType.Function:
windowUI = ResourceManager.Instance.Load ("UIWindow_Function", resType: ResourceType.UIWindow);
break;
case UIWindowType.Skill:
windowUI = ResourceManager.Instance.Load ("UIWindow_Skill", resType: ResourceType.UIWindow);
break;
default:
break;
}
UIWindowCtrBase windowUICtr = windowUI.GetOrAddComponent<UIWindowCtrBase> ();
windowUICtr.windowType = windowType;
UIWindowContainerType windowConType = windowUICtr.windowConType;
Transform transParentWindowContainer = null;
switch(windowConType){
case UIWindowContainerType.Center:
transParentWindowContainer = UISceneManager.Instance.currentUISceneCtr.centerContainer;
break;
case UIWindowContainerType.LeftBottom:
transParentWindowContainer = UISceneManager.Instance.currentUISceneCtr.leftBottomContainer;
break;
case UIWindowContainerType.LeftTop:
transParentWindowContainer = UISceneManager.Instance.currentUISceneCtr.leftTopContainer;
break;
case UIWindowContainerType.RightBottom:
transParentWindowContainer = UISceneManager.Instance.currentUISceneCtr.rightBottomContainer;
break;
case UIWindowContainerType.RightCenter:
transParentWindowContainer = UISceneManager.Instance.currentUISceneCtr.rightCenterContainer;
break;
case UIWindowContainerType.RighTop:
transParentWindowContainer = UISceneManager.Instance.currentUISceneCtr.rightTopContainer;
break;
}
windowUI.transform.parent = transParentWindowContainer;
windowUI.transform.localPosition = Vector3.zero;
windowUI.transform.localScale = Vector3.one;
m_Dic_UIWindow.Add (windowType,windowUI);
UIWindowShowAnimationType windowShowType = windowUICtr.windowShowType;
StartActiveUIWindow (windowUI,windowShowType,true);
}
//closeUIwindow关闭窗口UI
public void CloseUIWindow(UIWindowType windowType){
if(m_Dic_UIWindow.ContainsKey(windowType)){
GameObject windowUI = m_Dic_UIWindow [windowType];
UIWindowCtrBase windowUICtr = windowUI.GetOrAddComponent<UIWindowCtrBase> ();
UIWindowShowAnimationType windowShowType = windowUICtr.windowShowType;
StartActiveUIWindow (windowUI,windowShowType,false);
}
}
//窗口关闭和显示动画
public void StartActiveUIWindow (GameObject windowUI,UIWindowShowAnimationType windowShowType=UIWindowShowAnimationType.CenterToBig,bool state=true)
{
switch(windowShowType){
case UIWindowShowAnimationType.Normal:
ShowNomal(windowUI,state);
break;
case UIWindowShowAnimationType.CenterToBig:
ShowCenterToBig(windowUI,state);
break;
case UIWindowShowAnimationType.LeftToRight:
ShowDirect(windowUI,state,1);
break;
case UIWindowShowAnimationType.RightToLeft:
ShowDirect(windowUI,state,2);
break;
case UIWindowShowAnimationType.TopToBottom:
ShowDirect(windowUI,state,3);
break;
case UIWindowShowAnimationType.BottomToTop:
ShowDirect(windowUI,state,4);
break;
default:
break;
}
}
public void ShowCenterToBig(GameObject windowUI,bool state){
TweenScale ts = windowUI.GetOrAddComponent<TweenScale> ();
ts.from = Vector3.zero;
ts.to = Vector3.one;
UIWindowCtrBase windowUICtr = windowUI.GetOrAddComponent<UIWindowCtrBase> ();
ts.duration = windowUICtr.duration;
ts.animationCurve = windowUICtr.windowUIShowAnimationCurve;
if (state) {
NGUITools.SetActive (windowUI, true);
} else {
ts.SetOnFinished (()=>{
DestroyUIWindow(windowUI);
});
ts.Play (false);
}
}
public void ShowNomal(GameObject windowUI,bool state){
if (state) {
NGUITools.SetActive (windowUI, true);
} else {
DestroyUIWindow (windowUI);
}
}
//从四个方向中某一个方向往中间移动显示/关闭
//1:从左到右
//2:从右到左
//3:从上到下
//4:从下到上
public void ShowDirect(GameObject windowUI,bool state,int direct){
Vector3 from = Vector3.zero;
Vector3 to = Vector3.zero;
switch(direct){
case 1:
from = Vector3.left * Screen.width;
break;
case 2:
from = Vector3.right * Screen.width;
break;
case 3:
from = Vector3.up * Screen.height;
break;
case 4:
from = Vector3.down * Screen.height;
break;
default:
Debug.Log ("no have ,please use 1/2/3/4");
break;
}
TweenPosition tp = windowUI.GetOrAddComponent<TweenPosition> ();
tp.from = from;
tp.to = to;
UIWindowCtrBase windowUICtr = windowUI.GetOrAddComponent<UIWindowCtrBase> ();
tp.duration = windowUICtr.duration;
tp.animationCurve = windowUICtr.windowUIShowAnimationCurve;
if (state) {
NGUITools.SetActive (windowUI, true);
} else {
tp.SetOnFinished (()=>{
DestroyUIWindow(windowUI);
});
tp.Play (false);
}
}
//删除窗口
private void DestroyUIWindow(GameObject windowUI){
UIWindowCtrBase windowUICtr = windowUI.GetOrAddComponent<UIWindowCtrBase> ();
if(m_Dic_UIWindow.ContainsKey(windowUICtr.windowType)){
m_Dic_UIWindow.Remove (windowUICtr.windowType);
GameObject.Destroy (windowUI);
}
}
}
UIWindowCtrBase :挂载窗口预设体上,并在预设体上进行一些配置
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UIWindowCtrBase : MonoBehaviour {
//窗口UI类型数组,存储的是即将打开的窗口
public List<UIWindowType> windowTypeList = new List<UIWindowType> ();
//窗口UI的挂点类型,默认是居中
public UIWindowContainerType windowConType = UIWindowContainerType.Center;
//显示/关闭窗口UI的动画,,默认是正常显示/关闭
public UIWindowShowAnimationType windowShowType = UIWindowShowAnimationType.Normal;
//显示/关闭动画的持续时间
public float duration = 1f;
//动画播放的曲线
public AnimationCurve windowUIShowAnimationCurve = new AnimationCurve ();
//窗口UI的类型,默认是登入窗口
[HideInInspector]
public UIWindowType windowType = UIWindowType.Login;
void Start(){
//GetComponentsInChildren获取所有子对象的UIButton脚本
UIButton[] btnArray = transform.GetComponentsInChildren<UIButton> ();
foreach (UIButton btn in btnArray) {
UIEventListener.Get (btn.gameObject).onClick = ButtonClick;
}
OnStart ();
}
void ButtonClick(GameObject btn){
OnButtonClick (btn);
}
void Update(){
OnUpdate ();
}
public virtual void OnStart(){
}
public virtual void OnButtonClick(GameObject btn){}
public virtual void OnUpdate(){}
}
ResourceManager :获取预设体并Instantiate和返回生成的对象
using UnityEngine;
using System.Collections;
using System.Text;
public class ResourceManager : SingleTon<ResourceManager> {
public GameObject Load(string ResName,ResourceType resType){
StringBuilder src = new StringBuilder ();
switch(resType){//根据相应的类型获取相应的路径
case ResourceType.UIScene:
src.Append ("UI/UIScene/");
break;
case ResourceType.UIWindow:
src.Append ("UI/UIWindow/");
break;
default:
break;
}
src.Append (ResName);
GameObject uiRes = Resources.Load<GameObject> (src.ToString());
return GameObject.Instantiate(uiRes);
}
}
GameTools:工具类
using UnityEngine;
using System.Collections;
public static class GameTools {
public static T GetOrAddComponent<T>(this GameObject go)where T:Behaviour
{
T ts = go.GetComponent<T> ();
if(ts==null){
ts=go.AddComponent<T> ();
}
return ts;
}
}
SingleTon<T> :单列包装
using UnityEngine;
using System.Collections;
public class SingleTon<T> where T :new (){
private static T instance;
public static T Instance{
get{
if(instance==null){
instance=new T();
}
return instance;
}
}
}