对象池

This commit is contained in:
sin365 2023-01-16 18:31:12 +08:00
parent 0ce2eab33a
commit 683126e76a
9 changed files with 371 additions and 7 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5824f5d37e1edb04e833120b53c5d904
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,98 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &3926400312630769306
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1694234330929003566}
- component: {fileID: 1759003027805891678}
- component: {fileID: 2967990438759553168}
m_Layer: 0
m_Name: 1
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1694234330929003566
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3926400312630769306}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!212 &1759003027805891678
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3926400312630769306}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 0
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: -90551785473829027, guid: 8f368c76f206e9d419a367e6293a9061, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 3, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!65 &2967990438759553168
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3926400312630769306}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 0.5, y: 0.5, z: 0.5}
m_Center: {x: 0, y: 0, z: 0}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a988fc8369d7b6f4ab74497dc9aaf1be
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -146,10 +146,10 @@ AnimatorStateMachine:
m_Position: {x: 240, y: 30, z: 0} m_Position: {x: 240, y: 30, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 7852368639546613177} m_State: {fileID: 7852368639546613177}
m_Position: {x: 340, y: 110, z: 0} m_Position: {x: 450, y: 140, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: -3802118723531622627} m_State: {fileID: -3802118723531622627}
m_Position: {x: 590, y: 60, z: 0} m_Position: {x: 610, y: 80, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 4164821889480732602} m_State: {fileID: 4164821889480732602}
m_Position: {x: 340, y: 260, z: 0} m_Position: {x: 340, y: 260, z: 0}
@ -203,19 +203,19 @@ AnimatorStateMachine:
m_Position: {x: 900, y: 1300, z: 0} m_Position: {x: 900, y: 1300, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 5828820298934977185} m_State: {fileID: 5828820298934977185}
m_Position: {x: 450, y: -170, z: 0} m_Position: {x: 610, y: -70, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 993502365009367806} m_State: {fileID: 993502365009367806}
m_Position: {x: 610, y: -120, z: 0} m_Position: {x: 610, y: -20, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 504469167200563978} m_State: {fileID: 504469167200563978}
m_Position: {x: 0, y: 0, z: 0} m_Position: {x: 100, y: -80, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 993732683156966362} m_State: {fileID: 993732683156966362}
m_Position: {x: 0, y: 0, z: 0} m_Position: {x: 20, y: -170, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 2819678479818455320} m_State: {fileID: 2819678479818455320}
m_Position: {x: 750, y: -50, z: 0} m_Position: {x: 610, y: 30, z: 0}
m_ChildStateMachines: [] m_ChildStateMachines: []
m_AnyStateTransitions: [] m_AnyStateTransitions: []
m_EntryTransitions: [] m_EntryTransitions: []

View File

@ -21,5 +21,14 @@ namespace Game
nodetype = E_NODE_TYPE.N_FREE; nodetype = E_NODE_TYPE.N_FREE;
return false; return false;
} }
#region µ¯Ä»
static public string GetBulletProfabPath(string Name)
{
return $"Assets/GameAssets/Prefabs/Bullet/{Name}.prefab"; ;
}
#endregion
} }
} }

View File

@ -0,0 +1,34 @@
using Axibug.Runtime;
using UnityEngine;
using UnityEditor;
using Axibug.Resources;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
namespace Game
{
public class BulletMgrComponent : GameComponent
{
public PrefabPoolForPath _BulletPool;
class BullteInfo
{
}
private void OnEnable()
{
_BulletPool = new PrefabPoolForPath(this.transform, E_PREFAB_TYPE.NONE,
Common.GetBulletProfabPath("1"));
}
void NewBullet()
{
GameObject obj = _BulletPool.GetAnyHide();
obj.GetHashCode();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 032f463a709b839499569476b49cf21c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,186 @@
using UnityEngine;
using System.Collections.Generic;
using Game;
using UnityEditor;
using Axibug.Resources;
// 局部对象池模板
public class PrefabPoolForPath
{
private Transform mb_vParentTf; // 创建对象时的父节点
private List<GameObject> mb_vObjList; // 对象列表
private string mb_vPrefabPath; // 对象预制名称
private E_PREFAB_TYPE mb_vPrefabType;
public int iActivityCount
{
get
{
int retCount = 0;
if (mb_vObjList != null)
{
for (int i = 0; i < mb_vObjList.Count; i++)
{
if (mb_vObjList[i].gameObject.activeSelf)
retCount++;
}
}
return retCount;
}
}
public List<GameObject> vObjList
{
get
{
if (mb_vObjList == null)
mb_vObjList = new List<GameObject>();
return mb_vObjList;
}
}
// 构造 并 Init()
public PrefabPoolForPath(Transform tfParent, E_PREFAB_TYPE type,string prefabPath)
{
mb_vPrefabType = type;
this.Init(tfParent, prefabPath);
}
// 初始化
void Init(Transform tfParent, string prefabPath)
{
mb_vParentTf = tfParent;
switch (mb_vPrefabType)
{
case E_PREFAB_TYPE.EFFECT:
mb_vPrefabPath = prefabPath;
break;
}
}
// 隐藏
public void Hide()
{
for (int i = 0; i < vObjList.Count; i++)
{
vObjList[i].gameObject.SetActive(false);
}
}
// 隐藏父节点
public void HideNode()
{
mb_vParentTf.gameObject.SetActive(false);
}
// 显示父节点
public void ShowNode()
{
mb_vParentTf.gameObject.SetActive(true);
}
// 获取
public GameObject GetObj(int index)
{
if (index < vObjList.Count)
{
vObjList[index].gameObject.SetActive(true);
return vObjList[index];
}
GameObject go = ClonePrefab(mb_vPrefabPath, mb_vParentTf);
vObjList.Add(go);
return go;
}
// 获取
public GameObject GetAnyHide()
{
int count = vObjList.Count;
for (int i = 0; i < count; i++)
{
if (!vObjList[i].gameObject.activeSelf)
{
vObjList[i].gameObject.SetActive(true);
return vObjList[i];
}
}
return GetObj(count);
}
public void Remove(int index)
{
if (index >= vObjList.Count)
return;
GameObject vObj = vObjList[index];
GameObject.Destroy(vObj.gameObject);
vObjList.RemoveAt(index);
}
public void Remove(GameObject obj)
{
for (int i = 0; i < vObjList.Count; i++)
{
if (vObjList[i] == obj)
{
GameObject.Destroy(obj.gameObject);
vObjList.RemoveAt(i);
return;
}
}
}
// 销毁
public void Destory()
{
for (int i = 0; i < vObjList.Count; i++)
{
GameObject vObj = vObjList[i];
GameObject.Destroy(vObj.gameObject);
}
vObjList.Clear();
}
private GameObject ClonePrefab(string path, Transform parent)
{
string rootPath = "Assets/GameAssets";
string MapName = path;
string tmp = MapName.Remove(0, rootPath.Length + 1);
int idx = tmp.LastIndexOf('/');
string bundleName = tmp.Substring(0, idx);
UnityEngine.Object asset = null;
if (AppEntry.Base.EditorResourceMode)
{
#if UNITY_EDITOR
asset = AssetDatabase.LoadAssetAtPath<GameObject>(MapName);
#endif
}
else
{
int id = MapName.GetHashCode();
asset = PrefabManager.LoadPrefab<UnityEngine.Object>(bundleName.ToLower(), MapName, id, parent);
}
if (asset == null)
{
Debug.LogError($"asset加载失败path={MapName}");
return null;
}
GameObject go = GameObject.Instantiate(asset, parent) as GameObject;
if (go == null)
{
Debug.LogError("LoadPrefabByEditor2. go == null. asset:" + asset);
}
return go;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 94f05d6e34892924d9a3f1ae5b0e5fff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: