当前位置: 首页 > news >正文

企业网站建设相关书籍企业网站快速建站

企业网站建设相关书籍,企业网站快速建站,做 爱 网站视频教程,bootstrap 做企业网站一、简介 最近在各大网站看了一下 Unity3d 的 UI 框架,各种 UI 框架已经有很多的版本了,各有千秋,有的功能虽然写的完善,但用起来太复杂,有的框架功能不完善,搞个课程就上架了,还有什么 MVC 框…

一、简介

最近在各大网站看了一下 Unity3d 的 UI 框架,各种 UI 框架已经有很多的版本了,各有千秋,有的功能虽然写的完善,但用起来太复杂,有的框架功能不完善,搞个课程就上架了,还有什么 MVC 框架,绕来绕去的看的头都大了,这些根本不想用。

于是我自己就写了一个 UI 框架,只有两个脚本,不用向 UI 预制体上挂载脚本,所有的组件访问都可以通过 UIManager 来控制,常用的几个方法:显示界面,关闭界面,查找子物体,就这么多。

二、UI 框架

下面的两个脚本是 UI 框架的核心部分,具体的用法在下面的章节有介绍

UIBase

using UnityEngine;public class UIBase
{#region 字段/// <summary>/// Prefabs路径/// </summary>public string PrefabsPath { get; set; }/// <summary>/// UI面板的名字/// </summary>public string UIName { get; set; }/// <summary>/// 当前UI所在的场景名/// </summary>public string SceneName { get; set; }/// <summary>/// Type 的全名/// </summary>public string FullName { get; set; }/// <summary>/// 当前UI的游戏物体/// </summary>public GameObject UIGameObject { get; set; }#endregion/// <summary>/// 面板实例化时执行一次/// </summary>public virtual void Start() { }/// <summary>/// 每帧执行/// </summary>public virtual void Update() { }/// <summary>/// 当前UI面板销毁之前执行一次/// </summary>public virtual void Destroy() { }/// <summary>/// 根据名称查找一个子对象/// </summary>/// <param name="name"></param>/// <returns></returns>public GameObject GetObject(string name){Transform[] trans = UIGameObject.GetComponentsInChildren<Transform>();foreach (var item in trans){if (item.name == name)return item.gameObject;}Debug.LogError(string.Format("找不到名为 {0} 的子对象", name));return null;}/// <summary>/// 根据名称获取一个子对象的组件/// </summary>/// <typeparam name="T"></typeparam>/// <param name="name"></param>/// <returns></returns>public T GetOrAddCommonent<T>(string name) where T : Component{GameObject child = GetObject(name);if (child){if (child.GetComponent<T>() == null)child.AddComponent<T>();return child.GetComponent<T>();}return null;}protected UIBase() { }
}

UIManager

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class UIManager : MonoBehaviour
{public static UIManager Instance;//存储场景中的UI信息private Dictionary<string, UIBase> UIDic = new Dictionary<string, UIBase>();//当前场景的 Canvas 游戏物体private Transform CanvasTransform = null;//当前字典中UI的个数public int UICount{get { return UIDic.Count; }}private void Awake(){Instance = this;}private void Start(){}private void Update(){if (UIDic.Count > 0){foreach (var key in UIDic.Keys){if (UIDic[key] != null)UIDic[key].Update();}}}/// <summary>/// 显示面板/// </summary>/// <typeparam name="T"></typeparam>/// <returns></returns>public UIBase ShowUI<T>() where T : UIBase{Type t = typeof(T);string fullName = t.FullName;if (UIDic.ContainsKey(fullName)){Debug.Log("当前面板已经显示了,名字:" + fullName);return UIDic[fullName];}GameObject canvasObj = GameObject.Find("Canvas");if (canvasObj == null){Debug.LogError("场景中没有Canvas组件,无法显示UI物体");return null;}CanvasTransform = canvasObj.transform;UIBase uiBase = Activator.CreateInstance(t) as UIBase;if (string.IsNullOrEmpty(uiBase.PrefabsPath)){Debug.LogError("Prefabs 路径不能为空");return null;}GameObject prefabs = Resources.Load<GameObject>(uiBase.PrefabsPath);GameObject uiGameOjbect = GameObject.Instantiate(prefabs, CanvasTransform);uiGameOjbect.name = uiBase.PrefabsPath.Substring(uiBase.PrefabsPath.LastIndexOf('/') + 1);uiBase.UIName = uiGameOjbect.name;uiBase.SceneName = SceneManager.GetActiveScene().name;uiBase.UIGameObject = uiGameOjbect;uiBase.FullName = fullName;uiBase.Start();UIDic.Add(fullName, uiBase);return uiBase;}/// <summary>/// 移除面板/// </summary>/// <typeparam name="T"></typeparam>public void RemoveUI<T>(){Type t = typeof(T);string fullName = t.FullName;if (UIDic.ContainsKey(fullName)){UIBase uIBase = UIDic[fullName];uIBase.Destroy();GameObject.Destroy(uIBase.UIGameObject);UIDic.Remove(fullName);return;}Debug.Log(string.Format("当前的UI物体未实例化,名字:{0}", fullName));}/// <summary>/// 清除所有的UI物体/// </summary>public void ClearAllPanel(){foreach (var key in UIDic.Keys){UIBase uIBase = UIDic[key];if (uIBase != null){uIBase.Destroy();GameObject.Destroy(uIBase.UIGameObject);}}UIDic.Clear();}/// <summary>/// 找到指定的UI面板/// </summary>/// <typeparam name="T"></typeparam>/// <param name="name"></param>/// <returns></returns>public GameObject GetGameObject<T>(string name){Type t = typeof(T);string fullName = t.FullName;UIBase uIBase = null;if (!UIDic.TryGetValue(fullName, out uIBase)){Debug.Log("没有找到对应的UI面板,名字:" + fullName);return null;}return uIBase.GetObject(name);}private UIManager() { }
}

