Skip to content

Commit 943000c

Browse files
author
Benoit Hudson
committed
Add ability to store the component values.
Allow to copy-paste the debug info in the prefab inspector. Store the component values. Parse the component values back out. TODO: do something with the component values!
1 parent 1aef756 commit 943000c

File tree

2 files changed

+124
-23
lines changed

2 files changed

+124
-23
lines changed

Assets/FbxExporters/Editor/FbxPrefabInspector.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ public override void OnInspectorGUI() {
3232
fbxPrefab.SetSourceModel(newFbxAsset);
3333
}
3434

35+
EditorGUI.EndDisabledGroup();
36+
3537
#if FBXEXPORTER_DEBUG
3638
GUILayout.BeginHorizontal();
3739
GUILayout.Label ("Debug info:");
38-
EditorGUILayout.SelectableLabel(fbxPrefab.GetFbxHistory().ToJson());
40+
try {
41+
fbxPrefab.GetFbxHistory().ToJson();
42+
} catch(System.Exception xcp) {
43+
Debug.LogException(xcp);
44+
}
45+
EditorGUILayout.SelectableLabel(fbxPrefab.GetFbxHistoryString());
3946
GUILayout.EndHorizontal();
4047
#endif
41-
42-
EditorGUI.EndDisabledGroup();
4348
}
4449
}
4550
}

Assets/FbxExporters/FbxPrefab.cs

Lines changed: 116 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,38 @@ public class FbxPrefab : MonoBehaviour
4545
#if UNITY_EDITOR
4646
public class FbxRepresentation
4747
{
48-
public Dictionary<string, FbxRepresentation> c;
48+
/// <summary>
49+
/// Children of this node.
50+
/// The key is the name, which is assumed to be unique.
51+
/// The value is, recursively, the representation of that subtree.
52+
/// </summary>
53+
Dictionary<string, FbxRepresentation> children;
54+
55+
/// <summary>
56+
/// Children of this node.
57+
/// The key is the name of the type of the Component. We accept that there may be several.
58+
/// The value is the json for the component, to be decoded with EditorJsonUtility.
59+
/// </summary>
60+
Dictionary<string, List<string>> components;
4961

5062
public static FbxRepresentation FromTransform(Transform xfo) {
51-
var c = new Dictionary<string, FbxRepresentation>();
63+
var children = new Dictionary<string, FbxRepresentation>();
5264
foreach(Transform child in xfo) {
53-
c.Add(child.name, FromTransform(child));
65+
children.Add(child.name, FromTransform(child));
66+
}
67+
var components = new Dictionary<string, List<string>>();
68+
foreach(var component in xfo.GetComponents<Component>()) {
69+
var typeName = component.GetType().ToString();
70+
List<string> comps;
71+
if (!components.TryGetValue(typeName, out comps)) {
72+
comps = new List<string>();
73+
components[typeName] = comps;
74+
}
75+
comps.Add(UnityEditor.EditorJsonUtility.ToJson(component));
5476
}
5577
var fbxrep = new FbxRepresentation();
56-
fbxrep.c = c;
78+
fbxrep.children = children;
79+
fbxrep.components = components;
5780
return fbxrep;
5881
}
5982

@@ -78,50 +101,115 @@ static bool Consume(char expected, string json, ref int index, bool required = t
78101
}
79102
}
80103

104+
// %-escape a string to make sure that " and \ are set up to be easy to parse
105+
static string EscapeString(string str) {
106+
var builder = new System.Text.StringBuilder();
107+
foreach(var c in str) {
108+
switch(c) {
109+
case '%': builder.Append("%25"); break;
110+
case '\\': builder.Append("%5c"); break;
111+
case '"': builder.Append("%22"); break;
112+
default: builder.Append(c); break;
113+
}
114+
}
115+
return builder.ToString();
116+
}
117+
118+
static string UnEscapeString(string str, int index, int len) {
119+
var builder = new System.Text.StringBuilder();
120+
for(int i = index; i < index + len; ++i) {
121+
if (str[i] != '%') {
122+
builder.Append(str[i]);
123+
} else {
124+
string number = str.Substring(i + 1, 2);
125+
int ord = System.Convert.ToInt32(number, 16);
126+
builder.Append( (char) ord );
127+
}
128+
}
129+
return builder.ToString();
130+
}
131+
81132
static FbxRepresentation FromJsonHelper(string json, ref int index) {
82133
Consume('{', json, ref index);
83134
var fbxrep = new FbxRepresentation();
84135
if (Consume('}', json, ref index, required: false)) {
85136
// this is a leaf; we're done.
86137
} else {
87-
// this is a parent of one or more objects; store them.
88-
fbxrep.c = new Dictionary<string, FbxRepresentation>();
138+
// this is a node with important data
139+
fbxrep.children = new Dictionary<string, FbxRepresentation>();
140+
fbxrep.components = new Dictionary<string, List<string>>();
141+
89142
do {
90143
Consume('"', json, ref index);
91144
int nameStart = index;
92145
while (json[index] != '"') { index++; }
93146
string name = json.Substring(nameStart, index - nameStart);
94147
index++;
95148
Consume(':', json, ref index);
96-
var subrep = FromJsonHelper(json, ref index);
97-
fbxrep.c.Add(name, subrep);
149+
// if name starts with - it's a child; otherwise it's a
150+
// component (which can't start with a - because it's
151+
// the name of a C# type)
152+
if (name[0] == '-') {
153+
var subrep = FromJsonHelper(json, ref index);
154+
fbxrep.children.Add(name.Substring(1), subrep);
155+
} else {
156+
// Read the string. It won't have any quote marks
157+
// in it, because we escape them using %-encoding
158+
// like in a URL.
159+
Consume('"', json, ref index);
160+
int componentStart = index;
161+
while (json[index] != '"') { index++; }
162+
index++;
163+
164+
// We %-escaped the string so there would be no
165+
// quotes (nor backslashes that might confuse other
166+
// json parsers), now undo that.
167+
List<string> comps;
168+
if (!fbxrep.components.TryGetValue(name, out comps)) {
169+
comps = new List<string>();
170+
fbxrep.components[name] = comps;
171+
}
172+
comps.Add(UnEscapeString(json, componentStart, index - componentStart));
173+
}
98174
} while(Consume(',', json, ref index, required: false));
99175
Consume('}', json, ref index);
100176
}
101177
return fbxrep;
102178
}
103179

