Skip to content

Commit 36489ff

Browse files
authored
Merge pull request #1 from OpenSourceUnityPackage/Refactoring
Refactoring of UtilitySystem
2 parents 8be48f1 + e6a10ca commit 36489ff

21 files changed

+757
-163
lines changed

Editor/UtilitySystemDataEditor.cs

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Mime;
5+
using UnityEditor;
6+
using UnityEngine;
7+
using UtilitySystem.Runtime;
8+
9+
namespace UtilitySystem.Editor
10+
{
11+
[CustomEditor(typeof(UtilitySystemData))]
12+
public class UtilitySystemDataEditor : UnityEditor.Editor
13+
{
14+
private static UtilitySystemData _utilitySystemData;
15+
private static SerializedProperty InputsProperty;
16+
private static SerializedProperty OutputsProperty;
17+
18+
private void OnEnable()
19+
{
20+
_utilitySystemData = target as UtilitySystemData;
21+
InputsProperty = serializedObject.FindProperty("inputs");
22+
OutputsProperty = serializedObject.FindProperty("utilities");
23+
}
24+
25+
public override void OnInspectorGUI()
26+
{
27+
// base.OnInspectorGUI();
28+
29+
serializedObject.Update();
30+
31+
EditorGUILayout.PropertyField(InputsProperty);
32+
33+
ShowOutputs();
34+
35+
serializedObject.ApplyModifiedProperties();
36+
}
37+
38+
private void ShowOutputs()
39+
{
40+
EditorGUILayout.BeginHorizontal();
41+
OutputsProperty.isExpanded =
42+
EditorGUILayout.Foldout(OutputsProperty.isExpanded, OutputsProperty.displayName);
43+
44+
// TODO: Use default constructor
45+
if (FitButton("Add Utility"))
46+
{
47+
OutputsProperty.InsertArrayElementAtIndex(OutputsProperty.arraySize);
48+
OutputsProperty.GetArrayElementAtIndex(OutputsProperty.arraySize - 1).isExpanded = true;
49+
}
50+
51+
EditorGUILayout.EndHorizontal();
52+
if (OutputsProperty.isExpanded)
53+
{
54+
EditorGUI.indentLevel += 1;
55+
for (int i = 0; i < OutputsProperty.arraySize; i++)
56+
{
57+
SerializedProperty outputProperty = OutputsProperty.GetArrayElementAtIndex(i);
58+
SerializedProperty statProperties = outputProperty.FindPropertyRelative("statImportances");
59+
60+
EditorGUILayout.BeginHorizontal();
61+
62+
outputProperty.isExpanded =
63+
EditorGUILayout.Foldout(outputProperty.isExpanded, outputProperty.displayName);
64+
if (FitButton("Remove Utility"))
65+
{
66+
OutputsProperty.DeleteArrayElementAtIndex(i);
67+
break;
68+
}
69+
70+
// TODO: Use default constructor
71+
if (statProperties.arraySize < InputsProperty.arraySize && FitButton("Add Stat Importance") )
72+
{
73+
outputProperty.isExpanded = true;
74+
statProperties.InsertArrayElementAtIndex(statProperties.arraySize);
75+
statProperties.isExpanded = true;
76+
statProperties.GetArrayElementAtIndex(statProperties.arraySize - 1).isExpanded = true;
77+
}
78+
79+
EditorGUILayout.EndHorizontal();
80+
81+
if (outputProperty.isExpanded)
82+
{
83+
EditorGUI.indentLevel += 1;
84+
85+
EditorGUILayout.PropertyField(outputProperty.FindPropertyRelative("Name"));
86+
87+
EditorGUILayout.BeginHorizontal();
88+
89+
statProperties.isExpanded =
90+
EditorGUILayout.Foldout(statProperties.isExpanded, statProperties.displayName);
91+
92+
EditorGUILayout.EndHorizontal();
93+
94+
if (statProperties.isExpanded)
95+
{
96+
EditorGUI.indentLevel += 1;
97+
98+
for (int j = 0; j < statProperties.arraySize; j++)
99+
{
100+
SerializedProperty statProperty = statProperties.GetArrayElementAtIndex(j);
101+
102+
EditorGUILayout.BeginHorizontal();
103+
104+
statProperty.isExpanded =
105+
EditorGUILayout.Foldout(statProperty.isExpanded, statProperty.displayName);
106+
107+
if (FitButton("Remove Stat Importance"))
108+
{
109+
statProperties.DeleteArrayElementAtIndex(j);
110+
break;
111+
}
112+
113+
EditorGUILayout.EndHorizontal();
114+
115+
if (statProperty.isExpanded)
116+
{
117+
SerializedProperty statName = statProperty.FindPropertyRelative("name");
118+
119+
List<string> selectable = _utilitySystemData.inputs.Where(s =>
120+
{
121+
for (int k = 0; k < statProperties.arraySize; k++)
122+
{
123+
if (statProperties.GetArrayElementAtIndex(k)
124+
.FindPropertyRelative("name").stringValue == s && k != j)
125+
{
126+
return false;
127+
}
128+
}
129+
130+
return true;
131+
}).ToList();
132+
133+
int current = selectable.FindIndex(value =>
134+
statName.stringValue == value);
135+
136+
int selected = EditorGUILayout.Popup(current >= 0 ? current : 0, selectable.ToArray());
137+
138+
if (current != selected)
139+
{
140+
statProperty.FindPropertyRelative("name").stringValue =
141+
selected >= 0 ? selectable[selected] : "";
142+
}
143+
144+
EditorGUILayout.PropertyField(statProperty.FindPropertyRelative("curve"),
145+
new GUIContent("Importance Curve"));
146+
147+
SerializedProperty statWeight = statProperty.FindPropertyRelative("weight");
148+
149+
statWeight.intValue = EditorGUILayout.IntSlider(statWeight.displayName, statWeight.intValue, 0, 10);
150+
}
151+
}
152+
153+
EditorGUI.indentLevel -= 1;
154+
}
155+
156+
EditorGUI.indentLevel -= 1;
157+
}
158+
}
159+
160+
EditorGUI.indentLevel -= 1;
161+
}
162+
}
163+
164+
private static Vector2 ComputeTextWidth(String text)
165+
{
166+
return GUI.skin.button.CalcSize(new GUIContent(text));
167+
}
168+
169+
private static bool FitButton(string text)
170+
{
171+
return GUILayout.Button(text, EditorStyles.miniButton, GUILayout.Width(ComputeTextWidth(text).x));
172+
}
173+
}
174+
}