三、UI 框架的用法

我用了两个 场景来测试框架,start 和 main 场景

start 场景如下:

   

在 Start 场景 GameRoot 上挂上 StartSceneRoot 脚本

这个脚本就调用框架在场景中显示一个UI

using UnityEngine;public class StartSceneRoot : MonoBehaviour {	void Start () {UIManager.Instance.ShowUI<Panel_MainUI>();}
}

在 GameManager 游戏物体上有两个脚本,这个是要跟随着场景一起跳转的。

UIManager 的代码在上一节,下面是 GameManager 代码 

using UnityEngine;
using UnityEngine.SceneManagement;public class GameManager : MonoBehaviour {public static GameManager Instance;private static bool origional = true;private void Awake(){if (origional){Instance = this as GameManager;origional = false;DontDestroyOnLoad(this.gameObject);}else{Destroy(this.gameObject);}}void Start () {SceneManager.sceneUnloaded += SceneManager_sceneUnloaded;SceneManager.sceneLoaded += SceneManager_sceneLoaded;}private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1){//Debug.Log("场景加载了,场景名:" + arg0.name);}private void SceneManager_sceneUnloaded(Scene arg0){//Debug.Log("场景卸载了,场景名:" + arg0.name);//注意:切换场景要清除掉 UIManager 中保存的 UI 数据UIManager.Instance.ClearAllPanel();}public void LoadScene(string sceneName){if (SceneManager.GetActiveScene().name != sceneName){SceneManager.LoadScene(sceneName);}}private GameManager() { }
}

在 start 场景中挂载到游戏物体上的脚本就这三个:StartSceneRoot,GameManager,UIManager

下面就准备要显示的 UI 了,在这里,我做了三个 UI 界面,并做成预制体

界面 Panel_MainUI

界面 Panel_Setting

界面 Panel_Affiche

这三个预制体对应的也是三个脚本,但不用挂在游戏物体上,作用是UI的逻辑部分。

Panel_MainUI

