//TODO Make Cleaner public static void SaveSelectedNode() { if (!instance.selectedNode) { return; } var fixedChildren = new List <Transform> (); GetOnlyChildren(instance.selectedNode, fixedChildren); AssetNodeData rootNode = new AssetNodeData("PlacementAssets", Vector3.zero); foreach (var subRoot in fixedChildren) { var subFixedChildren = new List <Transform> (); GetOnlyChildren(subRoot.gameObject, subFixedChildren); var splitName = subRoot.gameObject.name.Split('.'); var node = new AssetNodeData(splitName [2], subRoot.gameObject.transform.localPosition); rootNode.children.Add(node); foreach (var subChild in subFixedChildren) { node.children.Add(new AssetNodeData(subChild.gameObject.name, subChild.gameObject.transform.localPosition)); } } XmlSerializer xmlSerializer = new XmlSerializer(typeof(AssetNodeData)); FileStream file = new FileStream(FilePath() + instance.fileName + ".txt", FileMode.Create); xmlSerializer.Serialize(file, rootNode); file.Close(); }
public static void LoadNode() { XmlSerializer xmlSerializer = new XmlSerializer(typeof(AssetNodeData)); if (!File.Exists(FilePath() + instance.fileName + ".txt")) { return; } FileStream file = new FileStream(FilePath() + instance.fileName + ".txt", FileMode.Open); AssetNodeData data = xmlSerializer.Deserialize(file) as AssetNodeData; file.Close(); GameObject rootLevel = null; rootLevel = GameObject.Find(data.text); if (!rootLevel) { rootLevel = new GameObject(data.text); } rootLevel.transform.localPosition = data.Position(); foreach (var dataNode in data.children) { GameObject subLevel = null; subLevel = GameObject.Find(dataNode.text); if (!subLevel) { subLevel = new GameObject(dataNode.text); } subLevel.transform.parent = rootLevel.transform; subLevel.transform.localPosition = dataNode.Position(); foreach (var childNode in dataNode.children) { //TODO Make cleaner string assetString = "Assets/PlacementAssets/" + dataNode.text + "/" + childNode.text + ".prefab"; #if UNITY_EDITOR var asset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetString, typeof(GameObject)) as GameObject; if (asset) { var newObject = GameObject.Instantiate(asset) as GameObject; newObject.name = childNode.text; newObject.transform.parent = subLevel.transform; newObject.transform.localPosition = childNode.Position(); } #endif } } }