Skip to content

Commit 2a3bbbd

Browse files
committed
設定情報をCacheするようにして、Collision設定の反映処理を高速化。
1 parent 57c1d97 commit 2a3bbbd

31 files changed

+314
-134
lines changed

Assets/PhysicsLayers/Editor/LayersManagerInspector.Drawers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace a3geek.PhysicsLayers.Editors
88
{
99
using Common;
10-
using Components;
10+
using Components.InternalManagements;
1111

1212
public partial class LayersManagerInspector
1313
{

Assets/PhysicsLayers/Editor/LayersManagerInspector.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace a3geek.PhysicsLayers.Editors
88
{
99
using Common;
10-
using Components;
10+
using Components.InternalManagements;
1111

1212
[CustomEditor(typeof(LayersManager))]
1313
public partial class LayersManagerInspector : Editor
16 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using System;
5+
using System.Linq;
6+
7+
namespace a3geek.PhysicsLayers.Components
8+
{
9+
using InternalManagements;
10+
11+
[Serializable]
12+
public sealed class AllLayerInfos
13+
{
14+
public PhysicsLayerInfos PhysicsLayerInfos
15+
{
16+
get { return this.physicsLayerInfos; }
17+
}
18+
public UnityLayerInfos UnityLayerInfos
19+
{
20+
get { return this.unityLayerInfos; }
21+
}
22+
23+
public int PhysicsLayerCount
24+
{
25+
get; private set;
26+
}
27+
public Dictionary<int, string> PhysicsLayers
28+
{
29+
get; private set;
30+
}
31+
public IEnumerable<int> PhysicsLayerIDs
32+
{
33+
get; private set;
34+
}
35+
public IEnumerable<string> PhysicsLayerNames
36+
{
37+
get; private set;
38+
}
39+
40+
public Dictionary<int, string> UnityLayers
41+
{
42+
get; private set;
43+
}
44+
public IEnumerable<int> UnityLayerIDs
45+
{
46+
get; private set;
47+
}
48+
public IEnumerable<string> UnityLayerNames
49+
{
50+
get; private set;
51+
}
52+
53+
[SerializeField]
54+
private PhysicsLayerInfos physicsLayerInfos = new PhysicsLayerInfos();
55+
[SerializeField]
56+
private UnityLayerInfos unityLayerInfos = new UnityLayerInfos();
57+
58+
private Dictionary<int, IEnumerable<int>> ignoreLayersCache = new Dictionary<int, IEnumerable<int>>();
59+
60+
61+
public AllLayerInfos()
62+
{
63+
this.PhysicsLayers = new Dictionary<int, string>();
64+
this.PhysicsLayerIDs = new List<int>();
65+
this.PhysicsLayerNames = new List<string>();
66+
67+
this.UnityLayers = new Dictionary<int, string>();
68+
this.UnityLayerIDs = new List<int>();
69+
this.UnityLayerNames = new List<string>();
70+
}
71+
72+
public void UpdateCache()
73+
{
74+
this.PhysicsLayerCount = this.PhysicsLayerInfos.LayerCount;
75+
this.PhysicsLayers = this.PhysicsLayerInfos.Layers;
76+
this.PhysicsLayerIDs = this.PhysicsLayerInfos.LayerIDs;
77+
this.PhysicsLayerNames = this.PhysicsLayerInfos.LayerNames;
78+
79+
this.UnityLayers = this.UnityLayerInfos.Layers;
80+
this.UnityLayerIDs = this.UnityLayerInfos.LayerIDs;
81+
this.UnityLayerNames = this.UnityLayerInfos.LayerNames;
82+
83+
foreach(var layerID in this.UnityLayerIDs.Concat(this.PhysicsLayerIDs))
84+
{
85+
this.ignoreLayersCache[layerID] = this.GetIgnoreIDs(layerID);
86+
}
87+
}
88+
89+
public IEnumerable<int> GetIgnoreLayerIDs(int layerID)
90+
{
91+
IEnumerable<int> ie = null;
92+
this.ignoreLayersCache.TryGetValue(layerID, out ie);
93+
94+
return ie ?? new List<int>();
95+
}
96+
97+
private IEnumerable<int> GetIgnoreIDs(int layerID)
98+
{
99+
if(layerID < LayersManager.UnityLayerCount)
100+
{
101+
return this.PhysicsLayerInfos.GetEnumerable()
102+
.Where(layer =>
103+
{
104+
var layerCollision = layer[layerID];
105+
return layerCollision != null && layerCollision.Collision == false;
106+
})
107+
.Select(layer => layer.LayerID);
108+
}
109+
110+
var physicsLayer = this.PhysicsLayerInfos[layerID];
111+
return physicsLayer == null ? new List<int>() : physicsLayer.GetEnumerable()
112+
.Where(layerCollision => layerCollision.Collision == false)
113+
.Select(layerCollision => layerCollision.LayerID);
114+
}
115+
}
116+
}

Assets/PhysicsLayers/Scripts/Components/AllLayerInfos.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using System;
4+
using System.Linq;
5+
6+
namespace a3geek.PhysicsLayers.Components
7+
{
8+
using Layers.Abstracts;
9+
10+
using AbsCollLayer = Layers.Abstracts.AbstractCollisionLayer<Collider>;
11+
using AbsCollLayer2D = Layers.Abstracts.AbstractCollisionLayer<Collider2D>;
12+
13+
public sealed class CollisionInfosSetter
14+
{
15+
public AllLayerInfos AllLayerInfos { get; set; }
16+
17+
private Dictionary<int, List<AbsCollLayer2D>> collLayers2D = new Dictionary<int, List<AbsCollLayer2D>>();
18+
private Dictionary<int, List<AbsCollLayer>> collLayers = new Dictionary<int, List<AbsCollLayer>>();
19+
20+
21+
public void Management(AbsCollLayer layer)
22+
{
23+
var layerID = this.SetIgnoreCollisions(layer, this.collLayers, lay => layer.IgnoreCollisions(lay.Colliders, true));
24+
if(layerID >= 0)
25+
{
26+
this.collLayers[layerID].Add(layer);
27+
}
28+
}
29+
30+
public void Management(AbsCollLayer2D layer)
31+
{
32+
var layerID = this.SetIgnoreCollisions(layer, this.collLayers2D, lay => layer.IgnoreCollisions(lay.Colliders, true));
33+
if(layerID >= 0)
34+
{
35+
this.collLayers2D[layerID].Add(layer);
36+
}
37+
}
38+
39+
public void UnManagement(AbsCollLayer layer)
40+
{
41+
var layerID = layer.LayerID;
42+
if(layerID < 0)
43+
{
44+
return;
45+
}
46+
47+
this.CheckDicKey(layerID);
48+
this.collLayers[layerID].Remove(layer);
49+
}
50+
51+
public void UnManagement(AbsCollLayer2D layer)
52+
{
53+
var layerID = layer.LayerID;
54+
if(layerID < 0)
55+
{
56+
return;
57+
}
58+
59+
this.CheckDicKey(layerID);
60+
this.collLayers2D[layerID].Remove(layer);
61+
}
62+
63+
public void ResetIgnoreCollision(AbsCollLayer layer)
64+
{
65+
this.SetIgnoreCollisions(layer, this.collLayers, lay => lay.IgnoreCollisions(layer.Colliders, false));
66+
}
67+
68+
public void ResetIgnoreCollision(AbsCollLayer2D layer)
69+
{
70+
this.SetIgnoreCollisions(layer, this.collLayers2D, lay2D => lay2D.IgnoreCollisions(layer.Colliders, false));
71+
}
72+
73+
private int SetIgnoreCollisions<T1, T2>(T1 layer, Dictionary<int, List<T2>> layersDic, Action<T2> setter)
74+
where T1 : AbstractLayer where T2 : AbstractLayer
75+
{
76+
var layerID = layer.LayerID;
77+
if(layerID < 0)
78+
{
79+
return -1;
80+
}
81+
82+
this.CheckDicKey(layerID);
83+
84+
var ignoreLayerIDs = this.AllLayerInfos.GetIgnoreLayerIDs(layerID);
85+
if(ignoreLayerIDs.Count() <= 0)
86+
{
87+
return layerID;
88+
}
89+
90+
foreach(var id in ignoreLayerIDs)
91+
{
92+
List<T2> colls = null;
93+
if(layersDic.TryGetValue(id, out colls) == true)
94+
{
95+
colls.ForEach(coll => setter(coll));
96+
}
97+
}
98+
99+
return layerID;
100+
}
101+
102+
private void CheckDicKey(int layerID)
103+
{
104+
if(this.collLayers.ContainsKey(layerID) == false)
105+
{
106+
this.collLayers.Add(layerID, new List<AbsCollLayer>());
107+
}
108+
109+
if(this.collLayers2D.ContainsKey(layerID) == false)
110+
{
111+
this.collLayers2D.Add(layerID, new List<AbsCollLayer2D>());
112+
}
113+
}
114+
}
115+
}

Assets/PhysicsLayers/Scripts/Components/CollisionInfosSetter.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PhysicsLayers/Scripts/Components/InternalManagements.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PhysicsLayers/Scripts/Components/AbstractLayer.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/AbstractLayer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
[Serializable]
1010
public abstract class AbstractLayer : ILayer

Assets/PhysicsLayers/Scripts/Components/AbstractLayerInfos.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/AbstractLayerInfos.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
[Serializable]
1010
public abstract class AbstractLayerInfos<T> : ILayerInfos where T : ILayer

Assets/PhysicsLayers/Scripts/Components/ILayer.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/ILayer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
public interface ILayer
1010
{

Assets/PhysicsLayers/Scripts/Components/ILayerInfos.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/ILayerInfos.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
public interface ILayerInfos
1010
{

Assets/PhysicsLayers/Scripts/Components/LayerCollision.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/LayerCollision.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
[Serializable]
1010
public sealed class LayerCollision

Assets/PhysicsLayers/Scripts/Components/PhysicsLayer.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/PhysicsLayer.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
9-
using Common;
10-
119
[Serializable]
1210
public sealed class PhysicsLayer : AbstractLayer
1311
{

Assets/PhysicsLayers/Scripts/Components/PhysicsLayerInfos.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/PhysicsLayerInfos.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
using Common;
1010

Assets/PhysicsLayers/Scripts/Components/UnityLayer.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/UnityLayer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
[Serializable]
1010
public sealed class UnityLayer : AbstractLayer

Assets/PhysicsLayers/Scripts/Components/UnityLayerInfos.cs Assets/PhysicsLayers/Scripts/Components/InternalManagements/UnityLayerInfos.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Linq;
66

7-
namespace a3geek.PhysicsLayers.Components
7+
namespace a3geek.PhysicsLayers.Components.InternalManagements
88
{
99
[Serializable]
1010
public sealed class UnityLayerInfos : AbstractLayerInfos<UnityLayer>

0 commit comments

Comments
 (0)