Chocolade19721309

Untitled

May 17th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. private void ImportHierarchyFromJson()
  2. {
  3. string importPath = $"{customOutputPath}.json";
  4.  
  5. if (!File.Exists(importPath))
  6. {
  7. Debug.LogError("JSON file not found at the specified path.");
  8. return;
  9. }
  10.  
  11. string jsonContent = File.ReadAllText(importPath);
  12. var rootObjectData = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonContent);
  13.  
  14. if (rootObjectData != null)
  15. {
  16. foreach (var objectData in rootObjectData)
  17. {
  18. CreateGameObjectFromJson(objectData, null);
  19. }
  20. }
  21.  
  22. AssetDatabase.Refresh();
  23. }
  24.  
  25. private void CreateGameObjectFromJson(Dictionary<string, object> objectData, Transform parent)
  26. {
  27. if (!objectData.ContainsKey("Name"))
  28. {
  29. Debug.LogError("Object data does not contain 'Name' key.");
  30. return;
  31. }
  32.  
  33. string name = objectData["Name"].ToString();
  34. GameObject newObject = new GameObject(name);
  35. Debug.Log($"Created GameObject: {name}");
  36.  
  37. if (parent != null)
  38. {
  39. newObject.transform.SetParent(parent);
  40. }
  41.  
  42. if (objectData.ContainsKey("Components"))
  43. {
  44. var components = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(objectData["Components"].ToString());
  45. foreach (var componentData in components)
  46. {
  47. if (componentData != null && componentData.ContainsKey("Type"))
  48. {
  49. string componentName = componentData["Type"].ToString();
  50. Component component = newObject.AddComponent(System.Type.GetType(componentName));
  51. Debug.Log($"Added Component: {componentName} to {name}");
  52.  
  53. if (component != null)
  54. {
  55. SerializedObject serializedObject = new SerializedObject(component);
  56. foreach (var prop in componentData)
  57. {
  58. if (prop.Key == "Type")
  59. continue;
  60.  
  61. SerializedProperty serializedProperty = serializedObject.FindProperty(prop.Key);
  62. if (serializedProperty != null)
  63. {
  64. SetPropertyValue(serializedProperty, prop.Value.ToString());
  65. }
  66. else
  67. {
  68. Debug.LogWarning($"Property '{prop.Key}' not found in component '{componentName}'");
  69. }
  70. }
  71. serializedObject.ApplyModifiedProperties();
  72. }
  73. else
  74. {
  75. Debug.LogError($"Failed to add component '{componentName}' to GameObject '{name}'");
  76. }
  77. }
  78. }
  79. }
  80. else
  81. {
  82. Debug.LogError($"Object '{name}' does not contain 'Components' key or 'Components' is not a list.");
  83. }
  84.  
  85. if (objectData.ContainsKey("Children"))
  86. {
  87. var children = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(objectData["Children"].ToString());
  88. foreach (var childData in children)
  89. {
  90. if (childData != null)
  91. {
  92. CreateGameObjectFromJson(childData, newObject.transform);
  93. }
  94. }
  95. }
  96. }
  97.  
  98. private void SetPropertyValue(SerializedProperty prop, string value)
  99. {
  100. try
  101. {
  102. switch (prop.propertyType)
  103. {
  104. case SerializedPropertyType.Integer:
  105. prop.intValue = int.Parse(value);
  106. break;
  107. case SerializedPropertyType.Boolean:
  108. prop.boolValue = bool.Parse(value);
  109. break;
  110. case SerializedPropertyType.Float:
  111. prop.floatValue = float.Parse(value);
  112. break;
  113. case SerializedPropertyType.String:
  114. prop.stringValue = value;
  115. break;
  116. case SerializedPropertyType.Color:
  117. prop.colorValue = ColorUtility.TryParseHtmlString(value, out Color color) ? color : Color.white;
  118. break;
  119. case SerializedPropertyType.ObjectReference:
  120. // This is a simplification. Ideally, you'd resolve the reference properly.
  121. prop.objectReferenceValue = null;
  122. break;
  123. case SerializedPropertyType.Enum:
  124. prop.enumValueIndex = prop.enumNames.ToList().IndexOf(value);
  125. break;
  126. case SerializedPropertyType.Vector2:
  127. prop.vector2Value = JsonConvert.DeserializeObject<Vector2>(value);
  128. break;
  129. case SerializedPropertyType.Vector3:
  130. prop.vector3Value = JsonConvert.DeserializeObject<Vector3>(value);
  131. break;
  132. case SerializedPropertyType.Vector4:
  133. prop.vector4Value = JsonConvert.DeserializeObject<Vector4>(value);
  134. break;
  135. case SerializedPropertyType.Rect:
  136. prop.rectValue = JsonConvert.DeserializeObject<Rect>(value);
  137. break;
  138. case SerializedPropertyType.Bounds:
  139. prop.boundsValue = JsonConvert.DeserializeObject<Bounds>(value);
  140. break;
  141. case SerializedPropertyType.Quaternion:
  142. prop.quaternionValue = JsonConvert.DeserializeObject<Quaternion>(value);
  143. break;
  144. case SerializedPropertyType.Vector2Int:
  145. prop.vector2IntValue = JsonConvert.DeserializeObject<Vector2Int>(value);
  146. break;
  147. case SerializedPropertyType.Vector3Int:
  148. prop.vector3IntValue = JsonConvert.DeserializeObject<Vector3Int>(value);
  149. break;
  150. case SerializedPropertyType.RectInt:
  151. prop.rectIntValue = JsonConvert.DeserializeObject<RectInt>(value);
  152. break;
  153. case SerializedPropertyType.BoundsInt:
  154. prop.boundsIntValue = JsonConvert.DeserializeObject<BoundsInt>(value);
  155. break;
  156. default:
  157. Debug.LogWarning($"Unsupported property type: {prop.propertyType}");
  158. break;
  159. }
  160. }
  161. catch (System.Exception ex)
  162. {
  163. Debug.LogError($"Failed to set property value: {ex.Message}");
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment