Skip to content

Commit e728932

Browse files
Merge pull request #17 from unity-game-framework/issue-13-fix-double-enable-of-drawers-and-panels
Fix double enable of drawers and panels
2 parents 837decb + 91b17c2 commit e728932

21 files changed

+237
-164
lines changed

Assets/Scenes/Empty.unity

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ MonoBehaviour:
241241
m_Name:
242242
m_EditorClassIdentifier:
243243
m_enable: 1
244+
m_defaultShapes: 1
244245
m_defaultMaterial: {fileID: 0}
245246
m_shapes: []
246247
--- !u!1 &770230239
@@ -468,7 +469,7 @@ GameObject:
468469
m_Icon: {fileID: 7866945982896999795, guid: 0000000000000000d000000000000000, type: 0}
469470
m_NavMeshLayer: 0
470471
m_StaticEditorFlags: 0
471-
m_IsActive: 1
472+
m_IsActive: 0
472473
--- !u!114 &1531290047
473474
MonoBehaviour:
474475
m_ObjectHideFlags: 0
@@ -593,7 +594,7 @@ GameObject:
593594
m_Icon: {fileID: 4422084297763085224, guid: 0000000000000000d000000000000000, type: 0}
594595
m_NavMeshLayer: 0
595596
m_StaticEditorFlags: 0
596-
m_IsActive: 1
597+
m_IsActive: 0
597598
--- !u!4 &1823135812
598599
Transform:
599600
m_ObjectHideFlags: 0

Assets/UGF.DebugTools.Runtime.Tests/UGF.DebugTools.Runtime.Tests.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"references": [
55
"GUID:37dddc4e4de3de84f81310a1d1cd5e2c",
66
"GUID:088d00b6871540e44bce58af1a3f0f17",
7-
"GUID:6c7389a7d4bbed54e96eb1e71a69798e"
7+
"GUID:6c7389a7d4bbed54e96eb1e71a69798e",
8+
"GUID:d067af32d09350a4bb33960432735e4d"
89
],
910
"includePlatforms": [],
1011
"excludePlatforms": [],

