Beispiel #1
0
        protected static ImportAsset AssetDataSource(ImportAsset importAsset, XmlNode node, ImportType importType)
        {
            switch (importType)
            {
            case ImportType.Background:
                importAsset.sourceAsset            = basePath + "background" + Path.DirectorySeparatorChar + node.SelectSingleNode("//data").InnerText;
                importAsset.targetCompletePath     = GetCompleteTargetPath(importAsset);
                importAsset.targetCompleteFilePath = GetCompleteTargetFilePath(importAsset, "");
                break;

            case ImportType.Object:
                importAsset.targetCompletePath     = GetCompleteTargetPath(importAsset);
                importAsset.targetCompleteFilePath = GetCompleteTargetFilePath(importAsset, "");
                break;

            case ImportType.Room:
                importAsset.targetCompletePath     = GetCompleteTargetPath(importAsset);
                importAsset.targetCompleteFilePath = GetCompleteTargetFilePath(importAsset, "");
                break;

            case ImportType.Sound:
                importAsset.sourceAsset            = basePath + node.SelectSingleNode("origname").InnerText;
                importAsset.targetCompletePath     = GetCompleteTargetPath(importAsset);
                importAsset.targetCompleteFilePath = GetCompleteTargetFilePath(importAsset, node.SelectSingleNode("extension").InnerText);
                break;

            case ImportType.Sprite:
                if (node != null && node.SelectSingleNode("//frame") != null)
                {
                    importAsset.sourceAsset            = basePath + "sprites" + Path.DirectorySeparatorChar + node.SelectSingleNode("//frame").InnerText;
                    importAsset.targetCompletePath     = GetCompleteTargetPath(importAsset);
                    importAsset.targetCompleteFilePath = GetCompleteTargetFilePath(importAsset, "");


                    XmlNodeList nodeList = node.SelectNodes("//frame");
                    if (nodeList.Count > 1)
                    {
                        importAsset = new ImportAsset(importAsset);
                        List <string> sourceSprites = new List <string>();

                        foreach (XmlNode nodeling in nodeList)
                        {
                            if (nodeling.InnerText != null)
                            {
                                sourceSprites.Add(basePath + "sprites" + Path.DirectorySeparatorChar + nodeling.InnerText);
                            }
                        }
                        importAsset.AddSpriteList(sourceSprites);
                    }
                }
                else
                {
                    importAsset = null;
                }
                break;
            }

            return(importAsset);
        }
Beispiel #2
0
        protected static string GetCompleteTargetFilePath(ImportAsset assetData, string extensionType)
        {
            string result = "";

            result += assetData.targetCompletePath + Path.DirectorySeparatorChar + assetData.targetName + extensionType;

            return(result);
        }
Beispiel #3
0
 public ImportAsset(ImportAsset importAsset)
 {
     targetName             = importAsset.targetName;
     targetPath             = importAsset.targetPath;
     targetCompletePath     = importAsset.targetCompletePath;
     targetCompleteFilePath = importAsset.targetCompleteFilePath;
     sourceXML   = importAsset.sourceXML;
     sourceAsset = importAsset.sourceAsset;
 }
Beispiel #4
0
        protected static string GetCompleteTargetPath(ImportAsset assetData)
        {
            string result = "";

            result = Application.dataPath + assetData.targetPath;
            result = result.Replace('/', Path.DirectorySeparatorChar);

            return(result);
        }
        public static void ImportObject(ImportAsset importAsset = null)
        {
            XmlDocument sourceXml = new XmlDocument();

            sourceXml.Load(importAsset.sourceXML);
            XmlElement rootElement = sourceXml.DocumentElement;

            string spriteName = rootElement.SelectSingleNode("spriteName").InnerText;
            string localPath  = "Assets" + importAsset.targetPath;
            string localName  = localPath + "/" + importAsset.targetName + ".prefab";

            localName = localName.Replace('\\', '/');
            int depth = 0;

            if (rootElement.SelectSingleNode("//depth").InnerText != null)
            {
                depth  = int.Parse(rootElement.SelectSingleNode("//depth").InnerText);
                depth /= ImportSettings.Instance.PixelsPerUnit;
            }

            if (!Directory.Exists(importAsset.targetCompletePath))
            {
                Directory.CreateDirectory(importAsset.targetCompletePath);
            }

            GameObject go      = new GameObject();
            Vector3    vector3 = new Vector3(0f, 0f, depth);

            go.name = importAsset.targetName;
            go.transform.position = vector3;

            if (spriteName != "<undefined>")
            {
                Sprite sprite = FindSprite(spriteName);
                if (sprite != null)
                {
                    SpriteRenderer sr = go.AddComponent <SpriteRenderer>();
                    sr.sprite = sprite;
                }
                else
                {
                    Debug.LogWarning("<color=#ff9999ff>Cannot find sprite for: " + spriteName + " in object </color><color=#ffdd22ff>" + importAsset.targetName + "</color>");
                }
            }

            Object prefab = PrefabUtility.CreateEmptyPrefab(localName);

            PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.Default);

            DestroyImmediate(go);

            if (ImportSettings.Instance.ShowLogging)
            {
                print("Imported object: <color=#22ffccff>" + localName + "</color> " + "<color=#22ee22ff>" + importAsset.targetName + "</color>");
            }
        }