using UnityEngine;
using UnityEngine.UI;public class Panel_MainUI : UIBase
{public Panel_MainUI(){PrefabsPath = "Prefabs/UI/Panel_MainUI";}public override void Start(){GetOrAddCommonent<Button>("Button_Setting").onClick.AddListener(() =>{//显示设置面板UIManager.Instance.ShowUI<Panel_Setting>();});GetOrAddCommonent<Button>("Button_Task").onClick.AddListener(() =>{//清除所有的面板//UIManager.Instance.ClearAllPanel();  //跳转到 main 场景GameManager.Instance.LoadScene("main");});GetOrAddCommonent<Button>("Button_Equipage").onClick.AddListener(() =>{Debug.Log("UIManager 中 UI 面板的个数:" + UIManager.Instance.UICount);});}public override void Update(){//Debug.Log("我是 Panel_MainUI Update 方法");}public override void Destroy(){//Debug.Log("我是 Panel_MainUI Destroy 方法");}
}

Panel_Setting

using UnityEngine;
using UnityEngine.UI;public class Panel_Setting : UIBase
{public Panel_Setting(){PrefabsPath = "Prefabs/UI/Panel_Setting";}public override void Start(){//Debug.Log("我是 Panel_Setting Start 方法");GetOrAddCommonent<Button>("Button_Close").onClick.AddListener(() =>{//移除自己UIManager.Instance.RemoveUI<Panel_Setting>();});GetOrAddCommonent<Button>("Button_Test").onClick.AddListener(() =>{//访问其他的面板的游戏物体 GameObject obj = UIManager.Instance.GetGameObject<Panel_MainUI>("Button_Map");if (obj != null)obj.transform.Find("Text").GetComponent<Text>().text = "Map";});GetOrAddCommonent<Button>("Button_Test1").onClick.AddListener(() =>{Debug.Log("UIManager 中 UI 面板的个数:" + UIManager.Instance.UICount);});}public override void Update(){//Debug.Log("我是 Panel_Setting Update 方法");}public override void Destroy(){//Debug.Log("我是 Panel_Setting Destroy 方法");}
}

Panel_Affiche

using UnityEngine;
using UnityEngine.UI;public class Panel_Affiche : UIBase
{public Panel_Affiche(){PrefabsPath = "Prefabs/UI/Panel_Affiche";}public override void Start(){GetOrAddCommonent<Button>("Button_Close").onClick.AddListener(() =>{GameManager.Instance.LoadScene("start");});}public override void Update(){//Debug.Log("我是 Panel_Affiche Update 方法");}public override void Destroy(){//Debug.Log("我是 Panel_Affiche Destroy 方法");}
}

main 场景只挂了一个脚本

MainSceneRoot 脚本同样也是只是用来显示UI用的

using UnityEngine;public class MainSceneRoot : MonoBehaviour {void Start () {UIManager.Instance.ShowUI<Panel_Affiche>();}
}

运行后就能看到,显示了 Panel_MainUI 的UI界面,点击设置,就会打开设置的界面,点击任务按钮,就会跳转到 main 场景(这里只是测试)

跳转到 main 场景后,点击关闭按钮,又会返回到 start 场景。

演示就这些了,写这个框架我也就用了几个小时而已,当然还有待继续完善和改进的,另外,我用的 Unity版本是 Unity 5.6.7f1 ,C# 的一些高级语法用不了,不然可以写的更优雅一些。

源码:点击跳转

结束

如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言

end


文章转载自:
http://dinncoshikker.tpps.cn
http://dinncotexan.tpps.cn
http://dinncohashimite.tpps.cn
http://dinncogourmand.tpps.cn
http://dinncodomo.tpps.cn
http://dinncoexfacto.tpps.cn
http://dinncoentoutcas.tpps.cn
http://dinncofeatured.tpps.cn
http://dinncounrestricted.tpps.cn
http://dinncohallstadt.tpps.cn
http://dinncocrackled.tpps.cn
http://dinncosalem.tpps.cn
http://dinncogelderland.tpps.cn
http://dinncocaloricity.tpps.cn
http://dinncovambrace.tpps.cn
http://dinncoorderly.tpps.cn
http://dinncostratoscope.tpps.cn
http://dinncopremo.tpps.cn
http://dinncoslinky.tpps.cn
http://dinncononrepetatur.tpps.cn
http://dinncolionism.tpps.cn
http://dinncoboating.tpps.cn
http://dinncodermographia.tpps.cn
http://dinnconaissance.tpps.cn
http://dinncomicroorganism.tpps.cn
http://dinncoyangtse.tpps.cn
http://dinncopedimeter.tpps.cn
http://dinncophenetic.tpps.cn
http://dinnconeoteric.tpps.cn
http://dinncorenascence.tpps.cn
http://dinncoixodid.tpps.cn
http://dinncochiropody.tpps.cn
http://dinncohomograft.tpps.cn
http://dinncodebited.tpps.cn
http://dinncothorshavn.tpps.cn
http://dinncopreambulate.tpps.cn
http://dinncohuggable.tpps.cn
http://dinncobedsheet.tpps.cn
http://dinncorusticism.tpps.cn
http://dinncovedalia.tpps.cn
http://dinncospelican.tpps.cn
http://dinncodissociation.tpps.cn
http://dinncosnaggletooth.tpps.cn
http://dinncoderma.tpps.cn
http://dinncomadly.tpps.cn
http://dinncolightly.tpps.cn
http://dinncocarabine.tpps.cn
http://dinncogleeful.tpps.cn
http://dinncoarguably.tpps.cn
http://dinncojaspilite.tpps.cn
http://dinncoabettal.tpps.cn
http://dinncoobscurantist.tpps.cn
http://dinncoindict.tpps.cn
http://dinncoopprobrium.tpps.cn
http://dinncojubilantly.tpps.cn
http://dinncocleaver.tpps.cn
http://dinncorelaxation.tpps.cn
http://dinncochairwoman.tpps.cn
http://dinncoexpulse.tpps.cn
http://dinncooverexposure.tpps.cn
http://dinncoconviviality.tpps.cn
http://dinncomoorhen.tpps.cn
http://dinncoproviso.tpps.cn
http://dinncouninfluenced.tpps.cn
http://dinncoconductance.tpps.cn
http://dinncounmeddled.tpps.cn
http://dinncohoot.tpps.cn
http://dinncocaseous.tpps.cn
http://dinncoadmittedly.tpps.cn
http://dinncogothickry.tpps.cn
http://dinncovilli.tpps.cn
http://dinncosilicomanganese.tpps.cn
http://dinnconiggard.tpps.cn
http://dinncogrounded.tpps.cn
http://dinncojynx.tpps.cn
http://dinncopantothenate.tpps.cn
http://dinncoalgeria.tpps.cn
http://dinncooptimization.tpps.cn
http://dinncotricyclist.tpps.cn
http://dinncooxytocia.tpps.cn
http://dinncowitchetty.tpps.cn
http://dinncotruman.tpps.cn
http://dinncoratheripe.tpps.cn
http://dinncoentogastric.tpps.cn
http://dinncoisochroous.tpps.cn
http://dinncospinet.tpps.cn
http://dinncovestment.tpps.cn
http://dinncosympatholytic.tpps.cn
http://dinncocanaan.tpps.cn
http://dinncomanageress.tpps.cn
http://dinncoconvalescent.tpps.cn
http://dinncofactory.tpps.cn
http://dinncolakeward.tpps.cn
http://dinncobenefaction.tpps.cn
http://dinncoluteinization.tpps.cn
http://dinncoimplacentate.tpps.cn
http://dinncoembonpoint.tpps.cn
http://dinncotrisoctahedron.tpps.cn
http://dinncodatel.tpps.cn
http://dinncocasal.tpps.cn
http://www.dinnco.com/news/142783.html

相关文章:

  • 建设与管理委员会网站网络平台有哪些?
  • 网站没有访问量推广策略
  • 自贡网站开发业务推广网站
  • 中文设计网站sem推广是什么
  • wordpress做淘宝客网站推广公司哪家好
  • 私人pk赛车网站怎么做沈阳疫情最新消息
  • 无锡做设计公司网站成都公司建站模板
  • 安徽芜湖网站建设网页设计与制作考试试题及答案
  • 做排名的网站哪个好上海整站seo
  • 做网站一天打多少个电话百度网盘app下载安装电脑版
  • 惠州网站建设找哪个公司seo网站优化是什么
  • wordpress访客代码今日头条关键词排名优化
  • 做网站的公司现在还 赚钱吗上海比较好的seo公司
  • 诚信通开了网站谁给做美国疫情最新数据消息
  • 网站建设一条龙全包抖音怎么运营和引流
  • 全面的移动网站建设东莞搜索优化
  • 滁州市大滁城建设网站舆情优化公司
  • 杭州建站模板系统建立网站一般要多少钱
  • 免费网站添加站长统计营销网站建设免费
  • 网站建设中企动力最佳a5长春seo网站优化
  • 交钱做网站对方拿了钱不做该怎么办seo是什么姓氏
  • 做自己的直播网站网络营销策划书5000字
  • 太原市做网站怎么在百度投放广告
  • 网站建设平台多少钱微博推广方法有哪些
  • wordpress 建站 linux济南最新消息
  • 北京skp高手优化网站
  • 网站模板内容怎么添加图片手机怎么做网站
  • 虐做视频网站产品全网营销推广
  • 做网站开发学什么超级推荐的关键词怎么优化
  • 怎么做同城商务网站自己怎么做网站