Unity3D UGUI与2D游戏(项目)UI框架

1、搭建UI界面

1.1准备工作

1、调整Game视图中的分辨率800*480

2、在 Edit --Project  Setting--Editor中,将Mode改为2D模式

3、调整摄像机为正交摄像机,调整摄像机的Clear Flags 为 Soild Color ,选择自己喜欢的颜色,调整Size 为 800/480

4、创建一个Canvas

4.1、在Canvas组件里,将Render Mode 改为 Screen Space - Camera,Render Camera中拖入Main Camera

4.2、在Canvas Scaler 组件中,UIScaleMode 改为 Scaler With Screen Size,把Refernce  Ressolution 改为 与分变率一致800*480,因为是横屏的Match选择 Weight=1

1.2、搭建界面(展示一下效果图)

开始界面:

选项界面:


2、UIManager:UI界面的切换

2.1、 首先创建两个脚本 UIBase,UIManager,一个空的游戏对象Manager,UIManager挂载到Manager

UIbase:

public class UIBase : MonoBehaviour {

        public virtual void UserInterfaceEnter(){}

       public virtual void UserInterfacePause(){}

       public virtual void UserInterfaceResume(){}

       public virtual void UserInterfaceExit(){}

}

UImaneger:

第一步创建脚本的单例:

static UIManager instance;

public static UIManager Instance{

get{return instance;}

}

void Awake(){

instance = this;

//  下面两行代码是加载UI界面

LoadUIPerfabByName ("UIstart");

LoadUIPerfabByName ("UIoption");

}

第二步:UI界面的显示就好比栈的形式,使用栈来控制

Stack<UIBase>  UIStack =new Stack<UIBase>();

// 界面入栈:让界面显示出来

public void PushUserInterface(string UIName){

if (UIStack.Count>0) {

UIBase old_UI = UIStack.Peek ();

old_UI.UserInterfacePause ();

}

UIBase newUI = InstantiateUIByName (UIName);                                    //InstantiateUIByName方法:创建UI界面

UIStack.Push (newUI);

newUI.UserInterfaceEnter ();                                                                     // 通过UserInterfaceEnter这个方法显示界面

}

// 界面出栈:让界面不显示出来

public void PopUserInterface(){

if (UIStack.Count == 0)

{return;}

UIBase old_UI = UIStack.Pop ();

old_UI.UserInterfaceExit ();

if (UIStack.Count>0) {

UIBase newUI = UIStack.Peek();

newUI.UserInterfaceResume ();

          }

}

//   InstantiateUIByName方法:创建UI界面

Dictionary<string,UIBase> currentUIDict =new Dictionary<string,UIBase>();     //   存储到当前为止,所有已出现过的UI界面的字典

public UIBase InstantiateUIByName(string UIName){ 

if (currentUIDict.ContainsKey(UIName)) {

return currentUIDict [UIName];

}

GameObject objPrefab = UIobject[UIName] ;                                         //   UIobject:一个存储所有已加载界面的字典

GameObject obj = GameObject.Instantiate (objPrefab);

UIBase uibase = obj.GetComponent();

currentUIDict.Add (UIName, uibase);

obj.name = UIName;

return uibase;

}

//  创建UIobject:一个存储所有已加载界面的字典

public   string perfabDir ="Perfab";

Dictionary<string,GameObject>UIobject =new Dictionary<string,GameObject>();

void LoadUIPerfabByName(string UIName){

if (UIobject.ContainsKey(UIName))

 {return;}

string path =perfabDir+"/"+UIName;

GameObject UIobj = Resources.Load(path);

if (UIobj!=null) {

UIobject.Add (UIName, UIobj);}

}

2.2、创建UIOption、UIStart脚本,分别挂载到Option和Start UI界面

UIStart:

public override void UserInterfaceEnter (){

GetComponent().worldCamera = Camera.main;

gameObject.SetActive (true);}

public override void UserInterfacePause ()

{gameObject.SetActive (false);}

public override void UserInterfaceResume ()

{gameObject.SetActive (true);}

public override void UserInterfaceExit ()

{gameObject.SetActive (false);}

//UIStart界面     选项Button添加监听事件

public void GoToOptionUI(){

UIManager.Instance.PushUserInterface ("UIoption");

}

UIOption:

public override void UserInterfaceEnter (){

GetComponent().worldCamera = Camera.main;

gameObject.SetActive (true);}

public override void UserInterfacePause ()

{gameObject.SetActive (false);}

public override void UserInterfaceResume ()

{gameObject.SetActive (true);}

public override void UserInterfaceExit ()

{gameObject.SetActive (false);}

//  Option界面:后退Button添加监听事件

public void Back(){

UIManager.Instance.PopUserInterface ();

}

到这一步,就完成了两个UI界面的切换

推荐阅读更多精彩内容