Skip to content

Commit 2360079

Browse files
committed
InspectorにDeleteボタンを追加するにあたって設計をやり直した。謎のエラーが出る。
1 parent 790bb29 commit 2360079

20 files changed

+548
-158
lines changed

Assets/PhysicsLayers/Editor/LayersManagerInspector.Drawer.cs renamed to Assets/PhysicsLayers/Editor/LayersManagerInspector.Drawers.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace a3geek.PhysicsLayers.Editors
99
using Common;
1010
using Components;
1111

12-
using LayerDic = Dictionary<int, string>;
12+
using LayerDic = Dictionary<Components.LayerID, string>;
1313

1414
public partial class LayersManagerInspector
1515
{
@@ -24,28 +24,33 @@ private void DrawPhysicsLayers(LayerDic layers)
2424
{
2525
using(var hori = new EditorGUILayout.HorizontalScope())
2626
{
27-
EditorGUILayout.LabelField("Size");
27+
EditorGUILayout.PrefixLabel("Size");
2828

2929
var count = EditorGUILayout.DelayedIntField(layers.Count);
3030
layers.SetCount(count, index => index + LayersManager.UnityLayerCount, index => "PhysicsLayer" + index);
3131
}
3232

3333
EditorGUILayout.Space();
3434

35-
var layerIDs = new List<int>(layers.Keys);
35+
var layerIDs = new List<LayerID>(layers.Keys);
3636
foreach(var layerID in layerIDs)
3737
{
3838
using(var hori = new EditorGUILayout.HorizontalScope())
3939
{
4040
var layerName = layers[layerID];
4141

42-
EditorGUILayout.LabelField("Layer ID : " + layerID);
42+
EditorGUILayout.PrefixLabel("Layer ID : " + layerID.ID);
4343
layers[layerID] = EditorGUILayout.DelayedTextField(layerName);
4444

4545
if(string.IsNullOrEmpty(layers[layerID]) == true)
4646
{
4747
layers[layerID] = layerName;
4848
}
49+
50+
if(GUILayout.Button("Delete"))
51+
{
52+
layers.Remove(layerID);
53+
}
4954
}
5055
}
5156
}
@@ -58,15 +63,14 @@ private void DrawCollInfos(PhysicsLayerInfos layerInfos)
5863
EditorGUI.indentLevel += 1;
5964

6065
this.collInfosFolders.SetCount(layerInfos.LayerCount, index => true);
61-
62-
foreach(var layerIndex in layerInfos.GetEnumerable().Select((layer, index) => new { index, layer }))
63-
{
64-
var physicsLayer = layerIndex.layer;
65-
var i = layerIndex.index;
6666

67+
var i = 0;
68+
foreach(var physicsLayer in layerInfos.GetEnumerable().OrderBy(layer => layer.LayerID))
69+
{
6770
this.collInfosFolders[i] = EditorGUILayout.Foldout(this.collInfosFolders[i], "Collision : " + physicsLayer.LayerName, true);
6871
if(this.collInfosFolders[i] == false)
6972
{
73+
i++;
7074
continue;
7175
}
7276

@@ -75,46 +79,44 @@ private void DrawCollInfos(PhysicsLayerInfos layerInfos)
7579
EditorGUI.indentLevel += 1;
7680
using(var vert = new EditorGUILayout.VerticalScope())
7781
{
78-
EditorGUILayout.LabelField("Layers");
79-
for(var j = 0; j < LayersManager.UnityLayerCount; j++)
80-
{
81-
var layerColl = physicsLayer[j];
82-
if(layerColl == null)
83-
{
84-
continue;
85-
}
86-
87-
var layerID = layerColl.LayerID;
88-
collInfos[layerID] = this.DrawCollInfo(layerID, collInfos[layerID]);
89-
}
82+
var keys = collInfos.Keys.OrderBy(info => info);
83+
var unityKeys = keys.Where(key => key < LayersManager.UnityLayerCount);
84+
var physicsKeys = keys.Where(key => key >= LayersManager.UnityLayerCount);
9085

91-
for(var j = 0; j < i; j++)
86+
EditorGUILayout.LabelField("Layers");
87+
foreach(var key in unityKeys)
9288
{
93-
var layerID = j + LayersManager.UnityLayerCount;
94-
collInfos[layerID] = layerInfos[layerID][i + LayersManager.UnityLayerCount].Collision;
89+
collInfos[key] = this.DrawCollInfo(key, collInfos[key]);
9590
}
9691

9792
EditorGUILayout.Space();
9893
EditorGUILayout.LabelField("Physics Layers");
99-
for(var j = i; j < layerInfos.LayerCount; j++)
94+
foreach(var key in physicsKeys)
10095
{
101-
var layerID = j + LayersManager.UnityLayerCount;
102-
collInfos[layerID] = this.DrawCollInfo(layerID, collInfos[layerID]);
96+
if(key < physicsLayer.LayerID)
97+
{
98+
collInfos[key] = layerInfos[key][physicsLayer.LayerID].Collision;
99+
}
100+
else
101+
{
102+
collInfos[key] = this.DrawCollInfo(key, collInfos[key]);
103+
}
103104
}
104105
}
105106

106-
physicsLayer.UpdateLayerCollisions(collInfos);
107+
i++;
108+
physicsLayer.Update(collInfos);
107109
EditorGUI.indentLevel -= 1;
108110
}
109111

110112
EditorGUI.indentLevel -= 1;
111113
}
112-
113-
private bool DrawCollInfo(int layerID, bool collision)
114+
115+
private bool DrawCollInfo(LayerID layerID, bool collision)
114116
{
115117
using(var hori = new EditorGUILayout.HorizontalScope())
116118
{
117-
EditorGUILayout.LabelField(this.Target.LayerToName(layerID));
119+
EditorGUILayout.PrefixLabel(this.Target.LayerToName(layerID) + " : " + layerID.ID);
118120
return EditorGUILayout.Toggle(collision);
119121
}
120122
}

Assets/PhysicsLayers/Editor/LayersManagerInspector.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ public LayersManager Target
2323

2424
public override void OnInspectorGUI()
2525
{
26-
var target = this.Target.PhysicsLayerInfos;
26+
var physicsLayerInfos = this.Target.PhysicsLayerInfos;
2727

2828
using(var dirtyCheck = new EditorGUI.ChangeCheckScope())
2929
{
3030
this.layersFolder = EditorGUILayout.Foldout(this.layersFolder, "Physics Layers", true);
3131
if(this.layersFolder == true)
3232
{
33-
var physicsLayers = target.Layers;
33+
var physicsLayers = physicsLayerInfos.Layers;
3434
this.DrawPhysicsLayers(physicsLayers);
3535

3636
if(dirtyCheck.changed == true)
3737
{
3838
Undo.RecordObject(this.target, "Changed Physics Layers");
39-
this.UpdateLayer(target, physicsLayers);
39+
this.UpdateLayer(physicsLayerInfos, physicsLayers);
4040
this.SetDirty();
4141
}
4242
}
4343

44-
if(target.LayerCount <= 0)
44+
if(physicsLayerInfos.LayerCount <= 0)
4545
{
4646
return;
4747
}
@@ -51,7 +51,7 @@ public override void OnInspectorGUI()
5151
if(this.collInfosFolder == true)
5252
{
5353
Undo.RecordObject(this.target, "Changed Collision Infos");
54-
this.DrawCollInfos(target);
54+
this.DrawCollInfos(physicsLayerInfos);
5555
}
5656

5757
if(dirtyCheck.changed == true)
@@ -66,13 +66,13 @@ public override void OnInspectorGUI()
6666
EditorUtility.SetDirty(this.target);
6767
Undo.IncrementCurrentGroup();
6868
}
69-
70-
private void UpdateLayer(PhysicsLayerInfos target, Dictionary<int, string> physicsLayers)
69+
70+
private void UpdateLayer(PhysicsLayerInfos target, Dictionary<LayerID, string> physicsLayers)
7171
{
72-
target.UpdatePhysicsLayers(physicsLayers);
73-
74-
physicsLayers.AddRange(this.Target.UnityLayers);
75-
target.GetEnumerable().ToList().ForEach(infos => infos.UpdateLayerCollisions(physicsLayers.Keys.ToList()));
72+
target.Update(physicsLayers);
73+
74+
physicsLayers.AddRange(this.Target.UnityLayerInfos.Layers);
75+
target.GetEnumerable().ToList().ForEach(infos => infos.Update(physicsLayers.Keys.ToList()));
7676
}
7777
}
7878
}
13.2 KB
Binary file not shown.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
[Serializable]
10+
public abstract class AbstractLayerInfos<T> : ILayerInfos where T : ILayer
11+
{
12+
public T this[int layerID]
13+
{
14+
get { return this.layers.FirstOrDefault(layer => layer.LayerID == layerID); }
15+
}
16+
public T this[string layerName]
17+
{
18+
get { return this.layers.FirstOrDefault(layer => layer.LayerName == layerName); }
19+
}
20+
21+
public abstract int LayerCount { get; }
22+
public abstract Dictionary<LayerID, string> Layers { get; }
23+
public abstract List<LayerID> LayerIDs { get; }
24+
public abstract List<string> LayerNames { get; }
25+
26+
[SerializeField]
27+
protected List<T> layers = new List<T>();
28+
29+
30+
public virtual string LayerToName(int layerID)
31+
{
32+
var layer = this[layerID];
33+
return layer == null ? string.Empty : layer.LayerName;
34+
}
35+
36+
public virtual int NameToLayer(string layerName)
37+
{
38+
var layer = this[layerName];
39+
return layer == null ? -1 : layer.LayerID.ID;
40+
}
41+
42+
public virtual IEnumerable<T> GetEnumerable()
43+
{
44+
for(var i = 0; i < this.layers.Count; i++)
45+
{
46+
yield return this.layers[i];
47+
}
48+
}
49+
}
50+
}

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

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
public interface ILayer
10+
{
11+
LayerID LayerID { get; }
12+
string LayerName { get; }
13+
}
14+
}

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

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
public interface ILayerInfos
10+
{
11+
int LayerCount { get; }
12+
Dictionary<LayerID, string> Layers { get; }
13+
List<LayerID> LayerIDs { get; }
14+
List<string> LayerNames { get; }
15+
16+
string LayerToName(int layerID);
17+
int NameToLayer(string layerName);
18+
}
19+
}

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

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

0 commit comments

Comments
 (0)