/// <summary> /// 同步加载 /// </summary> /// <param name="path">路径</param> /// <param name="setSceneObj">是否设置Parent为SceneTrs</param> /// <param name="bClear">跳场景是否清除</param> /// /// <param name="createCall">创建时回调函数</param> /// <returns></returns> public GameObject InstantiateObject(string path, bool setSceneParent = false, bool bClear = true) { uint crc = CRC32.GetCRC32(path); TResouceObj resouceObj = GetObjectFromPool(crc); if (resouceObj == null) { resouceObj = mResouceObjPool.Allocate(); resouceObj.Crc = crc; resouceObj.Clear = bClear; //ResouceManager提供加载方法 resouceObj = ResourcesManager.Instance.LoadResouce(path, resouceObj); if (resouceObj.ResItem.Obj != null) { resouceObj.CloneObj = GameObject.Instantiate(resouceObj.ResItem.Obj) as GameObject; resouceObj.OffData = resouceObj.CloneObj.GetComponent <OfflineData>(); resouceObj.OffData.ResetProp(); } } if (setSceneParent) { resouceObj.CloneObj.transform.SetParent(SceneTrs, false); } int tempID = resouceObj.CloneObj.GetInstanceID(); if (!mResouceObjDic.ContainsKey(tempID)) { mResouceObjDic.Add(tempID, resouceObj); } return(resouceObj.CloneObj); }
public void SimpleObjectPool_Test() { var fishPool = new SimpleObjectPool <Fish>(() => new Fish(), null, 100); Assert.AreEqual(fishPool.CurCount, 100); var fishOne = fishPool.Allocate(); Assert.AreEqual(fishPool.CurCount, 99); fishPool.Recycle(fishOne); Assert.AreEqual(fishPool.CurCount, 100); for (var i = 0; i < 10; i++) { fishPool.Allocate(); } Assert.AreEqual(fishPool.CurCount, 90); }
/// <summary> /// 加载单个AssetBundle根据名字 /// </summary> /// <param name="name">AB 包名</param> /// <returns></returns> AssetBundle LoadAssetBundle(string name) { TAssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (!mAssetBundleItemDic.TryGetValue(crc, out item)) { AssetBundle assetBundel = null; string hotAbPath = HotPatchManager.Instance.ComputeABPath(name); string fullpath = string.IsNullOrEmpty(hotAbPath) ? ABLoadPath + name : hotAbPath; byte[] bytes = AES.AESFileByteDecrypt(fullpath, EdgeFrameworkConst.AESKEY); assetBundel = AssetBundle.LoadFromMemory(bytes); if (assetBundel == null) { Debug.LogError("Load AssetBundle Error:" + fullpath); } item = mAssetBundleItemPool.Allocate(); item.AB = assetBundel; item.ReCount++; mAssetBundleItemDic.Add(crc, item); } else { item.ReCount++; } return(item.AB); }
/// <summary> /// 生成预设物体 /// </summary> /// <param name="prefab">预设物体</param> /// <returns></returns> protected GameObject SpawnChildren(SpawnChild sc, Transform parent) { GameObject go = null; if (!Spawnall.ContainsKey(sc)) { Spawnall.Add(sc, new List <GameObject>()); } switch (sc) { case SpawnChild.FirstPoolItem: go = selfMainpool.Allocate(); break; case SpawnChild.SecendPoolItem: go = selfSecendpool.Allocate(); //Instantiate(prefab); break; default: break; } go.SetActive(true); go.transform.SetParent(parent); Spawnall[sc].Add(go); return(go); }
public static RectTransform NewUIGameObject(string name, GameObject parent = null) { SimpleObjectPool <GameObject> simpleObjectPool = new SimpleObjectPool <GameObject> ( () => { GameObject clone = new GameObject(name); RectTransform rect = clone.OnAddComponent <RectTransform> (); rect.localScale = Vector3.one; rect.anchoredPosition3D = Vector3.zero; if (parent != null) { clone.transform.parent = parent.transform; } return(clone); }, (GameObject g) => { Debug.Log(g.name); g.transform.parent = null; g.GetComponent <RectTransform> ().anchoredPosition = Vector3.zero; g.SetActive(false); }, 1); //从对象池中获取对象 GameObject go = simpleObjectPool.Allocate(); return(go.GetComponent <RectTransform> ()); }
//这里的对象池无法回收 public static GameObject NewGameObject(string name, GameObject parent = null) { SimpleObjectPool <GameObject> simpleObjectPool = new SimpleObjectPool <GameObject> ( () => { GameObject clone = new GameObject(name); if (parent != null) { clone.transform.parent = parent.transform; clone.transform.position = Vector3.zero; clone.transform.localScale = Vector3.one; //Debug.Log ("GameObjectTool:" + clone.name + "创建成功"); } return(clone); }, (GameObject g) => { Debug.Log(g.name); g.transform.parent = null; g.GetComponent <RectTransform> ().anchoredPosition = Vector3.zero; g.SetActive(false); }, 1); //从对象池中获取对象 GameObject go = simpleObjectPool.Allocate(); return(go); }
void Start() { var fishPool = new SimpleObjectPool <Fish>(() => new Fish(), null, 100); Debug.LogFormat("fishPool.CurCount:{0}", fishPool.CurCount); var fishOne = fishPool.Allocate(); Debug.LogFormat("fishPool.CurCount:{0}", fishPool.CurCount); fishPool.Recycle(fishOne); Debug.LogFormat("fishPool.CurCount:{0}", fishPool.CurCount); for (var i = 0; i < 10; i++) { fishPool.Allocate(); } Debug.LogFormat("fishPool.CurCount:{0}", fishPool.CurCount); }
private void Start() { var fishPool = new SimpleObjectPool <Fish>(() => new Fish(), null, 100); Log.I("fishPool.CurCount:{0}", fishPool.CurCount); var fishOne = fishPool.Allocate(); Log.I("fishPool.CurCount:{0}", fishPool.CurCount); fishPool.Recycle(fishOne); Log.I("fishPool.CurCount:{0}", fishPool.CurCount); for (int i = 0; i < 10; i++) { fishPool.Allocate(); } Log.I("fishPool.CurCount:{0}", fishPool.CurCount); }
void Start() { pool = new SimpleObjectPool <Fish> (Create, (Fish f) => { f.id = 0; }, num); for (int i = 0; i < pool.count; i++) { Fish f = pool.Allocate(); Debug.Log(f.id); } }
public void SimpleObjectPool_Test() { var fishPool = new SimpleObjectPool <Fish>(() => new Fish(), (hh) => { Debug.Log(hh.GetType().Name); }, 100); Assert.AreEqual(fishPool.CurCount, 100); var fishOne = fishPool.Allocate(); Assert.AreEqual(fishPool.CurCount, 99); fishPool.Recycle(fishOne); Assert.AreEqual(fishPool.CurCount, 100); for (var i = 0; i < 10; i++) { fishPool.Allocate(); } Assert.AreEqual(fishPool.CurCount, 90); }
public void SimpleObjectPool_Test() { var fishPool = new SimpleObjectPool <Fish>(() => new Fish(), null, 100); Debug.Log(fishPool.CurCount); var fishOne = fishPool.Allocate(); Debug.Log(fishPool.CurCount); fishPool.Recycle(fishOne); Debug.Log(fishPool.CurCount); for (var i = 0; i < 10; i++) { fishPool.Allocate(); } Debug.Log(fishPool.CurCount); }
// public MonsterBulletCharacter CreatBullet(Vector3 dir,Vector3 creatPoint) // { // MonsterBulletCharacter monsterBulletCharacter = Instantiate(monsterBulletTemplate, creatPoint, Quaternion.identity); // monsterBulletCharacter.gameObject.SetActive(true); // monsterBulletCharacter.dir = dir; // tempBullets.Add(monsterBulletCharacter); // return monsterBulletCharacter; // } public MonsterBulletCharacter AllocateBullet(Vector3 dir, Vector3 creatPoint) { MonsterBulletCharacter temp = _bulletPool.Allocate(); temp.transform.position = creatPoint; temp.gameObject.SetActive(true); temp.enabled = true; temp.isMove = true; temp.speed = bulletSpeed; temp.FireMode = this; temp.dir = dir; return(temp); }
/// <summary> /// 异步加载资源(仅仅是不需要实例化的资源,例如音频,图片等) /// </summary> /// <param name="path"></param> /// <param name="dealFinish"></param> /// <param name="priority"></param> /// <param name="param1"></param> /// <param name="param2"></param> /// <param name="param3"></param> /// <param name="crc"></param> public void AsyncLoadResouce(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool isSprite = false, object param1 = null, object param2 = null, object param3 = null, uint crc = 0) { if (crc == 0) { crc = CRC32.GetCRC32(path); } TResouceItem item = GetCacheResouceItem(crc); if (item != null) { if (dealFinish != null) { dealFinish(path, item.Obj, param1, param2, param3); } return; } //判断是否在加载中 TAsyncLoadResParam para = null; if (!mLoadingAssetDic.TryGetValue(crc, out para) || para == null) { para = mAsyncLoadResParamPool.Allocate(); para.Crc = crc; para.Path = path; para.IsSprite = isSprite; para.Priority = priority; mLoadingAssetDic.Add(crc, para); mLoadingAssetList[(int)priority].Add(para); } //往回调列表里面加回调 TAsyncCallBack callBack = mAsyncCallBackPool.Allocate(); callBack.DealObjFinish = dealFinish; callBack.Param1 = param1; callBack.Param2 = param2; callBack.Param3 = param3; para.CallbackList.Add(callBack); }
public void AllocateMonster() { EnemyControllerBase temp = _boomEnemysPool.Allocate(); temp.transform.position = currentInstantiatePos.position + new Vector3( UnityEngine.Random.Range(-3, 4), 0, UnityEngine.Random.Range(-3, 4)); if (!_boomEnemys.Contains(temp)) { _boomEnemys.Enqueue(temp); } temp.gameObject.SetActive(true); temp.Respawn(); }
private void Start() { #region 简易对象池示例 SimpleObjectPool <EFrameSimpleMsg> simpleMsgPool = new SimpleObjectPool <EFrameSimpleMsg>( () => new EFrameSimpleMsg(), simpleMsg => { simpleMsg.Reset(); }, 10 ); Debug.Log(string.Format("simpleMsgPool.CurCount: {0}", simpleMsgPool.CurCount)); var t_simpleMsg = simpleMsgPool.Allocate(); Debug.Log(string.Format("simpleMsgPool.CurCount: {0}", simpleMsgPool.CurCount)); var res = simpleMsgPool.Recycle(t_simpleMsg); Debug.Log(string.Format("simpleMsgPool.CurCount: {0}", simpleMsgPool.CurCount)); EFrameSimpleMsg[] msgs = new EFrameSimpleMsg[5]; for (int i = 0; i < msgs.Length; i++) { msgs[i] = simpleMsgPool.Allocate(); } Debug.Log(string.Format("simpleMsgPool.CurCount: {0}", simpleMsgPool.CurCount)); for (int i = 0; i < msgs.Length; i++) { simpleMsgPool.Recycle(msgs[i]); } Debug.Log(string.Format("simpleMsgPool.CurCount: {0}", simpleMsgPool.CurCount)); #endregion #region 安全对象池示例 SafeObjectPool <EFrameSafeMsg> safeMsgPool = SafeObjectPool <EFrameSafeMsg> .Instance; safeMsgPool.Init(100, 50); Debug.Log(string.Format("safeMsgPool.CurCount: {0}", safeMsgPool.CurCount)); var msg = EFrameSafeMsg.Allocate(); Debug.Log(string.Format("safeMsgPool.CurCount: {0}", safeMsgPool.CurCount)); msg.Recycle2Cache(); Debug.Log(string.Format("safeMsgPool.CurCount: {0}", safeMsgPool.CurCount)); EFrameSafeMsg[] safeMsgs = new EFrameSafeMsg[40]; for (int i = 0; i < safeMsgs.Length; i++) { safeMsgs[i] = EFrameSafeMsg.Allocate(); } Debug.Log(string.Format("safeMsgPool.CurCount: {0}", safeMsgPool.CurCount)); for (int i = 0; i < safeMsgs.Length; i++) { safeMsgs[i].Recycle2Cache(); } Debug.Log(string.Format("safeMsgPool.CurCount: {0}", safeMsgPool.CurCount)); #endregion }