92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using Thousandto.Core.Base;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
namespace Thousandto.Plugins.Common
|
|
{
|
|
/// <summary>
|
|
/// 继承MonoBehaviour,用于记录当前脚本所在对象的GameObject以及Transform引用
|
|
/// </summary>
|
|
public class MonoCacheBase : MonoBehaviour,IMonoCache
|
|
{
|
|
//GameObject的实例引用
|
|
private GameObject _goInstance;
|
|
//Transform的实例引用
|
|
private Transform _transInstance;
|
|
|
|
//GameObject的实例引用
|
|
public GameObject GameObjectInst
|
|
{
|
|
get {
|
|
if (_goInstance == null)
|
|
{
|
|
_goInstance = gameObject;
|
|
}
|
|
return _goInstance;
|
|
}
|
|
}
|
|
|
|
//Transform的实例引用
|
|
public Transform TransformInst
|
|
{
|
|
get {
|
|
if (_transInstance == null)
|
|
{
|
|
_transInstance = transform;
|
|
}
|
|
return _transInstance;
|
|
}
|
|
}
|
|
|
|
public T FindUI<T>(string name) where T : Component
|
|
{
|
|
return FindUI<T>(TransformInst, name);
|
|
}
|
|
|
|
public T FindUI<T>(Transform transInst, string name) where T : Component
|
|
{
|
|
T ret = default(T);
|
|
Transform trans = transInst.Find(name);
|
|
if (trans == null)
|
|
{
|
|
//UnityEngine.Debug.LogError("找不到节点 " + name);
|
|
}
|
|
else
|
|
{
|
|
ret = trans.RequireComponent<T>();
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public T FindCommpent<T>(string nodePath)
|
|
{
|
|
return TransformInst.Find(nodePath).GetComponent<T>();
|
|
}
|
|
public GameObject FindGameObject(string nodePath)
|
|
{
|
|
return TransformInst.Find(nodePath).gameObject;
|
|
}
|
|
public Transform FindTransform(string nodePath)
|
|
{
|
|
return TransformInst.Find(nodePath);
|
|
}
|
|
public T RequireComponent<T>(string nodePath) where T : Component
|
|
{
|
|
return UnityUtils.RequireComponent<T>(FindGameObject(nodePath));
|
|
}
|
|
public T RequireComponent<T>(GameObject go) where T : Component
|
|
{
|
|
return UnityUtils.RequireComponent<T>(go);
|
|
}
|
|
public void AddButtonEvent(UIButton btn, EventDelegate.Callback callBack)
|
|
{
|
|
btn.onClick.Clear();
|
|
btn.onClick.Add(new EventDelegate(callBack));
|
|
}
|
|
}
|
|
}
|