笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。
CSDN视频网址:http://edu.csdn.net/lecturer/144
在快速实现UI架构设计一中已经实现了两个类,这两个类都是通用的类,它们把UI的大部分功能都实现了,接下来继续类的编写,我们使用PluginManager类中的方法,所以这个类也需要实现一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
/// <summary>
/// 插件管理器
/// </summary>
public class PluginManager : Manager<PluginManager, string, PluginManager.Plugin>
{
public class Plugin : IDisposable
{
public delegate void EventHandler(string eventName, GameObject sender, object arg);
EventHandler nEventHandler;
/// <summary>
/// 初始化
/// </summary>
public virtual void Init() {}
/// <summary>
/// 退出
/// </summary>
public virtual void Exit() {}
/// <summary>
/// 插件事件分发器
/// </summary>
/// <param name="eventName"></param>
/// <param name="sender"></param>
/// <param name="args"></param>
public void OnEvent(string eventName,GameObject sender, object args) {
if(nEventHandler != null)
{
nEventHandler(eventName,sender,args);
}
}
/// <summary>
/// 设置事件监听
/// </summary>
/// <param name="handler"></param>
public void SetEventHandler(EventHandler handler)
{
nEventHandler= handler;
}
/// <summary>
/// 销毁
/// </summary>
public void Dispose()
{
Exit();
}
}
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
this.Put("NGUI",new NGUIExt());
// this.Put("FingerGuestures", new FingerGuesturesExt());
//this.Put("TwoDExt", new TwoDExt());
foreach(Plugin plugin in Values)
{
plugin.Init();
}
}
/// <summary>
/// 退出
/// </summary>
public void Exit()
{
foreach (Plugin plugin in Values)
{
plugin.Exit();
}
}
}
该类也是继承于Manager类,设置了事件监听函数,在UIService函数中还有一个NGUIExt类的调用,下面再把该函数实现如下如下所示;
using UnityEngine;
using System.Collections;
/// <summary>
/// NGUI 插件管理器
/// </summary>
public class NGUIExt : PluginManager.Plugin
{
/// <summary>
/// 初始化
/// </summary>
public override void Init()
{
bool success = _CreateUI(true, (int)LayerUtils.ELayerIndex.ui);
if (!success) Looper.Log("_CreateRoot called more the one!!");
Looper.Log("NGUIExt Init");
CreateUI3D((int)LayerUtils.ELayerIndex.UIInScene);
}
/// <summary>
/// 退出
/// </summary>
public override void Exit()
{
Looper.Log("NGUIExt Exit");
}
/// <summary>
/// 手指是否触摸到GUI上面了
/// </summary>
public bool IsFingerHoverGUI
{
get { return (UICamera.lastHit.collider != null); }
}
public bool IsFingerHoverGUI3D()
{
return UICamera.lastHit.collider != null && UICamera.lastHit.collider.gameObject.layer == (int)LayerUtils.ELayerIndex.UIInScene;
}
public bool IsFingerHoverGUIWithout3D()
{
return UICamera.lastHit.collider != null && UICamera.lastHit.collider.gameObject.layer != (int)LayerUtils.ELayerIndex.UIInScene;
}
public bool IsPrefab(GameObject ui_prefab)
{
if (UICamera.lastHit.collider != null)
{
Transform hit = UICamera.lastHit.collider.transform;
while (hit != null)
{
if (hit.gameObject == ui_prefab)
{
return true;
}
hit = hit.parent;
}
}
return false;
}
GameObject mUICamera = null;
public Camera UICamera3D { get; private set; }
public Transform PlaceRoot { get; private set; }
public GameObject GetRoot()
{
if (mUICamera == null)
{
UIRoot root = UIRoot.list[0];
if (root == null)
{
return null;
}
UICamera camera = root.GetComponentInChildren<UICamera>();
if(camera == null)
{
}
mUICamera = camera.gameObject;
}
return mUICamera;
}
public UIRoot GetUIRoot()
{
return UIRoot.list[0];
}
/// <summary>
/// 获取NGUI的对应的屏幕宽高
/// </summary>
/// <returns></returns>
public Vector2 GetSize()
{
UIRoot root = UIRoot.list[0];
float s = (float)root.activeHeight / Screen.height;
int height = Mathf.CeilToInt(Screen.height * s);
int width = Mathf.CeilToInt(Screen.width * s);
return new Vector2(width, height);
}
/// <summary>
/// 获取当前控件的根面板(UI是以)
/// </summary>
/// <param name="obj"></param>
/// <returns>obj 所在的根面板</returns>
public GameObject GetRootPanel(GameObject obj)
{
Transform parent = obj.transform.parent;
UIPanel panel = null;
while (parent != null)
{
if (mHudPanel != null)
{
if (parent.gameObject == mHudPanel.gameObject)
break;
}
if (parent.gameObject.GetComponent<UICamera>() != null)
break;
UIPanel tempPanel = parent.GetComponent<UIPanel>();
if (tempPanel != null)
{
panel = tempPanel;
}
parent = parent.parent;
}
if (panel == null)
{
return null;
}
return panel.gameObject;
}
/// <summary>
/// 添加NGUI子对象
/// </summary>
/// <param name="prefab"></param>
/// <returns></returns>
public GameObject AddChild(GameObject prefab)
{
return NGUITools.AddChild(mUICamera.gameObject, prefab);
}
static public UIPanel AddPanel(GameObject go,string name = "panel")
{
if (go == null) return null;
int depth = UIPanel.nextUnusedDepth;
UIPanel panel = NGUITools.AddChild<UIPanel>(go);
panel.name = name;
panel.depth = depth;
return panel;
}
UIPanel mEffectPanel;
public UIPanel GetEffectPanel()
{
if (mEffectPanel == null)
{
mEffectPanel = AddPanel(GetRoot());
if (mEffectPanel != null)
{
mEffectPanel.depth = 0;
}
}
return mEffectPanel;
}
public UIPanel GetHudPanel()
{
if (mHudPanel == null)
{
mHudPanel = AddPanel(GetRoot());
if(mHudPanel != null)
{
mHudPanel.depth = -2;
}
}
return mHudPanel;
}
public void ApplyHud(GameObject panel, Transform target)
{
panel.transform.parent = GetHudPanel().transform;
UIFollowTarget uifollow = panel.gameObject.GetComponent<UIFollowTarget>();
if (uifollow == null)
{
uifollow = panel.gameObject.AddComponent<UIFollowTarget>();
}
if (uifollow != null)
{
uifollow.target = target;
uifollow.enabled = true;
uifollow.SetVisible(true);
}
}
UIPanel mHudPanel;
/// <summary>
///创建UI
/// </summary>
/// <param name="is2D"></param>
/// <param name="layer"></param>
/// <returns></returns>
private bool _CreateUI(bool is2D,int layer)
{
if (UIRoot.list.Count == 0)
{
UIPanel root = NGUITools.CreateUI(!is2D, layer);
{
UIRoot.list[0].scalingStyle = UIRoot.Scaling.FixedSizeOnMobiles;
}
//UIRoot.list[0].adjustByDPI = true;
UIRoot.list[0].maximumHeight = UIRoot.list[0].minimumHeight = UIRoot.list[0].manualHeight = 768;
Camera cam = UIRoot.list[0].gameObject.GetComponentInChildren<Camera>();//
//cam.transform.position = new Vector3(0,0,9999);
cam.clearFlags = CameraClearFlags.Depth;
cam.farClipPlane = 100f;
cam.depth = 3;
cam.GetComponent<UICamera>().eventReceiverMask = 1 << layer;
mUICamera = cam.gameObject;
mHudPanel = AddPanel(mUICamera,"panel_hub");
return true;
}
return false;
}
void CreateUI3D(int layer)
{
}
}
给读者封装这些类的目的是提供一种编写思路,接下来我们将它们应用到项目开发中。