Packages/UGF.DebugTools/Editor/DebugGLComponentEditor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ namespace UGF.DebugTools.Editor
99
internal class DebugGLComponentEditor : UnityEditor.Editor
1010
{
1111
private SerializedProperty m_propertyEnable;
12+
private SerializedProperty m_propertyDefaultShapes;
1213
private SerializedProperty m_propertyDefaultMaterial;
1314
private AssetReferenceListDrawer m_listShapes;
1415

1516
private void OnEnable()
1617
{
1718
m_propertyEnable = serializedObject.FindProperty("m_enable");
19+
m_propertyDefaultShapes = serializedObject.FindProperty("m_defaultShapes");
1820
m_propertyDefaultMaterial = serializedObject.FindProperty("m_defaultMaterial");
1921
m_listShapes = new AssetReferenceListDrawer(serializedObject.FindProperty("m_shapes"));
2022
m_listShapes.Enable();
@@ -30,6 +32,7 @@ public override void OnInspectorGUI()
3032
using (new SerializedObjectUpdateScope(serializedObject))
3133
{
3234
EditorGUILayout.PropertyField(m_propertyEnable);
35+
EditorGUILayout.PropertyField(m_propertyDefaultShapes);
3336
EditorGUILayout.PropertyField(m_propertyDefaultMaterial);
3437

3538
m_listShapes.DrawGUILayout();

Packages/UGF.DebugTools/Runtime/DebugGL.Shapes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void CylinderWire(Vector3 position, Quaternion rotation, Vector3 s
5454

5555
public static void Shape(string id, Vector3 position, Quaternion rotation, Vector3 scale, Color color)
5656
{
57-
Shape(id, position, rotation, scale, color, DefaultMaterial);
57+
Shape(id, position, rotation, scale, color, Drawer.DefaultMaterial);
5858
}
5959

6060
public static void Shape(string id, Vector3 position, Quaternion rotation, Vector3 scale, Color color, Material material)
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
11
using System;
2-
using UnityEngine;
32

43
namespace UGF.DebugTools.Runtime
54
{
65
public static partial class DebugGL
76
{
8-
public static DebugGLDrawer Drawer { get; } = new DebugGLDrawer();
9-
public static Material DefaultMaterial { get { return m_defaultMaterial != null ? m_defaultMaterial : throw new ArgumentException("Value not specified."); } }
10-
public static bool HasDefaultMaterial { get { return m_defaultMaterial != null; } }
7+
public static DebugGLDrawer Drawer { get { return m_drawer ?? throw new ArgumentException("Value not specified."); } }
8+
public static bool HasDrawer { get { return m_drawer != null; } }
119

12-
private static Material m_defaultMaterial;
10+
private static DebugGLDrawer m_drawer;
1311

14-
static DebugGL()
12+
public static void DrawerSet(DebugGLDrawer drawer)
1513
{
16-
Drawer.AddShape(ShapeLineWireId, DebugGLUtility.CreateShapeLineWire());
17-
Drawer.AddShape(ShapeTriangleWireId, DebugGLUtility.CreateShapeTriangleWire());
18-
Drawer.AddShape(ShapeQuadWireId, DebugGLUtility.CreateShapeQuadWire());
19-
Drawer.AddShape(ShapeCircleWireId, DebugGLUtility.CreateShapeCircleWire());
20-
Drawer.AddShape(ShapeCubeWireId, DebugGLUtility.CreateShapeCubeWire());
21-
Drawer.AddShape(ShapeSphereWireId, DebugGLUtility.CreateShapeSphereWire());
22-
Drawer.AddShape(ShapeCylinderWireId, DebugGLUtility.CreateShapeCylinderWire());
14+
m_drawer = drawer ?? throw new ArgumentNullException(nameof(drawer));
2315
}
2416

25-
public static void SetDefaultMaterial(Material material)
17+
public static void DrawerClear()
2618
{
27-
if (material == null) throw new ArgumentNullException(nameof(material));
28-
29-
m_defaultMaterial = material;
30-
}
31-
32-
public static void ClearDefaultMaterial()
33-
{
34-
m_defaultMaterial = null;
19+
m_drawer = null;
3520
}
3621
}
3722
}

Packages/UGF.DebugTools/Runtime/DebugGLComponent.cs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UGF.EditorTools.Runtime.IMGUI.AssetReferences;
45
using UnityEngine;
@@ -9,38 +10,72 @@ namespace UGF.DebugTools.Runtime
910
public class DebugGLComponent : MonoBehaviour
1011
{
1112
[SerializeField] private bool m_enable = true;
13+
[SerializeField] private bool m_defaultShapes = true;
1214
[SerializeField] private Material m_defaultMaterial;
1315
[SerializeField] private List<AssetReference<DebugGLShapeAsset>> m_shapes = new List<AssetReference<DebugGLShapeAsset>>();
1416

1517
public bool Enable { get { return m_enable; } set { m_enable = value; } }
18+
public bool DefaultShapes { get { return m_defaultShapes; } set { m_defaultShapes = value; } }
1619
public Material DefaultMaterial { get { return m_defaultMaterial; } set { m_defaultMaterial = value; } }
1720
public List<AssetReference<DebugGLShapeAsset>> Shapes { get { return m_shapes; } }
21+
public DebugGLDrawer Drawer { get { return m_drawer ?? throw new ArgumentException("Value not specified."); } }
22+
public bool HasDrawer { get { return m_drawer != null; } }
1823

24+
private DebugGLDrawer m_drawer;
25+
private readonly Camera.CameraCallback m_onPostRenderHandler;
1926
private static readonly WaitForEndOfFrame m_waitForEndOfFrame = new WaitForEndOfFrame();
20-
private static readonly Camera.CameraCallback m_onPostRenderHandler = OnRender;
27+
28+
public DebugGLComponent()
29+
{
30+
m_onPostRenderHandler = OnRender;
31+
}
2132

2233
private void Start()
2334
{
24-
DebugGL.Drawer.Enable = m_enable;
25-
DebugGL.SetDefaultMaterial(m_defaultMaterial ? m_defaultMaterial : DebugGLUtility.CreateDefaultMaterial());
35+
if (DebugGL.HasDrawer) throw new InvalidOperationException("Debug GL Drawer already specified.");
36+
37+
m_drawer = new DebugGLDrawer
38+
{
39+
Enable = m_enable
40+
};
41+
42+
m_drawer.SetDefaultMaterial(m_defaultMaterial ? m_defaultMaterial : DebugGLUtility.CreateDefaultMaterial());
43+
44+
if (m_defaultShapes)
45+
{
46+
m_drawer.AddShape(DebugGL.ShapeLineWireId, DebugGLUtility.CreateShapeLineWire());
47+
m_drawer.AddShape(DebugGL.ShapeTriangleWireId, DebugGLUtility.CreateShapeTriangleWire());
48+
m_drawer.AddShape(DebugGL.ShapeQuadWireId, DebugGLUtility.CreateShapeQuadWire());
49+
m_drawer.AddShape(DebugGL.ShapeCircleWireId, DebugGLUtility.CreateShapeCircleWire());
50+
m_drawer.AddShape(DebugGL.ShapeCubeWireId, DebugGLUtility.CreateShapeCubeWire());
51+
m_drawer.AddShape(DebugGL.ShapeSphereWireId, DebugGLUtility.CreateShapeSphereWire());
52+
m_drawer.AddShape(DebugGL.ShapeCylinderWireId, DebugGLUtility.CreateShapeCylinderWire());
53+
}
2654

2755
for (int i = 0; i < m_shapes.Count; i++)
2856
{
2957
AssetReference<DebugGLShapeAsset> reference = m_shapes[i];
3058

31-
DebugGL.Drawer.AddShape(reference.Guid, reference.Asset.Build());
59+
m_drawer.AddShape(reference.Guid, reference.Asset.Build());
3260
}
61+
62+
DebugGL.DrawerSet(m_drawer);
63+
64+
m_drawer.Initialize();
3365
}
3466

3567
private void OnDestroy()
3668
{
37-
DebugGL.ClearDefaultMaterial();
38-
39-
for (int i = 0; i < m_shapes.Count; i++)
69+
if (HasDrawer)
4070
{
41-
AssetReference<DebugGLShapeAsset> reference = m_shapes[i];
71+
m_drawer.Uninitialize();
72+
73+
if (DebugGL.HasDrawer && DebugGL.Drawer == m_drawer)
74+
{
75+
DebugGL.DrawerClear();
76+
}
4277

43-
DebugGL.Drawer.RemoveShape(reference.Guid);
78+
m_drawer = null;
4479
}
4580
}
4681

@@ -56,18 +91,21 @@ private void OnDisable()
5691
Camera.onPostRender -= m_onPostRenderHandler;
5792
}
5893

59-
private static void OnRender(Camera _)
94+
private void OnRender(Camera _)
6095
{
61-
DebugGL.Drawer.DrawGL();
96+
if (HasDrawer)
97+
{
98+
m_drawer.DrawGL();
99+
}
62100
}
63101

64102
private IEnumerator OnRenderEndRoutine()
65103
{
66-
while (enabled)
104+
while (enabled && HasDrawer)
67105
{
68106
yield return m_waitForEndOfFrame;
69107

70-
DebugGL.Drawer.ClearCommands();
108+
m_drawer.ClearCommands();
71109
}
72110
}
73111
}

Packages/UGF.DebugTools/Runtime/DebugGLDrawer.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using UGF.DebugTools.Runtime.GL.Scopes;
5+
using UGF.Initialize.Runtime;
56
using UnityEngine;
67

78
namespace UGF.DebugTools.Runtime
89
{
9-
public class DebugGLDrawer
10+
public class DebugGLDrawer : InitializeBase
1011
{
1112
public bool Enable { get; set; } = true;
1213
public IReadOnlyDictionary<string, DebugGLShape> Shapes { get; }
1314
public IReadOnlyList<DebugGLDrawCommand> Commands { get; }
15+
public Material DefaultMaterial { get { return m_defaultMaterial != null ? m_defaultMaterial : throw new ArgumentException("Value not specified."); } }
16+
public bool HasDefaultMaterial { get { return m_defaultMaterial != null; } }
1417

1518
private readonly Dictionary<string, DebugGLShape> m_shapes = new Dictionary<string, DebugGLShape>();
1619
private readonly List<DebugGLDrawCommand> m_commands = new List<DebugGLDrawCommand>();
20+
private Material m_defaultMaterial;
1721

1822
public DebugGLDrawer()
1923
{
@@ -36,6 +40,11 @@ public bool RemoveShape(string id)
3640
return m_shapes.Remove(id);
3741
}
3842

43+
public void ClearShapes()
44+
{
45+
m_shapes.Clear();
46+
}
47+
3948
public void AddCommand(DebugGLDrawCommand command)
4049
{
4150
if (!command.IsValid()) throw new ArgumentException("Value should be valid.", nameof(command));
@@ -94,5 +103,15 @@ public void DrawGLVertices(IReadOnlyList<Vector3> vertices, DebugGLMode mode, Ma
94103
}
95104
}
96105
}
106+
107+
public void SetDefaultMaterial(Material material)
108+
{
109+
m_defaultMaterial = material ? material : throw new ArgumentNullException(nameof(material));
110+
}
111+
112+
public void ClearDefaultMaterial()
113+
{
114+
m_defaultMaterial = null;
115+
}
97116
}
98117
}

Packages/UGF.DebugTools/Runtime/DebugUI.Menu.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ private static DebugUIMenuDrawer GetMenuDrawer()
119119
};
120120

121121
Drawer.Add(id, drawer);
122+
123+
drawer.Initialize();
122124
}
123125

