Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [MenuItem("Custom/Save RAINpuppet")]
- public static void SaveRAINpuppet()
- {
- GameObject obj = GameObject.Find("RAINPuppet1");
- if (null == obj)
- {
- Debug.LogError("Save failed - RAINpuppet1 object not found");
- return;
- }
- XmlDocument doc = new XmlDocument();
- XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
- doc.AppendChild(declaration);
- XmlElement rainElems = doc.CreateElement("RAINComponents");
- doc.AppendChild(rainElems);
- RAIN.Core.RAINComponent[] rainComponents = obj.GetComponentsInChildren<RAIN.Core.RAINComponent>();
- Dictionary<Type, int> componentTypesCounter = new Dictionary<Type, int>();
- foreach (RAIN.Core.RAINComponent component in rainComponents)
- {
- int count;
- if (!componentTypesCounter.TryGetValue(component.GetType(), out count))
- componentTypesCounter.Add(component.GetType(), 1);
- else
- componentTypesCounter[component.GetType()] = count + 1;
- component.Serialize();
- XmlElement componentElem = doc.CreateElement("RAINComponent");
- componentElem.SetAttribute("type", component.GetType().ToString());
- componentElem.SetAttribute("typeIndex", (componentTypesCounter[component.GetType()] - 1).ToString());
- rainElems.AppendChild(componentElem);
- byte[] data = System.Text.Encoding.UTF8.GetBytes(component.DataSerializer.SerializedData);
- string base64data = Convert.ToBase64String(data);
- XmlElement componentData = doc.CreateElement("data");
- componentElem.AppendChild(componentData);
- componentData.InnerText = base64data;
- XmlElement componentCustomData = doc.CreateElement("customData");
- componentElem.AppendChild(componentCustomData);
- foreach (RAIN.Serialization.FieldSerializer.CustomSerializedData c in component.DataSerializer.SerializedCustomData)
- {
- XmlElement customDataEntry = doc.CreateElement("entry");
- componentCustomData.AppendChild(customDataEntry);
- string base64customData = Convert.ToBase64String(c.Data);
- customDataEntry.InnerText = base64customData;
- }
- XmlElement componentObjectsData = doc.CreateElement("objectsData");
- componentElem.AppendChild(componentObjectsData);
- foreach (UnityEngine.Object o in component.DataSerializer.SerializedGameObjects)
- {
- XmlElement objectDataEntry = doc.CreateElement("entry");
- componentObjectsData.AppendChild(objectDataEntry);
- objectDataEntry.SetAttribute("type", o.GetType().ToString());
- objectDataEntry.SetAttribute("name", o.name);
- }
- }
- using (var writer = new XmlTextWriter("D:\\data.txt", null))
- {
- writer.Formatting = Formatting.Indented;
- doc.WriteTo(writer);
- writer.Flush();
- writer.Close();
- }
- }
- [MenuItem("Custom/Load RAINpuppet")]
- public static void LoadRAINpuppet()
- {
- GameObject obj = GameObject.Find("RAINPuppet1");
- if (null == obj)
- {
- Debug.LogError("Load failed - RAINpuppet1 object not found");
- return;
- }
- RAIN.Core.AIRig[] aiRigs = obj.GetComponentsInChildren<RAIN.Core.AIRig>();
- RAIN.Entities.EntityRig[] entityRigs = obj.GetComponentsInChildren<RAIN.Entities.EntityRig>();
- RAIN.Navigation.NavMesh.NavMeshRig[] navMeshRigs = obj.GetComponentsInChildren<RAIN.Navigation.NavMesh.NavMeshRig>();
- RAIN.Navigation.Targets.NavigationTargetRig[] navTargetRigs = obj.GetComponentsInChildren<RAIN.Navigation.Targets.NavigationTargetRig>();
- RAIN.Navigation.Waypoints.WaypointRig[] waypointRigs = obj.GetComponentsInChildren<RAIN.Navigation.Waypoints.WaypointRig>();
- RAIN.Core.RAINComponent component = (aiRigs.Length > 0) ? aiRigs.ElementAt(0) : null;
- component = (null == component && entityRigs.Length > 0) ? entityRigs.ElementAt(0) : component;
- component = (null == component && navMeshRigs.Length > 0) ? navMeshRigs.ElementAt(0) : component;
- component = (null == component && navTargetRigs.Length > 0) ? navTargetRigs.ElementAt(0) : component;
- component = (null == component && waypointRigs.Length > 0) ? waypointRigs.ElementAt(0) : component;
- if (null == component)
- {
- Debug.LogError("no RAIN component found on GameObject \"RAINPuppet1\"!");
- return;
- }
- GameObject[] gos = GameObject.FindObjectsOfType<GameObject>();
- using (System.IO.StreamReader dataReader = new System.IO.StreamReader("D:\\data.txt"))
- {
- string data = dataReader.ReadToEnd();
- dataReader.Close();
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(data);
- XmlNodeList nodes = doc.GetElementsByTagName("RAINComponent");
- foreach (XmlNode node in nodes)
- {
- XmlNode typeNode = node.Attributes.GetNamedItem("type");
- XmlNode typeIndexNode = node.Attributes.GetNamedItem("typeIndex");
- XmlNodeList componentNodes = node.ChildNodes;
- string componentData = "";
- List<RAIN.Serialization.FieldSerializer.CustomSerializedData> customData = new List<RAIN.Serialization.FieldSerializer.CustomSerializedData>();
- List<UnityEngine.Object> objectsData = new List<UnityEngine.Object>();
- foreach (XmlNode n in componentNodes)
- {
- switch (n.Name)
- {
- case "data":
- componentData = n.InnerText;
- byte[] nodeData = Convert.FromBase64String(componentData);
- componentData = System.Text.Encoding.UTF8.GetString(nodeData);
- break;
- case "customData":
- XmlNodeList customDataNodes = n.ChildNodes;
- foreach(XmlNode no in customDataNodes)
- {
- byte[] customRawData = Convert.FromBase64String(no.InnerText);
- RAIN.Serialization.FieldSerializer.CustomSerializedData c = new RAIN.Serialization.FieldSerializer.CustomSerializedData(customRawData);
- customData.Add(c);
- }
- break;
- case "objectsData":
- XmlNodeList objectsDataNodes = n.ChildNodes;
- foreach (XmlNode no in objectsDataNodes)
- {
- string name, type;
- name = type = "";
- XmlAttributeCollection noAttrs = no.Attributes;
- foreach (XmlAttribute a in noAttrs)
- {
- switch (a.Name)
- {
- case "name":
- name = a.Value;
- break;
- case "type":
- type = a.Value;
- break;
- default:
- Debug.Log("unknown object entry attribute " + a.Name);
- break;
- }
- }
- //GameObject go = GameObject.Find(name);
- GameObject go = null;
- foreach (GameObject g in gos)
- {
- if (g.name == name )
- go = g;
- }
- if (go != null)
- {
- switch (type)
- {
- case "UnityEngine.GameObject":
- objectsData.Add(go);
- break;
- case "UnityEngine.Transform":
- objectsData.Add(go.transform);
- break;
- default:
- Debug.Log(type);
- Debug.LogError("unknown gameobject type");
- break;
- }
- }
- }
- break;
- default:
- Debug.LogError("Unknown node " + n.Name + " in RAIN component!");
- break;
- }
- }
- int index = Convert.ToInt32(typeIndexNode.Value);
- switch (typeNode.Value)
- {
- case "RAIN.Core.AIRig":
- if (index >= aiRigs.Length)
- Debug.LogError("RAINComponent AIRig with index " + index.ToString() + " was not found in the parent object!");
- if (index >= 0 && index < aiRigs.Length)
- {
- aiRigs.ElementAt(index).AI.WorkingMemory.Clear();
- aiRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
- //aiRigs.ElementAt(index).ApplySerializationChanges();
- }
- break;
- case "RAIN.Entities.EntityRig":
- if (index >= entityRigs.Length)
- Debug.LogError("RAINComponent EntityRig with index " + index.ToString() + " was not found in the parent object!");
- if (index >= 0 && index < entityRigs.Length)
- {
- entityRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
- //entityRigs.ElementAt(index).ApplySerializationChanges();
- }
- break;
- case "RAIN.Navigation.NavMesh.NavMeshRig":
- if (index >= navMeshRigs.Length)
- Debug.LogError("RAINComponent NavMeshRig with index " + index.ToString() + " was not found in the parent object!");
- if (index >= 0 && index < navMeshRigs.Length)
- {
- navMeshRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
- //navMeshRigs.ElementAt(index).ApplySerializationChanges();
- }
- break;
- case "RAIN.Navigation.Targets.NavigationTargetRig":
- if (index >= navTargetRigs.Length)
- Debug.LogError("RAINComponent NavigationTargetRig with index " + index.ToString() + " was not found in the parent object!");
- if (index >= 0 && index < navTargetRigs.Length)
- {
- navTargetRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
- //navTargetRigs.ElementAt(index).ApplySerializationChanges();
- }
- break;
- case "RAIN.Navigation.Waypoints.WaypointRig":
- if (index >= waypointRigs.Length)
- Debug.LogError("RAINComponent WaypointRig with index " + index.ToString() + " was not found in the parent object!");
- if (index >= 0 && index < waypointRigs.Length)
- {
- waypointRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
- //waypointRigs.ElementAt(index).ApplySerializationChanges();
- }
- break;
- default:
- Debug.LogError("unknown RAINComponent type");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement