1
This commit is contained in:
37
Src/Unity/Pool/PoolObject.cs
Normal file
37
Src/Unity/Pool/PoolObject.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using ZXing.QrCode.Internal;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public interface IPoolObject
|
||||
{
|
||||
ValidHandle EnabledHandle { get; }
|
||||
}
|
||||
[CustomType(typeof(IPoolObject))]
|
||||
public class PoolObject : MonoBehaviour, IPoolObject
|
||||
{
|
||||
[SerializeField, ReadOnly] private bool isEnabled;
|
||||
[SerializeField, ReadOnly(HideLabel = true), TextArea] private string report;
|
||||
public ValidHandle EnabledHandle { get; } = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
EnabledHandle.AddListener(OnEnabled);
|
||||
}
|
||||
private void OnEnabled(bool obj)
|
||||
{
|
||||
isEnabled = obj;
|
||||
report = EnabledHandle.ToString();
|
||||
}
|
||||
[BIT]
|
||||
private void Check()
|
||||
{
|
||||
isEnabled = EnabledHandle.Allow;
|
||||
report = EnabledHandle.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
11
Src/Unity/Pool/PoolObject.cs.meta
Normal file
11
Src/Unity/Pool/PoolObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7efa56ee20709854aa759ada8501b977
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -37,9 +37,30 @@ namespace BITKit
|
||||
{
|
||||
Prefab = _Prefabs[name],
|
||||
OnReturn = null,
|
||||
OnSpawn = OnSpawnInternal
|
||||
};
|
||||
return pool;
|
||||
|
||||
void OnSpawnInternal(Transform newObject)
|
||||
{
|
||||
OnSpawn(name,newObject);
|
||||
}
|
||||
|
||||
}
|
||||
private static void OnSpawn(string newName,Transform newObject)
|
||||
{
|
||||
if (newObject.TryGetComponent<IPoolObject>(out var obj) is false) return;
|
||||
obj.EnabledHandle.AddListener(OnEnabled);
|
||||
// BIT4Log.Log<PoolService>($"Adding {newObject.name} to pool");
|
||||
return;
|
||||
void OnEnabled(bool obj)
|
||||
{
|
||||
if(obj)return;
|
||||
Release(newName,newObject);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[SerializeField] private SerializedDictionary<Transform,int> initialCapacity;
|
||||
|
||||
private void Start()
|
||||
@@ -52,6 +73,7 @@ namespace BITKit
|
||||
DefaultCapacity = value,
|
||||
Root = transform,
|
||||
OnReturn = null,
|
||||
OnSpawn =OnSpawnInternal
|
||||
};
|
||||
_Prefabs.TryAdd(key.name, key);
|
||||
_Pools.TryAdd(key.name, pool);
|
||||
@@ -65,6 +87,12 @@ namespace BITKit
|
||||
x.gameObject.SetActive(false);
|
||||
pool.Return(x);
|
||||
}
|
||||
|
||||
continue;
|
||||
void OnSpawnInternal(Transform newObject)
|
||||
{
|
||||
OnSpawn(key.name,newObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user