Skip to content

Commit 1d8ec8f

Browse files
committed
CacheCompactionの機能を有効化
1 parent a150deb commit 1d8ec8f

File tree

6 files changed

+75
-18
lines changed

6 files changed

+75
-18
lines changed

Assets/PhysicsLayers/Example/Scripts/TestController.cs

+59-15
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ public class TestController : MonoBehaviour
1616
{
1717
[SerializeField]
1818
private FieldInfo fieldInfo = new FieldInfo();
19-
20-
private List<AbstractCollisionCallbacks> collisions = new List<AbstractCollisionCallbacks>();
19+
[SerializeField]
20+
private bool autoRespawn = true;
21+
[SerializeField, Range(0f, 1f)]
22+
private float respawnRate = 0.05f;
23+
24+
private bool spawned = false;
25+
private Dictionary<int, List<AbstractCollisionCallbacks>> collisions = new Dictionary<int, List<AbstractCollisionCallbacks>>();
2126

2227

2328
void Awake()
@@ -27,25 +32,53 @@ void Awake()
2732

2833
void Update()
2934
{
35+
if(this.spawned == false)
36+
{
37+
return;
38+
}
39+
3040
if(Input.GetKeyDown(KeyCode.Space))
3141
{
32-
this.collisions
33-
.FindAll(coll => coll.LayerID == LayersManager.UnityLayerCount)
42+
this.collisions[LayersManager.UnityLayerCount]
3443
.ForEach(coll => coll.gameObject.SetActive(!coll.gameObject.activeSelf));
3544
}
3645

3746
if(Input.GetKeyDown(KeyCode.A))
3847
{
39-
var colls = this.collisions
40-
.FindAll(coll => coll.LayerID == LayersManager.UnityLayerCount).ToArray();
48+
var colls = this.collisions[LayersManager.UnityLayerCount];
4149

42-
for(var i = 0; i < colls.Length; i++)
50+
for(var i = 0; i < colls.Count; i++)
4351
{
52+
colls[i].Layer.ChangeLayer(LayersManager.UnityLayerCount + 1);
4453
colls[i].UpdateIgnoreLayers();
4554
}
4655
}
47-
}
4856

57+
if(this.autoRespawn == false || Random.value > this.respawnRate)
58+
{
59+
return;
60+
}
61+
62+
var keys = this.collisions.Keys.ToList();
63+
var layerID = keys[Random.Range(0, keys.Count)];
64+
65+
if(Random.value <= 0.5f)
66+
{
67+
Debug.Log("Destroy");
68+
var colls = this.collisions[layerID];
69+
var coll = colls[Random.Range(0, colls.Count)];
70+
71+
this.collisions[layerID].Remove(coll);
72+
Destroy(coll.gameObject);
73+
}
74+
else
75+
{
76+
Debug.Log("Create");
77+
var info = this.fieldInfo.Infos.FirstOrDefault(e => e.Prefab.LayerID == layerID);
78+
this.Create(info.Prefab, info.Parent, info.Color);
79+
}
80+
}
81+
4982
private IEnumerator Spawn()
5083
{
5184
for(var i = 0; i < this.fieldInfo.Infos.Count; i++)
@@ -54,19 +87,27 @@ private IEnumerator Spawn()
5487

5588
var parent = new GameObject(info.Prefab.name + "s");
5689
parent.transform.parent = transform;
90+
info.Parent = parent.transform;
91+
92+
this.collisions.Add(info.Prefab.LayerID, new List<AbstractCollisionCallbacks>());
5793

5894
for(var j = 0; j < info.Count; j++)
5995
{
60-
var obj = Instantiate(info.Prefab);
61-
obj.name = i + ":" + obj.name;
62-
obj.transform.parent = parent.transform;
63-
64-
obj.GetComponent<Renderer>().material.SetColor("_Color", info.Color);
65-
this.collisions.Add(obj);
66-
96+
this.Create(info.Prefab, parent.transform, info.Color);
6797
yield return new WaitForSeconds(0f);
6898
}
6999
}
100+
101+
this.spawned = true;
102+
}
103+
104+
private void Create(AbstractCollisionCallbacks prefab, Transform parent, Color color)
105+
{
106+
var obj = Instantiate(prefab);
107+
obj.transform.parent = parent;
108+
109+
obj.GetComponent<Renderer>().material.SetColor("_Color", color);
110+
this.collisions[obj.LayerID].Add(obj);
70111
}
71112

72113
[Serializable]
@@ -78,6 +119,9 @@ public class Info
78119
public AbstractCollisionCallbacks Prefab = null;
79120
public int Count = 1;
80121
public Color Color = Color.white;
122+
123+
[HideInInspector]
124+
public Transform Parent = null;
81125
}
82126

83127
public List<Info> Infos = new List<Info>();

Assets/PhysicsLayers/Scripts/Common/IntervalExecutor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void Update()
3535
{
3636
this.actions[this.index]();
3737

38+
this.timer = 0f;
3839
this.index++;
3940
if(this.index >= this.actions.Count)
4041
{

Assets/PhysicsLayers/Scripts/Components/CollisionInfosSetter.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,15 @@ private void AddCollLayer(int id)
107107
this.collLayers.Add(id, new CacheableArray<AbsCollLayer>(this.manager.cacheCapacity));
108108
this.collLayers2D.Add(id, new CacheableArray<AbsCollLayer2D>(this.manager.cacheCapacity));
109109

110-
this.manager.compactionExecutor.AddAction(() => this.collLayers[id].CacheCompaction());
111-
this.manager.compactionExecutor.AddAction(() => this.collLayers2D[id].CacheCompaction());
110+
this.manager.compactionExecutor.AddAction(() =>
111+
{
112+
this.collLayers[id].CacheCompaction();
113+
});
114+
115+
this.manager.compactionExecutor.AddAction(() =>
116+
{
117+
this.collLayers2D[id].CacheCompaction();
118+
});
112119
}
113120
}
114121
}

Assets/PhysicsLayers/Scripts/LayersManager.cs

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ private static void Initialize()
111111
ins.collisionInfosSetter = new CollisionInfosSetter(ins);
112112
}
113113

114+
private void Update()
115+
{
116+
this.compactionExecutor.Update();
117+
}
118+
114119
public IEnumerable<int> GetIgnoreLayerIDs(int layerID)
115120
{
116121
return this.AllLayerInfos.GetIgnoreLayerIDs(layerID);

ProjectSettings/ProjectSettings.asset

11.7 KB
Binary file not shown.

ProjectSettings/ProjectVersion.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
m_EditorVersion: 5.5.0f3
1+
m_EditorVersion: 5.6.3f1

0 commit comments

Comments
 (0)