Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ImportHierarchyFromJson()
- {
- string importPath = $"{customOutputPath}.json";
- if (!File.Exists(importPath))
- {
- Debug.LogError("JSON file not found at the specified path.");
- return;
- }
- string jsonContent = File.ReadAllText(importPath);
- var rootObjectData = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonContent);
- if (rootObjectData != null)
- {
- foreach (var objectData in rootObjectData)
- {
- CreateGameObjectFromJson(objectData, null);
- }
- }
- AssetDatabase.Refresh();
- }
- private void CreateGameObjectFromJson(Dictionary<string, object> objectData, Transform parent)
- {
- if (!objectData.ContainsKey("Name"))
- {
- Debug.LogError("Object data does not contain 'Name' key.");
- return;
- }
- string name = objectData["Name"].ToString();
- GameObject newObject = new GameObject(name);
- Debug.Log($"Created GameObject: {name}");
- if (parent != null)
- {
- newObject.transform.SetParent(parent);
- }
- if (objectData.ContainsKey("Components"))
- {
- var components = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(objectData["Components"].ToString());
- foreach (var componentData in components)
- {
- if (componentData != null && componentData.ContainsKey("Type"))
- {
- string componentName = componentData["Type"].ToString();
- Component component = newObject.AddComponent(System.Type.GetType(componentName));
- Debug.Log($"Added Component: {componentName} to {name}");
- if (component != null)
- {
- SerializedObject serializedObject = new SerializedObject(component);
- foreach (var prop in componentData)
- {
- if (prop.Key == "Type")
- continue;
- SerializedProperty serializedProperty = serializedObject.FindProperty(prop.Key);
- if (serializedProperty != null)
- {
- SetPropertyValue(serializedProperty, prop.Value.ToString());
- }
- else
- {
- Debug.LogWarning($"Property '{prop.Key}' not found in component '{componentName}'");
- }
- }
- serializedObject.ApplyModifiedProperties();
- }
- else
- {
- Debug.LogError($"Failed to add component '{componentName}' to GameObject '{name}'");
- }
- }
- }
- }
- else
- {
- Debug.LogError($"Object '{name}' does not contain 'Components' key or 'Components' is not a list.");
- }
- if (objectData.ContainsKey("Children"))
- {
- var children = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(objectData["Children"].ToString());
- foreach (var childData in children)
- {
- if (childData != null)
- {
- CreateGameObjectFromJson(childData, newObject.transform);
- }
- }
- }
- }
- private void SetPropertyValue(SerializedProperty prop, string value)
- {
- try
- {
- switch (prop.propertyType)
- {
- case SerializedPropertyType.Integer:
- prop.intValue = int.Parse(value);
- break;
- case SerializedPropertyType.Boolean:
- prop.boolValue = bool.Parse(value);
- break;
- case SerializedPropertyType.Float:
- prop.floatValue = float.Parse(value);
- break;
- case SerializedPropertyType.String:
- prop.stringValue = value;
- break;
- case SerializedPropertyType.Color:
- prop.colorValue = ColorUtility.TryParseHtmlString(value, out Color color) ? color : Color.white;
- break;
- case SerializedPropertyType.ObjectReference:
- // This is a simplification. Ideally, you'd resolve the reference properly.
- prop.objectReferenceValue = null;
- break;
- case SerializedPropertyType.Enum:
- prop.enumValueIndex = prop.enumNames.ToList().IndexOf(value);
- break;
- case SerializedPropertyType.Vector2:
- prop.vector2Value = JsonConvert.DeserializeObject<Vector2>(value);
- break;
- case SerializedPropertyType.Vector3:
- prop.vector3Value = JsonConvert.DeserializeObject<Vector3>(value);
- break;
- case SerializedPropertyType.Vector4:
- prop.vector4Value = JsonConvert.DeserializeObject<Vector4>(value);
- break;
- case SerializedPropertyType.Rect:
- prop.rectValue = JsonConvert.DeserializeObject<Rect>(value);
- break;
- case SerializedPropertyType.Bounds:
- prop.boundsValue = JsonConvert.DeserializeObject<Bounds>(value);
- break;
- case SerializedPropertyType.Quaternion:
- prop.quaternionValue = JsonConvert.DeserializeObject<Quaternion>(value);
- break;
- case SerializedPropertyType.Vector2Int:
- prop.vector2IntValue = JsonConvert.DeserializeObject<Vector2Int>(value);
- break;
- case SerializedPropertyType.Vector3Int:
- prop.vector3IntValue = JsonConvert.DeserializeObject<Vector3Int>(value);
- break;
- case SerializedPropertyType.RectInt:
- prop.rectIntValue = JsonConvert.DeserializeObject<RectInt>(value);
- break;
- case SerializedPropertyType.BoundsInt:
- prop.boundsIntValue = JsonConvert.DeserializeObject<BoundsInt>(value);
- break;
- default:
- Debug.LogWarning($"Unsupported property type: {prop.propertyType}");
- break;
- }
- }
- catch (System.Exception ex)
- {
- Debug.LogError($"Failed to set property value: {ex.Message}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment