- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg) - LoginPanel: InputField placeholder本地化、字体颜色保持 - HistoryPanel: 用时数据本地化、placeholder本地化 - RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建) - AppraiseWindowBase: 评价等级本地化、操作消息重建 - EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized - StudentOperateRecorder: 新增InjectOperateMsgLocalized方法 - LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选 - 字体切换时保留颜色和verticalOverflow
182 lines
6.0 KiB
C#
182 lines
6.0 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
using DevelopEngine;
|
||
|
||
namespace XFramework.Objects
|
||
{
|
||
/// <summary>
|
||
/// 对象池管理
|
||
/// </summary>
|
||
public class ObjectPool : MonoSingleton<ObjectPool>
|
||
{
|
||
/// <summary>
|
||
/// 存放所有对象池的字典
|
||
/// 每一个原始对象都有一个id,每个id就对应一个对象池
|
||
/// </summary>
|
||
private Dictionary<int, SingleObjPool<GameObject>> originalDict;
|
||
/// <summary>
|
||
/// 存放所有已经被实例化对象
|
||
/// 每一个被实例化后的对象都有相应的对象池
|
||
/// </summary>
|
||
private Dictionary<GameObject, SingleObjPool<GameObject>> objPoolDict;
|
||
/// <summary>
|
||
/// 场景里面的对象
|
||
/// 用于将已经释放的对象收起来
|
||
/// </summary>
|
||
private GameObject poolManager;
|
||
//private GameRoot Game { get => GameRoot.Instance; }
|
||
|
||
private void Awake()
|
||
{
|
||
originalDict = new Dictionary<int, SingleObjPool<GameObject>>();
|
||
objPoolDict = new Dictionary<GameObject, SingleObjPool<GameObject>>();
|
||
}
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
poolManager = this.gameObject;//GameObject.Find("PoolManager") ?? new GameObject("PoolManager");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据原始对象获取对应的对象池
|
||
/// </summary>
|
||
/// <param name="prefab">对象</param>
|
||
/// <returns></returns>
|
||
private SingleObjPool<GameObject> GetPool(GameObject prefab)
|
||
{
|
||
int id = prefab.GetInstanceID();
|
||
|
||
if (!originalDict.ContainsKey(id))
|
||
{
|
||
SingleObjPool<GameObject> objPool = new SingleObjPool<GameObject>(prefab);
|
||
originalDict.Add(id, objPool);
|
||
}
|
||
|
||
return originalDict[id];
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从对象池里获取一个对象
|
||
/// </summary>
|
||
/// <param name="prefab">对象</param>
|
||
/// <returns></returns>
|
||
private GameObject GetObject(GameObject prefab)
|
||
{
|
||
GameObject obj = GetPool(prefab).Get();
|
||
|
||
if (!objPoolDict.ContainsKey(obj))
|
||
objPoolDict.Add(obj, GetPool(prefab));
|
||
obj.SetActive(true);
|
||
return obj;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从对象池取出一个对象
|
||
/// </summary>
|
||
/// <param name="prefab"></param>
|
||
/// <returns></returns>
|
||
public GameObject Instantiate(GameObject prefab)
|
||
{
|
||
GameObject obj = GetObject(prefab);
|
||
obj.transform.SetParent(null);
|
||
|
||
return obj;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从对象池取出一个对象
|
||
/// </summary>
|
||
/// <param name="prefab">对象</param>
|
||
/// <param name="parent">父对象</param>
|
||
/// <returns></returns>
|
||
public GameObject Instantiate(GameObject prefab, Transform parent)
|
||
{
|
||
GameObject obj = GetObject(prefab);
|
||
obj.transform.SetParent(parent);
|
||
return obj;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从对象池取出一个对象
|
||
/// </summary>
|
||
/// <param name="prefab">对象</param>
|
||
/// <param name="position">位置</param>
|
||
/// <param name="rotation">旋转</param>
|
||
/// <returns></returns>
|
||
public GameObject Instantiate(GameObject prefab, Vector3 position, Quaternion rotation)
|
||
{
|
||
GameObject obj = GetObject(prefab);
|
||
obj.transform.position = position;
|
||
obj.transform.rotation = rotation;
|
||
obj.transform.SetParent(null);
|
||
|
||
return obj;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从对象池取出一个对象
|
||
/// </summary>
|
||
/// <param name="prefab">对象</param>
|
||
/// <param name="position">位置</param>
|
||
/// <param name="rotation">旋转</param>
|
||
/// <param name="parent">父对象</param>
|
||
/// <returns></returns>
|
||
public GameObject Instantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent)
|
||
{
|
||
GameObject obj = GetObject(prefab);
|
||
obj.transform.position = position;
|
||
obj.transform.rotation = rotation;
|
||
obj.transform.SetParent(parent);
|
||
|
||
return obj;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 立即回收对象
|
||
/// </summary>
|
||
/// <param name="prefab">要回收的对象</param>
|
||
/// <param name="action">回收对象后执行的方法</param>
|
||
public void Destroy(GameObject prefab, UnityAction<GameObject> action = null)
|
||
{
|
||
if (!objPoolDict.ContainsKey(prefab))
|
||
return;
|
||
if (prefab == null|| poolManager==null) return;
|
||
SingleObjPool<GameObject> objPool = objPoolDict[prefab];
|
||
if (objPool.Release(prefab))
|
||
{
|
||
objPoolDict.Remove(prefab);
|
||
prefab.SetActive(false);
|
||
prefab.transform.SetParent(poolManager.transform);
|
||
action?.Invoke(prefab);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 延迟执行回收
|
||
/// </summary>
|
||
/// <param name="prefab">要回收的对象</param>
|
||
/// <param name="time">延迟时间</param>
|
||
/// <param name="action">回收对象后执行的方法</param>
|
||
public void Destroy(GameObject prefab, float time, UnityAction<GameObject> action = null)
|
||
{
|
||
StartCoroutine(Delay(prefab, time, action));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 延迟
|
||
/// </summary>
|
||
/// <param name="prefab"></param>
|
||
/// <param name="time"></param>
|
||
/// <param name="action"></param>
|
||
/// <returns></returns>
|
||
private IEnumerator Delay(GameObject prefab, float time, UnityAction<GameObject> action)
|
||
{
|
||
yield return new WaitForSeconds(time);
|
||
Destroy(prefab, action);
|
||
}
|
||
}
|
||
}
|