Beispiel #6
0
        protected static void SetAssetDataFromXML(XmlNode node, string targetType)
        {
            ImportAsset importAsset = new ImportAsset(GetRelativeNodePath(node), GetSanitizedName(node.InnerText));

            importAsset.sourceXML = basePath + node.InnerText + GetGmxSuffix(node.InnerText);
            XmlElement xmlRoot = GetRootXmlElement(importAsset.sourceXML);

            importAsset.targetCompletePath = GetCompleteTargetPath(importAsset);

            ImportType importType = GetImportType(targetType);

            importAsset = AssetDataSource(importAsset, xmlRoot, importType);

            if (importAsset != null)
            {
                targetPaths.Add(importAsset);
            }
        }
Beispiel #7
0
        protected static void CheckAndCopy(ImportAsset assetData)
        {
            if (!Directory.Exists(assetData.targetCompletePath))
            {
                Directory.CreateDirectory(assetData.targetCompletePath);
            }

            if (File.Exists(assetData.sourceAsset))
            {
                File.Copy(assetData.sourceAsset, assetData.targetCompleteFilePath, true);

                if (ImportSettings.Instance.ShowLogging)
                {
                    print("Imported audio <color=#22ffccff>" + assetData.targetCompleteFilePath + "</color> " + "<color=#22ee22ff>" + assetData.targetName + "</color>");
                }
            }
            else
            {
                Debug.LogWarning("Cannot find " + assetData.targetName + " where Game Maker said the file should be");
            }
        }
        public static void ImportRoom(ImportAsset importAsset = null)
        {
            XmlDocument sourceXml = new XmlDocument();

            sourceXml.Load(importAsset.sourceXML);
            XmlElement rootElement = sourceXml.DocumentElement;

            XmlNodeList nodeList = rootElement.SelectNodes("//instance");

            int ppu = ImportSettings.Instance.PixelsPerUnit;

            Scene scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);

            int height = (int.Parse(rootElement.SelectSingleNode("//height").InnerText)) / ppu;

            string assetPath = "Assets" + importAsset.targetPath + "/" + importAsset.targetName + ".unity";

            assetPath = assetPath.Replace('\\', '/');

            foreach (XmlNode node in nodeList)
            {
                Vector3 pos = new Vector3();
                pos.x = (int.Parse(node.Attributes["x"].Value)) / ppu;
                pos.y = height - (int.Parse(node.Attributes["y"].Value) / ppu);

                string assetName = node.Attributes["objName"].Value;

                string[] guids = AssetDatabase.FindAssets(assetName);

                if (guids != null && guids.Length > 0)
                {
                    string     path   = AssetDatabase.GUIDToAssetPath(guids[0]);
                    Object     prefab = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
                    GameObject go     = PrefabUtility.InstantiatePrefab(prefab as GameObject) as GameObject;
                    pos.z = go.transform.position.z;
                    go.transform.position = pos;
                }
                else
                {
                    Debug.LogWarning("<color=#ff9999ff>Cannot find an object in the database for </color><color=#ffdd22ff>: " + assetName + "</color>");
                }
            }

            if (!Directory.Exists(importAsset.targetCompletePath))
            {
                Directory.CreateDirectory(importAsset.targetCompletePath);
            }

            bool sceneSaved = EditorSceneManager.SaveScene(scene, assetPath);

            if (!sceneSaved)
            {
                Debug.LogWarning("Saved scene <color=#22ffccff>" + assetPath + "</color> " + "<color=#ee2222ff>Nope</color>");
            }
            else
            {
                if (ImportSettings.Instance.ShowLogging)
                {
                    print("Saved scene <color=#22ffccff>" + assetPath + "</color> " + "<color=#22ee22ff>OK</color>");
                }
            }
        }
        public static void ImportSprite(ImportAsset importAsset = null)
        {
            string shortPath = importAsset.targetPath + Path.DirectorySeparatorChar;

            string[]  files;
            Texture2D textured2D;

            if (importAsset.SourceElements != null && importAsset.SourceElements.Count > 0)
            {
                files = importAsset.SourceElements.ToArray();
            }
            else
            {
                files    = new string[1];
                files[0] = importAsset.sourceAsset;
            }

            string otherPath = "Assets" + shortPath;
            string assetPath = otherPath + importAsset.targetName + Path.GetExtension(importAsset.sourceAsset);

            path  = Application.dataPath;
            path  = path.Replace('/', Path.DirectorySeparatorChar);
            path += Path.DirectorySeparatorChar + shortPath;

            if (!Directory.Exists(importAsset.targetCompletePath))
            {
                Directory.CreateDirectory(importAsset.targetCompletePath);
            }

            textured2D = GetSpriteTexture(files);

            if (textured2D != null)
            {
                string nameSprite = Path.GetFileNameWithoutExtension(assetPath) + Path.GetExtension(assetPath);
                File.WriteAllBytes(Application.dataPath + shortPath + nameSprite, textured2D.EncodeToPNG());
                Sprite sprite = GetSprite(textured2D);

                AssetDatabase.Refresh();
                if (sprite == null)
                {
                    if (textured2D == null)
                    {
                        textured2D = GetSpriteTexture(files);
                    }

                    sprite = GetSprite(textured2D);
                }
                AssetDatabase.AddObjectToAsset(sprite, otherPath + nameSprite);
                AssetDatabase.SaveAssets();
                UpdateTextureSettings(otherPath, nameSprite, files, importAsset.sourceXML);

                if (ImportSettings.Instance.ShowLogging)
                {
                    print("Imported sprite: <color=#22ffccff>" + assetPath + "</color> " + "<color=#22ee22ff>" + nameSprite + "</color>");
                }
            }
            else
            {
                Debug.LogWarning("<color=#ff9999ff>" + importAsset.targetName + "</color><color=#ffdd22ff> cannot be imported, probably because giant texture or something</color>");
            }
        }