104180
public static FbxRepresentation FromJson(string json) {
105-
if (json.Length == 0) { return null; }
181+
if (json.Length == 0) { return new FbxRepresentation(); }
106182
int index = 0;
107183
return FromJsonHelper(json, ref index);
108184
}
109185

110186
void ToJsonHelper(System.Text.StringBuilder builder) {
111187
builder.Append("{");
112-
if (c != null) {
113-
bool first = true;
114-
foreach(var kvp in c.OrderBy(kvp => kvp.Key)) {
188+
bool first = true;
189+
if (children != null) {
190+
foreach(var kvp in children.OrderBy(kvp => kvp.Key)) {
115191
if (!first) { builder.Append(','); }
116192
else { first = false; }
117193

118-
builder.Append('"');
119-
builder.Append(kvp.Key); // the name
120-
builder.Append('"');
121-
builder.Append(':');
194+
// print names with a '-' in front
195+
builder.AppendFormat("\"-{0}\":", kvp.Key);
122196
kvp.Value.ToJsonHelper(builder);
123197
}
124198
}
199+
if (components != null) {
200+
foreach(var kvp in components.OrderBy(kvp => kvp.Key)) {
201+
var name = kvp.Key;
202+
foreach(var componentValue in kvp.Value) {
203+
if (!first) { builder.Append(','); }
204+
else { first = false; }
205+
206+
// print component name and value, but escape the value
207+
// string to make sure we can parse it later
208+
builder.AppendFormat("\"{0}\": \"{1}\"", name,
209+
EscapeString(componentValue));
210+
}
211+
}
212+
}
125213
builder.Append("}");
126214
}
127215

@@ -132,22 +220,22 @@ public string ToJson() {
132220
}
133221

134222
public static bool IsLeaf(FbxRepresentation rep) {
135-
return rep == null || rep.c == null;
223+
return rep == null || rep.children == null;
136224
}
137225

138226
public static HashSet<string> CopyKeySet(FbxRepresentation rep) {
139227
if (IsLeaf(rep)) {
140228
return new HashSet<string>();
141229
} else {
142-
return new HashSet<string>(rep.c.Keys);
230+
return new HashSet<string>(rep.children.Keys);
143231
}
144232
}
145233

146234
public static FbxRepresentation Find(FbxRepresentation rep, string key) {
147235
if (IsLeaf(rep)) { return null; }
148236

149237
FbxRepresentation child;
150-
if (rep.c.TryGetValue(key, out child)) {
238+
if (rep.children.TryGetValue(key, out child)) {
151239
return child;
152240
}
153241
return null;
@@ -164,7 +252,6 @@ public static FbxRepresentation Find(FbxRepresentation rep, string key) {
164252
/// </summary>
165253
public class UpdateList
166254
{
167-
168255
// We build up a flat list of names for the nodes of the old fbx,
169256
// the new fbx, and the prefab. We also figure out the parents.
170257
class Data {
@@ -502,6 +589,15 @@ public FbxRepresentation GetFbxHistory()
502589
return FbxRepresentation.FromJson(m_fbxHistory);
503590
}
504591

592+
/// <summary>
593+
/// Returns the string representation of the fbx file as it was last time we sync'd.
594+
/// Really just for debugging.
595+
/// </summary>
596+
public string GetFbxHistoryString()
597+
{
598+
return m_fbxHistory;
599+
}
600+
505601
/// <summary>
506602
/// Sync the prefab to match the FBX file.
507603
/// </summary>

0 commit comments

Comments
 (0)