124126
return drawer;

Packages/UGF.DebugTools/Runtime/DebugUI.Panel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ public static DebugUIPanelText PanelText(string text)
2525
public static void PanelAdd(DebugUIPanel panel)
2626
{
2727
Drawer.Get<DebugUIPanelDrawer>().Add(panel);
28+
29+
panel.Initialize();
2830
}
2931

3032
public static bool PanelRemove(DebugUIPanel panel)
3133
{
34+
panel.Uninitialize();
35+
3236
return Drawer.Get<DebugUIPanelDrawer>().Remove(panel);
3337
}
3438
}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
namespace UGF.DebugTools.Runtime
1+
using System;
2+
3+
namespace UGF.DebugTools.Runtime
24
{
35
public static partial class DebugUI
46
{
5-
public static DebugUIDrawer Drawer { get; } = new DebugUIDrawer();
7+
public static DebugUIDrawer Drawer { get { return m_drawer ?? throw new ArgumentException("Value not specified."); } }
8+
public static bool HasDrawer { get { return m_drawer != null; } }
9+
10+
private static DebugUIDrawer m_drawer;
11+
12+
public static void DrawerSet(DebugUIDrawer drawer)
13+
{
14+
m_drawer = drawer ?? throw new ArgumentNullException(nameof(drawer));
15+
}
16+
17+
public static void DrawerClear()
18+
{
19+
m_drawer = null;
20+
}
621
}
722
}

