现在开始最乱的代码显示登陆窗口,按钮跳转注册窗口,
搭这个框架,大大小小写了12个脚本,咱们前两篇写了7个,还有5个,其中包含一个存取名称密码的记录数据的脚本(dataMgr)
using UnityEngine;
using System.Collections;
/// <summary>
/// 登陆场景UI控制器
/// </summary>
public class UILoginSceneCtrl : UISceneCtrBase {
protected override void OnStart(){
base.OnStart ();
StartCoroutine (OpenWindowUI());
//打开窗口
//OpenWindowUI ();
}
protected override void BeforeOnDestroy(){
base.BeforeOnDestroy ();
}
IEnumerator OpenWindowUI(){
yield return new WaitForSeconds (2.0f);
GameObject windowUI = UIWindowsMgr.GetInstance ().OpenWindowUI (WindowUIType.Login);
}
}
它继承的UISceneCtrBase在前面说过了,是一个基类
这个脚本用来打开窗口的,协程里有一个延迟两秒,运行后,等两秒,登录窗口会蹦出来,我还设置了动画,这个最后说
UILoginSceneCtrl中涉及到了UIWindowsMgr
UIWindowsMgr.的核心代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 窗口UI管理器
/// </summary>
public class UIWindowsMgr :SingleTo<UIWindowsMgr>{
public Dictionary<WindowUIType,GameObject> windowDic=new Dictionary<WindowUIType, GameObject>();//这是字典
public GameObject OpenWindowUI(WindowUIType windowUIType = WindowUIType.NONE){
//如果对应的窗口类型已经在字典中就不要重复打开
if(windowDic.ContainsKey(windowUIType)){
return windowDic[windowUIType];
}//单例模式
string resName = string.Empty;
switch(windowUIType){
case WindowUIType.NONE:
break;
case WindowUIType.Login:
resName = "Login_Window";
break;
case WindowUIType.Register:
resName = "Register_Window";
break;
case WindowUIType.Choose:
resName = "Root_choose";//这是我添加的选择英雄界面,因为这时候我还没有弄主界面,所以没有,不过随着完善,应该有主界面
break;
default:
break;
}
GameObject windowUI = ResourcesMgr.GetInstance().LoadRes (resName, resType: ResourcesMgr.ResourceType.UIWindow);
windowDic.Add (windowUIType,windowUI);//实例化的窗口需要放入字典中缓存起来
UIWindowCtrlBase windowCtrlBase = windowUI.GetComponent<UIWindowCtrlBase> ();//获取窗口UI控制器组件
windowCtrlBase.currentWindowUIType = windowUIType;//设置当前窗口的窗口类型
WindowUIContainerType windowUIContainerType = windowCtrlBase.windowUIContainerType;//从窗口UI控制器组件中获取窗口的挂点类型
Transform parent = null;
switch(windowUIContainerType){
case WindowUIContainerType.NONE:
break;
case WindowUIContainerType.Center:
parent = UISceneMgr.GetInstance().currentSceneUICtrBase.center_container;
break;
case WindowUIContainerType.LeftTop:
break;
case WindowUIContainerType.LeftButton:
break;
case WindowUIContainerType.RightTop:
break;
case WindowUIContainerType.RightButton:
break;
default:
break;
}
windowUI.SetParent (parent);
windowUI.ResetPositionAndScale();
NGUITools.SetActive (windowUI,false);//隐藏窗口
StartShowWindowUI (windowCtrlBase);
return windowUI;
}
public void CloseWindowUI(WindowUIType windowUIType,bool delete = true){//关闭窗口
if(windowDic.ContainsKey(windowUIType)){
if(delete){
GameObject.Destroy(windowDic[windowUIType]);
windowDic.Remove(windowUIType);//销毁掉移除
}
}
}
private void StartShowWindowUI(UIWindowCtrlBase windowCtrlBase){//这是限制窗口显示的动画方式
//这里又涉及到了一个脚本UIWindowCtrlBase
WindowShowAnimationType animationType = windowCtrlBase.WindowShowAnimationType ;
switch(animationType){
case WindowShowAnimationType.Nomal:
ShowNormal(windowCtrlBase);
break;
case WindowShowAnimationType.CenterToBig:
ShowCenterToBig(windowCtrlBase);
break;
case WindowShowAnimationType.LeftToRight:
ShowDirection(windowCtrlBase,Vector3.left);
break;
case WindowShowAnimationType.RightToLeft:
ShowDirection(windowCtrlBase,Vector3.right);
break;
case WindowShowAnimationType.TopToBottom:
ShowDirection(windowCtrlBase,Vector3.down);
break;
case WindowShowAnimationType.BottomToTop:
ShowDirection(windowCtrlBase,Vector3.up);
break;
default:
break;
}
}
/// <summary>
/// 正常显示
/// </summary>
/// <param name="windowCtrlBase">Window ctrl base.</param>
private void ShowNormal(UIWindowCtrlBase windowCtrlBase){
GameObject windowUI = windowCtrlBase.gameObject;
NGUITools.SetActive (windowUI,true);
}
/// <summary>
/// 从中间向四周放大显示
/// </summary>
/// <param name="windowCtrlBase">窗口UI控制器</param>
private void ShowCenterToBig(UIWindowCtrlBase windowCtrlBase){
GameObject windowUI = windowCtrlBase.gameObject;
TweenScale ts = windowUI.GetOrAddCoponent<TweenScale>();
NGUITools.SetActive (windowUI,true);
ts.duration = windowCtrlBase.duration;
ts.delay = windowCtrlBase.delay;
ts.animationCurve = windowCtrlBase.animationCurve;
ts.from = Vector3.zero;
ts.to = Vector3.one;
}
private void ShowDirection(UIWindowCtrlBase windowCtrlBase,Vector3 direction){
GameObject windowUI = windowCtrlBase.gameObject;
NGUITools.SetActive (windowUI,true);
TweenPosition tp = windowUI.GetOrAddCoponent<TweenPosition> ();
tp.duration = windowCtrlBase.duration;
tp.delay = windowCtrlBase.delay;
tp.animationCurve = windowCtrlBase.animationCurve;
//从左到右
tp.from = direction*300;
tp.to = Vector3.zero;
}
}
选择动画的时候涉及的脚本
核心代码:
using UnityEngine;
using System.Collections;
/// <summary>
/// 窗口UI控制基类
/// </summary>
public class UIWindowCtrlBase : MonoBehaviour {
public WindowUIContainerType windowUIContainerType = WindowUIContainerType.NONE;//窗口UI的挂点类型
public WindowShowAnimationType WindowShowAnimationType = WindowShowAnimationType.Nomal;//窗口UI显示时播放的类型
[Header("窗口显示的动画模块")]
//动画播放时间
public float duration = 0.5f;
//动画播放延迟时间
public float delay = 0f;
[HideInInspector]//在检视面板中隐藏
public WindowUIType currentWindowUIType = WindowUIType.NONE;//当前窗口UI类型
//动画曲线
public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
// Use this for initialization
void Start () {
OnStart ();
}
void OnDestroy(){
BeforeOnDestroy ();
}
protected virtual void OnStart(){}
protected virtual void BeforeOnDestroy(){
if (UIWindowsMgr.GetInstance ().windowDic.ContainsKey (currentWindowUIType))
UIWindowsMgr.GetInstance ().windowDic.Remove (currentWindowUIType);
}
}
到这里显示窗口的完了,剩下的即使点注册,切换窗口,还有记录数据
using UnityEngine;
using System.Collections;
/// <summary>
/// 数据管理器
/// </summary>
public class datamgr : SingleTo<datamgr> {
public const string ACCOUNT_KEY = "ACCOUNT_KEY";//账号对应的key
public const string PASSWPRLD_KEY = "PASSWPRLD_KEY";//密码对应的key
//用户类
public class User
{
public string Account {
set;
get;
}
public string passworld {
set;
get;
}
public User(string account,string passworld){
this.Account = account;
this.passworld = passworld;
}
}
public void RegisterUser(User aUser){
string inputAccount = aUser.Account;
string inputPassworld = aUser.passworld;
PlayerPrefs.SetString (ACCOUNT_KEY,inputAccount);
PlayerPrefs.SetString (PASSWPRLD_KEY,inputPassworld);
}
public bool ExcuteUser(User aUser){
//先获取已经存在的用户
string account = PlayerPrefs.GetString (ACCOUNT_KEY);
string password = PlayerPrefs.GetString (PASSWPRLD_KEY);
//比对该用的密码
string inputAccount = aUser.Account;
string inputPassworld = aUser.passworld;
if(!account.Equals(inputAccount)||!password.Equals(inputPassworld)){
return false;
}
return true;
}
}
using UnityEngine;
using System.Collections;
/// <summary>
/// 登录窗口UI控制器
/// </summary>
public class UILoginWidowCtrl : UIWindowCtrlBase {
[SerializeField]
private UIInput account;
[SerializeField]
private UIInput passworld;
protected override void OnStart(){
base.OnStart ();//调用基类的虚方法
BindClick ();//添加点击事件
}
//绑定点击回调函数
void BindClick(){
//UIButton[] btnArr = GameObject.FindObjectsOfType<UIButton> ();//范围是整个游戏
UIButton[] btnArr = transform.GetComponentsInChildren<UIButton>();//限制在本窗口
for(int i = 0;i<btnArr.Length;i++){
UIEventListener.Get(btnArr[i].gameObject).onClick = Click;
}
}
//点击的回调函数
void Click(GameObject btnObj){Debug.Log (btnObj.name);
switch(btnObj.name){
case "Register_Button":
UIWindowsMgr.GetInstance().CloseWindowUI(currentWindowUIType);
UIWindowsMgr.GetInstance().OpenWindowUI(WindowUIType.Register);
break;
case "Login_Button ":
Excute();
break;
default:
break;
}
}
void Excute(){
if(string.IsNullOrEmpty(account.value)){
Debug.Log("Please Input Account");
return;
}if(string.IsNullOrEmpty(passworld.value)){
Debug.Log("Please Input Passworld");
return;
}
datamgr.User aUser = new datamgr.User (account.value,passworld.value);
bool state = datamgr.GetInstance ().ExcuteUser (aUser);
if (state) {
Debug.Log ("Success!");
} else {
Debug.Log("Failure!");
}
}
protected override void BeforeOnDestroy(){
base.BeforeOnDestroy ();
}
}
using UnityEngine;
using System.Collections;
public class UIRegisterWindowCtrl : UIWindowCtrlBase {
[SerializeField]
private UIInput account;
[SerializeField]
private UIInput passworld;
protected override void OnStart(){
base.OnStart ();//调用基类的虚方法
BindClick ();
}
//绑定点击回调函数
void BindClick(){
UIButton[] btnArr = transform.GetComponentsInChildren<UIButton>();//限制在本窗口
for(int i = 0;i<btnArr.Length;i++){
UIEventListener.Get(btnArr[i].gameObject).onClick = Click;
}
}
//点击的回调函数
void Click(GameObject btnObj){
switch(btnObj.name){
case "Register_Button":
RegisterUser();
break;
case "canle_Button":
UIWindowsMgr.GetInstance().CloseWindowUI(currentWindowUIType);
UIWindowsMgr.GetInstance().OpenWindowUI(WindowUIType.Login);
break;
default:
break;
}
Debug.Log (btnObj.name);
}
void RegisterUser(){
datamgr.User aUser = new datamgr.User(account.value,passworld.value);
Debug.Log (account.value +passworld.value);
datamgr.GetInstance ().RegisterUser (aUser);
}
protected override void BeforeOnDestroy(){
base.BeforeOnDestroy ();
}
}