using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
UnityEngine;
///
/// UI 服务类
///
public
class
UIService : Managerstring
,=
""
uiservice.ui=
""
>,IDisposable
{
public
void
Dispose()
{
DestroyAll();
}
///
/// 创建UI
///
///
///
///
private
T _CreateUI(
string
name) where T : UI
{
T ui = Activator.CreateInstance(
typeof
(T), name)
as
T;
return
ui;
}
public
class
UIHolder : MonoBehaviour
{
public
UI ui {
get
;
set
; }
}
public
class
UI : IDisposable
{
public
UI(
string
name)
{
_Init(name);
}
public
enum
UIStyle
{
Normal,
HideByTapScene,
}
UIStyle mStyle = UIStyle.Normal;
public
UIStyle Style
{
get
{
return
mStyle; }
set
{ mStyle = value; }
}
internal
GameObject mPrefab =
null
;
protected
UIPanel mPanel =
null
;
#region 面板动画处理
protected
UITweener[] mTweens =
null
;
protected
UITweener mMainTween =
null
;
protected
UITweener.ToggleStyle mMainToggleStyle = UITweener.ToggleStyle.normal;
private
void
_TweensInit()
{
mTweens = mPrefab.GetComponentsInChildren();
if
(mTweens !=
null
)
{
UITweener tween;
for
(
int
i = 0; i < mTweens.Length; ++i)
{
tween = mTweens[i];
if
(tween.toggleStyle == UITweener.ToggleStyle.OnShow)
{
mMainTween = tween;
mMainToggleStyle = UITweener.ToggleStyle.OnShow;
}
if
(tween.toggleStyle == UITweener.ToggleStyle.OnShowAndHide)
{
mMainTween = tween;
mMainToggleStyle = UITweener.ToggleStyle.OnShowAndHide;
break
;
}
}
}
}
private
bool
_HasTween()
{
return
mMainTween !=
null
&& mMainTween.toggleStyle != UITweener.ToggleStyle.normal;
}
private
void
_TweenOnShow(EventDelegate.Callback call)
{
mMainTween.SetOnFinished(call);
UITweener tween;
for
(
int
i = 0; i < mTweens.Length; ++i)
{
tween = mTweens[i];
if
(tween.toggleStyle != UITweener.ToggleStyle.normal)
{
tween.ResetToStart(
true
);
tween.PlayForward();
}
}
}
private
void
_TweenOnHide(EventDelegate.Callback call)
{
if
(mMainToggleStyle == UITweener.ToggleStyle.OnShow)
{
call();
return
;
}
mMainTween.SetOnFinished(call);
UITweener tween;
for
(
int
i = 0; i < mTweens.Length; ++i)
{
tween = mTweens[i];
if
(tween.toggleStyle == UITweener.ToggleStyle.OnShowAndHide)
{
tween.ResetToStart(!
true
);
tween.PlayReverse();
}
}
}
private
void
_Dummy()
{
}
private
void
_Hide()
{
mPrefab.SetActive(
false
);
mHiding =
false
;
}
#endregion
internal
void
_Init(
string
name)
{
try
{
GameObject prefab = Resource.LoadUI(name);
mPrefab = GameObject.Instantiate(prefab)
as
GameObject;
UIHolder uiholder = UtilGameObject.GetOrAddComponent(mPrefab);
uiholder.ui =
this
;
mPrefab.name = name;
mPanel = mPrefab.GetComponent();
_TweensInit();
}
catch
(Exception e)
{
Looper.LogException(e);
}
UIService.Instance.InitUI(mPrefab);
UIService.Instance._OnShowUI(
this
,
true
);
}
#region <默认属性>
///
/// 深度信息
///
public
int
depth
{
get
{
if
(mPanel==
null
)
{
return
-1;
}
return
mPanel.depth;
}
set
{
if
(mPanel ==
null
)
return
;
mPanel.depth = value;
}
}
///
/// 是否正在显示
///
///
public
bool
IsShowing()
{
if
(mPrefab ==
null
)
return
false
;
return
mPrefab.activeSelf;
}
float
mLastShowTime = Time.time;
///
/// 最后一次显示的时间
///
public
float
LastShowTime
{
get
{
return
mLastShowTime; }
set
{ mLastShowTime = value; }
}
#endregion
///
/// 名字(和预制件名称一样)
///
public
string
Name
{
get
{
return
mPrefab !=
null
? mPrefab.name :
""
; }
}
///
/// 销毁对象
///
public
void
Dispose()
{
UIService.Instance._OnHidUI(
this
);
try
{
OnClose();
if
(mPrefab !=
null
)
{
GameObject.DestroyImmediate(mPrefab);
mPrefab =
null
;
}
}
catch
(Exception e)
{
Looper.LogException(e);
}
}
///
/// 刷新界面
/// 1 Grid 重排问题
///
public
void
Refresh()
{
UIGrid[] grids = mPrefab.GetComponentsInChildren();
foreach
(UIGrid grid
in
grids)
{
if
(grid !=
null
&& !grid.animateSmoothly)
{
grid.Reposition();
}
}
}
public
IEnumerator _Refresh()
{
{
UIGrid[] grids = mPrefab.GetComponentsInChildren();
foreach
(UIGrid grid
in
grids)
{
if
(grid !=
null
&& !grid.enabled)
{
grid.repositionNow =
true
;
grid.Reposition();
}
}
}
yield
return
null
;
}
bool
mHiding =
false
;
///
/// 是否显示
///
public
void
Show(
bool
v)
{
if
(v)
{
if
(mHiding)
{
_Hide();
}
UIService.Instance._OnShowUI(
this
);
}
else
{
UIService.Instance._OnHidUI(
this
);
}
try
{
if
(mPrefab !=
null
)
{
if
(_HasTween())
{
if
(v)
{
mPrefab.SetActive(
true
);
_TweenOnShow(_Dummy);
}
else
{
mHiding =
true
;
_TweenOnHide(_Hide);
}
}
else
{
mPrefab.SetActive(v);
}
OnShow(v);
if
(v)
{
LastShowTime = Time.time;
Refresh();
}
}
}
catch
(Exception e)
{
Looper.LogException(e);
}
}
///
/// 跟随场景中物体
///
///
public
void
ApplyHub(Transform target)
{
NGUIExt guiExt = PluginManager.Instance.Get();
if
(guiExt !=
null
)
{
guiExt.ApplyHud(mPrefab, target);
}
}
public
virtual
void
OnCreate() { }
public
virtual
void
OnShow(
bool
v) { }
public
virtual
void
OnClose() { }
///
/// 点击事件
///
/// 被点击的控件
public
virtual
void
OnClick(GameObject obj) { }
///
/// 双击事件
///
/// 被双击的控件
public
virtual
void
OnDoubleClick(GameObject obj) { }
///
/// 按住事件
///
/// 被Pressed的控件
///
public
virtual
void
OnPress(GameObject obj,
bool
isPressed) { }
///
/// 拖动事件
///
/// 拖动的控件
///
public
virtual
void
OnDrag(GameObject obj, Vector2 delta) { }
///
/// 拖放事件
///
/// 拖放的当前控件
/// 一直被拖住的控件
public
virtual
void
OnDrap(GameObject obj, GameObject objSelected) { }
///
/// 显示Tooltip事件
///
///
/// 显示或隐藏Tooltip
public
virtual
void
OnTooltip(GameObject obj,
bool
isShow) { }
///
/// 被选中事件
///
/// 被选中的控件
/// 选中或取消被选中
public
virtual
void
OnSelect(GameObject obj,
bool
isSelected) { }
///
/// 光标划过事件
///
///
/// 光标进入或光标离开
public
virtual
void
OnHover(GameObject obj,
bool
isHover) { }
///
/// 根据类型获取对应名称控件;
///
///
///
///
protected
T GetChild(
string
name,GameObject obj =
null
,
int
index = 0) where T : MonoBehaviour
{
if
(obj ==
null
)
{
obj = mPrefab;
}
Transform child = obj.transform.Find(name);
if
(child ==
null
)
{
T[] childs = obj.GetComponentsInChildren();
foreach
(T t
in
childs)
{
if
(t.gameObject.name == name)
{
return
t;
}
}
}
else
{
if
(child.childCount == 0)
{
return
child.GetComponent();
}
{
int
count = 0;
T[] comps = child.GetComponents();
foreach
(T t
in
comps)
{
if
(t.gameObject.name == name && count == index)
{
return
t;
}
count++;
}
}
{
int
count = 0;
T[] childs = child.GetComponentsInChildren();
foreach
(T t
in
childs)
{
if
(t.gameObject.name == name && count == index)
{
return
t;
}
count++;
}
}
}
Debug.LogError(obj.name +
" hasn't Components :"
+
typeof
(T).Name +
" in children named:"
+ name);
return
null
;
}
protected
GameObject FindChild(
string
name)
{
var child = mPrefab.transform.FindChild(name);
return
child.gameObject;
}
protected
T FindChild(
string
name) where T : Component
{
var child = mPrefab.transform.FindChild(name);
T cmp = child.GetComponent();
return
cmp;
}
///
/// 获取对应名称子控件;
///
///
///
protected
GameObject GetChild(
string
name, GameObject obj =
null
)
{
if
(obj ==
null
)
{
obj = mPrefab;
}
Transform child = obj.transform.Find(name);
if
(child ==
null
)
{
Debug.LogError(obj.name +
"is not find child of:"
+ name);
return
null
;
}
return
child.gameObject;
}
///
/// 用指定的对象替换子对象
///
/// 子对象名字
/// 指定的对象
///
protected
bool
ReplaceChild(
string
name, GameObject obj)
{
GameObject orginal = GetChild(name);
if
(orginal ==
null
) {
GameObject.DestroyImmediate (obj);
return
false
;
}
obj.transform.parent = orginal.transform.parent;
obj.transform.localPosition = Vector3.zero;
obj.transform.localRotation = Quaternion.identity;
obj.transform.localScale = Vector3.one;
obj.name = orginal.name;
orginal.transform.parent =
null
;
GameObject.DestroyImmediate (orginal);
return
true
;
}
///
/// 用指定的对象替换子对象
///
/// 原件
/// 指定的对象
///
protected
bool
ReplaceChild(GameObject orginal, GameObject obj)
{
if
(orginal ==
null
) {
GameObject.DestroyImmediate (obj);
return
false
;
}
obj.transform.parent = orginal.transform.parent;
obj.transform.localPosition = Vector3.zero;
obj.transform.localRotation = Quaternion.identity;
obj.transform.localScale = Vector3.one;
obj.name = orginal.name;
orginal.transform.parent =
null
;
GameObject.DestroyImmediate (orginal);
return
true
;
}
}
NGUIExt mPlugin;
GameObject mBG;
UIPanel mBGPanel;
int
mBGCount = 0;
private
void
_OnShowUI(UI t,
bool
init =
false
)
{
if
(t.IsShowing())
{
if
(!init)
return
;
}
if
(t.IsShowBlackBG())
{
mBGCount++;
if
(mBGCount > 0 && mBG.activeSelf ==
false
)
{
mBG.SetActive(
true
);
}
if
(mBG.activeSelf)
{
mBGPanel.depth = t.depth-1;
}
}
}
private
void
_OnHidUI(UI t)
{
if
(!t.IsShowing())
return
;
if
(t.IsShowing() && t.IsShowBlackBG())
{
mBGCount--;
if
(mBGCount <= 0)
{
mBGCount = 0;
if
(mBG.activeSelf ==
true
)
{
mBG.SetActive(
false
);
}
}
if
(mBG.activeSelf)
{
UI ui = _GetLastShowUI(
true
, t);
if
(ui !=
null
) mBGPanel.depth = ui.depth-1;
}
}
}
public
bool
IsFingerHoverGUI()
{
if
(mPlugin ==
null
)
{
return
false
;
}
return
mPlugin.IsFingerHoverGUI;
}
public
bool
IsFingerHoverGUI3D()
{
if
(mPlugin ==
null
)
{
return
false
;
}
return
mPlugin.IsFingerHoverGUI3D();
}
public
bool
IsFingerHoverGUIWithout3D()
{
if
(mPlugin ==
null
)
{
return
false
;
}
return
mPlugin.IsFingerHoverGUIWithout3D();
}
public
bool
IsPrefab(GameObject ui_prefab)
{
return
mPlugin.IsPrefab(ui_prefab);
}
///
/// 初始化 继承与 Singleton 对象构建的时候被调用
///
protected
override
void
OnCreate()
{
mPlugin = PluginManager.Instance.Get();
if
(mPlugin !=
null
)
{
mPlugin.SetEventHandler(
this
._HandlerUIEvent);
}
GameObject prefab = Resource.LoadUICommon(
"black_background"
);
Looper.Assert(prefab !=
null
,
"默认黑底 black_background Prefab不存在 !!"
);
if
(prefab !=
null
)
{
mBG = mPlugin.AddChild(prefab);
mBGPanel = mBG.GetComponent();
mBG.transform.localScale =
new
Vector3(1, 1, 1);
mBG.transform.localPosition =
new
Vector3(mBG.transform.localPosition.x, mBG.transform.localPosition.y, Mathf.Clamp(mBG.transform.localPosition.z, -2f, 2f));
if
(mBG !=
null
)
{
UISprite sprite = mBG.GetComponent();
BoxCollider collider = mBG.GetComponent();
if
(sprite!=
null
)
{
Vector2 size = mPlugin.GetSize();
sprite.SetDimensions((
int
)size.x, (
int
)size.y);
collider.size =
new
Vector3(size.x, size.y, 0);
}
}
mBG.SetActive(
false
);
}
}
public
void
InitUI(GameObject ui)
{
try
{
LayerUtils.SetLayer(ui.transform, (
int
)LayerUtils.ELayerIndex.ui);
ui.transform.parent = mPlugin.GetRoot().transform;
switch
(ui.name)
{
default
:
ui.transform.localScale =
new
Vector3(1, 1, 1);
ui.transform.localPosition = Vector3.zero;
break
;
}
}
catch
(Exception e)
{
Looper.LogException(e);
}
}
///
/// 创建UI
///
///
///
///
public
T CreateUI(
string
name) where T: UI
{
UI t =
this
.Get(name)
as
T;
if
(t ==
null
)
{
t = _CreateUI(name)
as
UI;
try
{
t.OnCreate();
}
catch
(Exception e)
{
Looper.LogException(e);
}
Put(name, t);
}
return
t
as
T;
}
List mUIScene =
new
List();
///
/// 创建一个不加入管理队列的UI,主要用于世界地图上关卡界面的创建。
///
///
///
///
public
T CreateUIWithoutManager(
string
name) where T : UI
{
T ui = _CreateUI(name);
try
{
ui.OnCreate();
}
catch
(Exception e)
{
Looper.LogException(e);
}
mUIScene.Add(ui);
return
ui;
}
///
/// 销毁不加入队列的UI
///
///
///
public
void
CloseUIWithoutManager(T ui) where T : UI
{
try
{
ui.OnClose();
ui.Dispose();
}
catch
(Exception e)
{
Looper.LogException(e);
}
mUIScene.Remove(ui);
}
///
/// 显示UI
///
///
///
public
T ShowUI(
string
name) where T : UI
{
T t = CreateUI(name);
if
(t !=
null
)
{
t.Show(
true
);
}
return
t;
}
///
/// 显示UI
///
///
///
public
T HideUI(
string
name) where T : UI
{
T t = Get(name);
if
(t !=
null
)
{
t.Show(!
true
);
}
return
t;
}
Stack mStack =
new
Stack();
///
/// 清除栈
///
public
void
StackClean()
{
mStack.Clear();
}
///
/// UI隐藏压栈
///
///
///
public
UI Push(UI t)
{
if
(t !=
null
)
{
t.Show(
false
);
mStack.Push(t);
}
return
t;
}
///
/// UI隐藏压栈
///
///
///
public
T Push(
string
name) where T: UI
{
T t = Get(name);
if
(t !=
null
)
{
t.Show(
false
);
mStack.Push(t);
}
return
t;
}
///
/// UI显示出栈
///
///
///
public
UI Pop()
{
UI t = mStack.Pop();
if
(t !=
null
)
{
t.Show(
true
);
}
return
t;
}
///
/// 销毁UI
///
///
public
void
Distroy(
string
name)
{
UI ui = Get(name);
if
(ui !=
null
)
{
ui.Dispose();
Remove(name);
}
}
public
void
Distroy(
string
name) where T : UI
{
UI ui = Get(name);
if
(ui !=
null
)
{
ui.Dispose();
Remove(name);
}
}
public
void
DestroyAll(
bool
includeScene=
false
)
{
StackClean();
Clear();
if
(includeScene)
{
foreach
(UI ui
in
mUIScene)
{
ui.Dispose();
}
mUIScene.Clear();
}
}
///
/// 隐藏其他UI
///
///
public
void
HideOtherUI(
string
name)
{
foreach
(UI ui
in
Values)
{
if
(ui.Name.Equals(name))
continue
;
ui.Show(
false
);
}
}
UI _GetLastShowUI(
bool
showBG =
false
,UI except=
null
)
{
UI lastShowUI =
null
;
float
lastShowTime = 0;
foreach
(UI ui
in
Values)
{
if
(ui == except)
continue
;
if
(ui.IsShowing() && ui.LastShowTime > lastShowTime)
{
if
(showBG)
{
if
(ui.IsShowBlackBG())
{
lastShowUI = ui;
lastShowTime = ui.LastShowTime;
}
}
else
{
lastShowUI = ui;
lastShowTime = ui.LastShowTime;
}
}
}
return
lastShowUI;
}
///
/// 隐藏最新显示的UI
///
public
void
HideLastShow()
{
UI lastShowUI = _GetLastShowUI();
if
(lastShowUI !=
null
)
{
lastShowUI.Show(
false
);
}
}
///
/// 隐藏所有UI
///
public
void
HideAllUI()
{
foreach
(UI ui
in
Values)
{
if
(ui !=
null
)ui.Show(
false
);
}
}
///
/// 获取UI跟面板
///
///
///
GameObject _GetRootPanelExt(GameObject obj)
{
Transform parent = obj.transform.parent;
UIHolder uiHolder =
null
;
while
(parent !=
null
)
{
if
(parent.gameObject.GetComponent() !=
null
)
break
;
UIHolder tempPanel = parent.GetComponent();
if
(tempPanel !=
null
)
{
uiHolder = tempPanel;
}
parent = parent.parent;
}
if
(uiHolder ==
null
)
{
return
null
;
}
return
uiHolder.gameObject;
}
///
/// NGUI事件处理器
///
/// 事件名称
/// 发送事件的控件
/// 事件参数
private
void
_HandlerUIEvent(
string
eventName, GameObject sender,
object
arg)
{
try
{
if
(sender == mBG)
{
if
(eventName.Equals(
"OnClick"
))
{
}
return
;
}
GameObject root = _GetRootPanelExt(sender);
if
(root ==
null
)
return
;
UI ui = Get(root.name);
if
(ui ==
null
)
{
UIHolder uiHolder = root.GetComponent();
if
( uiHolder!=
null
) ui = uiHolder.ui;
}
if
(ui ==
null
)
{
Debug.LogWarning(
"UI:"
+ root.name +
" not match prefab's name "
+ sender.name);
return
;
}
if
(eventName.Equals(
"OnClick"
))
{
ui.OnClick(sender);
}
else
if
(eventName.Equals(
"OnPress"
))
{
ui.OnPress(sender, (
bool
)arg);
}
else
if
(eventName.Equals(
"OnDrag"
))
{
ui.OnDrag(sender, (Vector2)arg);
}
else
if
(eventName.Equals(
"OnDrop"
))
{
ui.OnDrap(sender, (GameObject)arg);
}
else
if
(eventName.Equals(
"OnSelect"
))
{
ui.OnSelect(sender, (
bool
)arg);
}
else
if
(eventName.Equals(
"OnHover"
))
{
ui.OnHover(sender, (
bool
)arg);
}
else
if
(eventName.Equals(
"OnTooltip"
))
{
ui.OnTooltip(sender, (
bool
)arg);
}
else
if
(eventName.Equals(
"OnDoubleClick"
))
{
ui.OnDoubleClick(sender);
}
}
catch
(Exception e)
{
Looper.LogException(e);
}
}
}