Packages/UGF.DebugTools/Runtime/DebugUIComponent.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,57 @@ public class DebugUIComponent : MonoBehaviour
1717
public float Scale { get { return m_scale; } set { m_scale = value; } }
1818
public GUISkin Skin { get { return m_skin; } set { m_skin = value; } }
1919
public List<AssetReference<DebugUIDrawerAsset>> Drawers { get { return m_drawers; } }
20+
public DebugUIDrawer Drawer { get { return m_drawer ?? throw new ArgumentException("Value not specified."); } }
21+
public bool HasDrawer { get { return m_drawer != null; } }
22+
23+
private DebugUIDrawer m_drawer;
2024

2125
private void Start()
2226
{
23-
DebugUI.Drawer.Enable = m_enable;
24-
DebugUI.Drawer.Scale = Vector2.one * m_scale;
27+
if (DebugUI.HasDrawer) throw new InvalidOperationException("Debug UI Drawer already specified.");
28+
29+
m_drawer = new DebugUIDrawer
30+
{
31+
Enable = m_enable,
32+
Scale = Vector2.one * m_scale
33+
};
2534

2635
if (m_skin != null)
2736
{
28-
DebugUI.Drawer.SetSkin(m_skin);
37+
m_drawer.SetSkin(m_skin);
2938
}
3039

3140
for (int i = 0; i < m_drawers.Count; i++)
3241
{
3342
AssetReference<DebugUIDrawerAsset> reference = m_drawers[i];
3443

35-
DebugUI.Drawer.Add(reference.Guid, reference.Asset.Build());
44+
m_drawer.Add(reference.Guid, reference.Asset.Build());
3645
}
46+
47+
DebugUI.DrawerSet(m_drawer);
48+
49+
m_drawer.Initialize();
3750
}
3851

3952
private void OnDestroy()
4053
{
41-
for (int i = m_drawers.Count - 1; i >= 0; i--)
54+
if (HasDrawer)
4255
{
43-
AssetReference<DebugUIDrawerAsset> reference = m_drawers[i];
56+
m_drawer.Uninitialize();
4457

45-
DebugUI.Drawer.Remove(reference.Guid);
58+
if (DebugUI.HasDrawer && DebugUI.Drawer == m_drawer)
59+
{
60+
DebugUI.DrawerClear();
61+
}
62+
63+
m_drawer = null;
4664
}
4765
}
4866

4967
private void OnGUI()
5068
{
51-
DebugUI.Drawer.DrawGUI();
69+
m_drawer.DrawGUI();
70+
5271
DebugUIContentCache.Reset();
5372
}
5473
}

0 commit comments

Comments
 (0)