using vnxbqy.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
///
/// 扩展函数,主要为调用泛型方式做性能优化
///
public static class ILExtension
{
#region 主项目相关
public static T GetWidgtEx(this WidgetBehavior mono, string name) where T : Component
{
var cp = mono.GetWidgt(typeof(T), name);
return (T)cp;
}
//不能在preopen中调用,会是null
public static T GetILBehaviour(this Component cp) where T : ILBehaviour
{
return cp.gameObject.GetILBehaviour();
}
public static T GetILBehaviour(this GameObject go) where T : ILBehaviour
{
var proxy = go.GetComponentEx();
if (proxy == null)
return default(T);
var type = typeof(T);
var bh = proxy.GetILBehaviour(type.FullName);
if (bh == null || !type.IsInstanceOfType(bh))
return default(T);
return (T)bh;
}
public static T GetModelEx(this ModelCenter center) where T : Model
{
var model = center.GetModel(typeof(T));
return (T)model;
}
public static T GetEx(this WindowCenter center) where T : Window
{
var win = center.Get(typeof(T).Name);
return (T)win;
}
public static void OpenFromLocalEx(this WindowCenter center) where T : Window
{
center.OpenFromLocal(typeof(T).Name);
}
public static void OpenEx(this WindowCenter center, bool forceSync = false, int functionOrder = 0) where T : Window
{
center.Open(typeof(T).Name, forceSync, functionOrder);
}
public static void OpenIL(this WindowCenter center, bool forceSync = false, int functionOrder = 0) where T : ILWindow
{
center.Open(typeof(T).Name, forceSync, functionOrder);
}
public static void CloseIL(this WindowCenter center) where T : ILWindow
{
center.Close(typeof(T).Name);
}
public static void CloseEx(this WindowCenter center) where T : Window
{
center.Close(typeof(T).Name);
}
public static bool IsOpenEx(this WindowCenter center) where T : Window
{
return center.IsOpen(typeof(T).Name);
}
#endregion
#region Unity 相关
public static T GetComponentEx(this Component component) where T : Component
{
var cp = component.GetComponent(typeof(T));
return (T)cp;
}
public static T AddComponentEx(this GameObject go) where T : Component
{
var cp = go.AddComponent(typeof(T));
return (T)cp;
}
public static T GetComponentEx(this GameObject go) where T : Component
{
var cp = go.GetComponent(typeof(T));
return (T)cp;
}
public static T GetComponentInParentEx(this Component component) where T : Component
{
var cp = component.GetComponentInParent(typeof(T));
return (T)cp;
}
public static T GetComponentInParentEx(this GameObject go) where T : Component
{
var cp = go.GetComponentInParent(typeof(T));
return (T)cp;
}
public static T FindComponentEx(this Component cp, string path) where T : Component
{
var child = cp.transform.Find(path);
var childCp = child.GetComponentEx();
return (T)childCp;
}
//效果同c#底层的ComponentExtersion SetActive
public static void SetActiveIL(this Component cp, bool state)
{
if (cp == null) return;
if (cp.gameObject.activeSelf != state)
{
cp.gameObject.SetActive(state);
}
}
#endregion
}