Exemplo n.º 1
0
    public static void ImportActionLibrary()
    {
        string file = EditorUtility.OpenFilePanel("Select Action Library to import", "", "xml");

        if (file == null || file.Length == 0)
        {
            return;
        }

        XDocument doc = XDocument.Load(file);

        try
        {
            ActionLibrary importedLibrary = new ActionLibrary();

            foreach (var network in doc.Root.Elements("Network"))
            {
                ActionNetwork actN = new ActionNetwork(network.Attribute("Name").Value);

                importedLibrary.Add(actN);

                foreach (var skill in network.Elements("Skills").Elements("Skill"))
                {
                    string[] position = skill.Element("Position").Value.Replace("(", "").Replace(")", "").Split(',');

                    EditorTwoVector pos = new EditorTwoVector(float.Parse(position[0]), float.Parse(position[1]));

                    actN.AddSkill(skill.Attribute("Name").Value, double.Parse(skill.Element("Q").Value), double.Parse(skill.Element("B").Value),
                                  double.Parse(skill.Element("v").Value), pos);
                }

                foreach (var connection in network.Elements("Connections").Elements("Connection"))
                {
                    actN.AddConnection(actN.Get(connection.Element("ToSkill").Value), actN.Get(connection.Element("FromSkill").Value),
                                       double.Parse(connection.Element("ToValue").Value), double.Parse(connection.Element("FromValue").Value));
                }
            }

            string fileName = Path.GetFileNameWithoutExtension(file);

            string nameCopy = fileName.Clone().ToString();

            int index = 0;

            if (System.IO.File.Exists(Application.dataPath + "/coAdjoint/Action/Libraries/" + fileName + ".asset"))
            {
                ++index;

                while (System.IO.File.Exists(Application.dataPath + "/coAdjoint/Action/Libraries/" + fileName + index + ".asset"))
                {
                    ++index;
                }

                nameCopy += index.ToString();
            }

            var asset = ScriptableObject.CreateInstance <ActionAsset>();

            asset.libraryData = importedLibrary.GetData();

            AssetDatabase.CreateAsset(asset, "Assets/coAdjoint/Action/Libraries/" + nameCopy + ".asset");

            Selection.activeObject = asset;

            ActionMenu.EditLibraryAsset();
        }
        catch (Exception e)
        {
            Debug.LogError("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
    }