Skip to content

Commit 53008f1

Browse files
committed
feat: experimental folder with dynamic pool
Fixed the bdebug namespace and added the experimental dynamic pooling
1 parent 6440e06 commit 53008f1

File tree

6 files changed

+145
-9
lines changed

6 files changed

+145
-9
lines changed

Runtime/Experimental.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace BigasTools{
6+
public class DynamicChild : MonoBehaviour
7+
{
8+
//
9+
// We will set the pool name to return to it here.
10+
//
11+
public string dynamicPoolName;
12+
13+
//
14+
// If it's a particle you can set the stop action as callback and change this
15+
//
16+
private void OnParticleSystemStopped() {
17+
DynamicPool.Instance.ReturnToPool(dynamicPoolName, this.gameObject);
18+
}
19+
}
20+
}

Runtime/Experimental/DynamicChild.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using System.Linq;
5+
6+
//
7+
// This class is responsible for a dynamic pool, its a concept where you can spawn any object you want and save it in the dictionary
8+
//
9+
namespace BigasTools{
10+
public class DynamicPool : MonoBehaviour
11+
{
12+
private static DynamicPool instance;
13+
public static DynamicPool Instance{
14+
get{
15+
if(instance == null)instance = FindObjectOfType<DynamicPool>();
16+
return instance;
17+
}
18+
}
19+
Dictionary<string, List<GameObject>> cachedObjects = new Dictionary<string, List<GameObject>>();
20+
21+
//
22+
// Here you need to create your logic to load the object from the resources folder
23+
//
24+
public virtual GameObject GetObj(string args){
25+
return Resources.Load<GameObject>("Prefabs/Particles/" + args);
26+
}
27+
//
28+
// The core for the dynamic pool, here we'll try to get the gameobject inside the list from the cache.
29+
//
30+
public virtual GameObject GetFromPool(string args, Vector3 pos){
31+
for (int i = 0; i < cachedObjects.Count; i++)
32+
{
33+
var obj = cachedObjects.ElementAt(i);
34+
Debug.Log(obj);
35+
if(obj.Key == args){
36+
if(obj.Value.Count <= 0)break;
37+
Debug.Log(obj.Value.Count);
38+
for (int u = 0; u < obj.Value.Count; u++)
39+
{
40+
if(!obj.Value[u].activeSelf){
41+
var realObj = obj.Value[u];
42+
Debug.Log(realObj + "....");
43+
realObj.SetActive(true);
44+
realObj.transform.position = pos;
45+
obj.Value.Remove(realObj);
46+
return realObj;
47+
}
48+
}
49+
var prefab = GetObj(args);
50+
var prefabIns = Instantiate(prefab, this.transform);
51+
prefabIns.transform.position = pos;
52+
prefabIns.AddComponent<DynamicChild>().dynamicPoolName = args;
53+
return prefabIns;
54+
}
55+
}
56+
var realPrefab = GetObj(args);
57+
var prefabInstance = Instantiate(realPrefab, this.transform);
58+
prefabInstance.transform.position = pos;
59+
prefabInstance.AddComponent<DynamicChild>().dynamicPoolName = args;
60+
try
61+
{
62+
cachedObjects.Add(args, new List<GameObject>(){prefabInstance});
63+
}
64+
catch (System.Exception e)
65+
{
66+
Debug.Log(e);
67+
}
68+
return prefabInstance;
69+
}
70+
//
71+
// The function that will handle the return of an object to the cache (destroying it)
72+
//
73+
public virtual void ReturnToPool(string args, GameObject gameObj){
74+
for (int i = 0; i < cachedObjects.Count; i++)
75+
{
76+
var obj = cachedObjects.ElementAt(i);
77+
if(obj.Key == args){
78+
gameObj.SetActive(false);
79+
obj.Value.Add(gameObj);
80+
}
81+
}
82+
}
83+
}
84+
}

Runtime/Experimental/DynamicPool.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Utils/Console/BDebug.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public static class BDebug
6-
{
7-
//
8-
// Use this to debug log with color and a "title" it's useful so you can search by logs with the same title;
9-
// To check logs related to some class you are testing;
10-
//
11-
public static void Log(object message, string title = "Log", Color? color = null){
12-
var c = color == null ? Color.yellow : color.Value;
13-
Debug.Log(string.Format("<color=#{0:X2}{1:X2}{2:X2}><b>{3}</b> || </color>{4}", (byte)(c.r * 255f), (byte)(c.g * 255f), (byte)(c.b * 255f), title, message));
5+
namespace BigasTools{
6+
public static class BDebug
7+
{
8+
//
9+
// Use this to debug log with color and a "title" it's useful so you can search by logs with the same title;
10+
// To check logs related to some class you are testing;
11+
//
12+
public static void Log(object message, string title = "Log", Color? color = null){
13+
var c = color == null ? Color.yellow : color.Value;
14+
Debug.Log(string.Format("<color=#{0:X2}{1:X2}{2:X2}><b>{3}</b> || </color>{4}", (byte)(c.r * 255f), (byte)(c.g * 255f), (byte)(c.b * 255f), title, message));
15+
}
1416
}
1517
}

0 commit comments

Comments
 (0)