Editor/UtilitySystemDataEditor.cs.meta

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

Runtime/Blackboard.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

Runtime/Desire.cs

Lines changed: 0 additions & 83 deletions
This file was deleted.

Runtime/Desire.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Runtime/Stat.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,33 @@
33
using UnityEngine;
44
using UnityEngine.Events;
55

6-
namespace UtilitySystem
6+
namespace UtilitySystem.Runtime
77
{
8-
98
[System.Serializable]
10-
public class Stat : MonoBehaviour
9+
public class Stat
1110
{
12-
[SerializeField]
13-
private float ratio;
14-
public float Ratio
11+
public string Name;
12+
13+
[SerializeField]
14+
[Range(0f, 1f)]
15+
private float value;
16+
17+
public float Value
1518
{
16-
get { return ratio; }
17-
set
18-
{
19-
ratio = Mathf.Clamp(value, 0, 1);
20-
}
19+
get { return value; }
20+
set { this.value = Mathf.Clamp(value, 0, 1); }
21+
}
22+
23+
public Stat(string name, float value)
24+
{
25+
Name = name;
26+
Value = value;
2127
}
22-
};
2328

29+
public Stat(Utility utility)
30+
{
31+
Name = utility.Name;
32+
Value = utility.Value;
33+
}
34+
};
2435
}

Runtime/StatImportance.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityEngine;
2+
3+
namespace UtilitySystem.Runtime
4+
{
5+
[System.Serializable]
6+
public class StatImportance
7+
{
8+
public string name;
9+
[SerializeField] public AnimationCurve curve;
10+
public int weight = 1;
11+
}
12+
}

Runtime/StatImportance.cs.meta

Lines changed: 3 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)