This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ResourcePoolEditor.cs
104 lines (92 loc) · 3.42 KB
/
ResourcePoolEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Archon.SwissArmyLib.ResourceSystem;
using UnityEditor;
using UnityEngine;
namespace Archon.SwissArmyLib.Editor.ResourceSystem
{
/// <summary>
/// Custom editor for <see cref="ResourcePoolBase"/> components.
///
/// Shows a health bar and debugging buttons.
/// </summary>
[CanEditMultipleObjects]
[CustomEditor(typeof(ResourcePoolBase), true)]
public class ResourcePoolEditor : UnityEditor.Editor
{
private float _addVal = 50;
private float _removeVal = 50;
/// <inheritdoc />
public override void OnInspectorGUI()
{
if (targets.Length == 1)
{
var resourcePool = (ResourcePoolBase)target;
EditorGUILayout.Separator();
var containerRect = EditorGUILayout.BeginHorizontal();
var barRect = GUILayoutUtility.GetRect(containerRect.width, 20);
EditorGUI.ProgressBar(barRect, resourcePool.Percentage, string.Format("{0:F1} / {1:F1}", resourcePool.Current, resourcePool.Max));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Separator();
}
DrawDefaultInspector();
if (Application.isPlaying)
{
EditorGUILayout.Separator();
EditorGUILayout.BeginHorizontal();
_addVal = EditorGUILayout.FloatField(_addVal);
if (GUILayout.Button("Add")) Add(_addVal);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
_removeVal = EditorGUILayout.FloatField(_removeVal);
if (GUILayout.Button("Remove")) Remove(_removeVal);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Empty")) Empty();
if (GUILayout.Button("Fill")) Fill();
if (GUILayout.Button("Renew")) Renew();
EditorGUILayout.EndHorizontal();
}
}
/// <summary>
/// Adds a resource amount to all targeted components.
/// </summary>
/// <param name="amount">Amount to add</param>
protected void Add(float amount)
{
for (var i = 0; i < targets.Length; i++)
((ResourcePoolBase)targets[i]).Add(amount);
}
/// <summary>
/// Removes a resource amount from all targeted components.
/// </summary>
/// <param name="amount">Amount to remove</param>
protected void Remove(float amount)
{
for (var i = 0; i < targets.Length; i++)
((ResourcePoolBase)targets[i]).Remove(amount);
}
/// <summary>
/// Empties all targeted components.
/// </summary>
protected void Empty()
{
for (var i = 0; i < targets.Length; i++)
((ResourcePoolBase)targets[i]).Empty();
}
/// <summary>
/// Fills all targeted components.
/// </summary>
protected void Fill()
{
for (var i = 0; i < targets.Length; i++)
((ResourcePoolBase) targets[i]).Fill();
}
/// <summary>
/// Renews all targeted components.
/// </summary>
protected void Renew()
{
for (var i = 0; i < targets.Length; i++)
((ResourcePoolBase)targets[i]).Renew();
}
}
}