Beispiel #1
0
        private List <XElement> CreateImportFilesElements(string exportToUnityProjectPath)
        {
            List <XElement> elements = new List <XElement>();

            // Add the mesh file as raw text
            {
                StringWriter objBuilder = BuildObjString();

                XElement mesh =
                    new XElement("ImportMesh",
                                 new XAttribute("filename", this.tmxMap.Name + ".obj"),
                                 StringToBase64String(objBuilder.ToString()));

                elements.Add(mesh);
            }

            {
                // Add all image files as compressed base64 strings
                var layerImages = from layer in this.tmxMap.Layers
                                  where layer.Visible == true
                                  from rawTileId in layer.TileIds
                                  where rawTileId != 0
                                  let tileId                       = TmxMath.GetTileIdWithoutFlags(rawTileId)
                                                          let tile = this.tmxMap.Tiles[tileId]
                                                                     select tile.TmxImage;

                // Find the images from the frames as well
                var frameImages = from layer in this.tmxMap.Layers
                                  where layer.Visible == true
                                  from rawTileId in layer.TileIds
                                  where rawTileId != 0
                                  let tileId                       = TmxMath.GetTileIdWithoutFlags(rawTileId)
                                                          let tile = this.tmxMap.Tiles[tileId]
                                                                     from rawFrame in tile.Animation.Frames
                                                                     let frameId                         = TmxMath.GetTileIdWithoutFlags(rawFrame.GlobalTileId)
                                                                                               let frame = this.tmxMap.Tiles[frameId]
                                                                                                           select frame.TmxImage;


                // Tile Objects may have images not yet references by a layer
                var objectImages = from objectGroup in this.tmxMap.ObjectGroups
                                   where objectGroup.Visible == true
                                   from tmxObject in objectGroup.Objects
                                   where tmxObject.Visible == true
                                   where tmxObject is TmxObjectTile
                                   let tmxTileObject = tmxObject as TmxObjectTile
                                                       from mesh in tmxTileObject.Tile.Meshes
                                                       select mesh.TmxImage;

                // Combine image paths from tile layers and object layers
                List <TmxImage> images = new List <TmxImage>();
                images.AddRange(layerImages);
                images.AddRange(frameImages);
                images.AddRange(objectImages);

                // Get rid of duplicate images
                TmxImageComparer imageComparer = new TmxImageComparer();
                images = images.Distinct(imageComparer).ToList();

                foreach (TmxImage image in images)
                {
                    // The source texture is internal if it has a sibling *.meta file
                    // We don't want to copy internal textures into Unity because they are already there.
                    bool isInternal = File.Exists(image.AbsolutePath + ".meta");
                    if (isInternal)
                    {
                        // The texture is already in the Unity project so don't import
                        XElement xmlInternalTexture = new XElement("InternalTexture");

                        // The path to the texture will be WRT to the Unity project root
                        string assetsFolder = GetUnityAssetsPath(image.AbsolutePath);
                        string assetPath    = image.AbsolutePath.Remove(0, assetsFolder.Length);
                        assetPath = "Assets" + assetPath;
                        assetPath = assetPath.Replace("\\", "/");

                        Logger.WriteLine("InternalTexture : {0}", assetPath);

                        // Path to texture in the asset directory
                        xmlInternalTexture.SetAttributeValue("assetPath", assetPath);

                        // Transparent color key?
                        if (!String.IsNullOrEmpty(image.TransparentColor))
                        {
                            xmlInternalTexture.SetAttributeValue("alphaColorKey", image.TransparentColor);
                        }

                        // Are we using depth shaders on our materials?
                        if (Tiled2Unity.Settings.DepthBufferEnabled)
                        {
                            xmlInternalTexture.SetAttributeValue("usesDepthShaders", true);
                        }

                        // Will the material be loaded as a resource?
                        if (this.tmxMap.IsResource)
                        {
                            xmlInternalTexture.SetAttributeValue("isResource", true);
                        }

                        elements.Add(xmlInternalTexture);
                    }
                    else
                    {
                        // The texture needs to be imported into the Unity project (under Tiled2Unity's care)
                        XElement xmlImportTexture = new XElement("ImportTexture");

                        // Note that compression is not available in Unity. Go with Base64 string. Blerg.
                        Logger.WriteLine("ImportTexture : will import '{0}' to {1}", image.AbsolutePath, Path.Combine(exportToUnityProjectPath, "Textures"));

                        // Is there a color key for transparency?
                        if (!String.IsNullOrEmpty(image.TransparentColor))
                        {
                            xmlImportTexture.SetAttributeValue("alphaColorKey", image.TransparentColor);
                        }

                        // Are we using depth shaders on our materials?
                        if (Tiled2Unity.Settings.DepthBufferEnabled)
                        {
                            xmlImportTexture.SetAttributeValue("usesDepthShaders", true);
                        }

                        // Will the material be loaded as a resource?
                        if (this.tmxMap.IsResource)
                        {
                            xmlImportTexture.SetAttributeValue("isResource", true);
                        }

                        // Bake the image file into the xml
                        xmlImportTexture.Add(new XAttribute("filename", Path.GetFileName(image.AbsolutePath)), FileToBase64String(image.AbsolutePath));

                        elements.Add(xmlImportTexture);
                    }
                }
            }

            return(elements);
        }
Beispiel #2
0
        private List <XElement> CreateImportFilesElements(string exportToUnityProjectPath)
        {
            List <XElement> elements = new List <XElement>();

            // Add the mesh file as raw text
            {
                StringWriter objBuilder = BuildObjString();

                XElement mesh =
                    new XElement("ImportMesh",
                                 new XAttribute("filename", this.tmxMap.Name + ".obj"),
                                 StringToBase64String(objBuilder.ToString()));

                elements.Add(mesh);
            }

            {
                // Add all image files as compressed base64 strings
                var layerImages = from layer in this.tmxMap.Layers
                                  where layer.Visible == true
                                  from rawTileId in layer.TileIds
                                  where rawTileId != 0
                                  let tileId                       = TmxMath.GetTileIdWithoutFlags(rawTileId)
                                                          let tile = this.tmxMap.Tiles[tileId]
                                                                     select tile.TmxImage;

                // Tile Objects may have images not yet references by a layer
                var objectImages = from objectGroup in this.tmxMap.ObjectGroups
                                   where objectGroup.Visible == true
                                   from tmxObject in objectGroup.Objects
                                   where tmxObject.Visible == true
                                   where tmxObject is TmxObjectTile
                                   let tmxTileObject = tmxObject as TmxObjectTile
                                                       from mesh in tmxTileObject.Tile.Meshes
                                                       select mesh.TmxImage;

                // Combine image paths from tile layers and object layers
                List <TmxImage> images = new List <TmxImage>();
                images.AddRange(layerImages);
                images.AddRange(objectImages);

                // Get rid of duplicate images
                TmxImageComparer imageComparer = new TmxImageComparer();
                images = images.Distinct(imageComparer).ToList();

                // Do not import files if they are already in the project (in the /Assets/ directory of where we're exporting too)
                string unityAssetsDir = Path.Combine(exportToUnityProjectPath, "Assets");

                foreach (TmxImage image in images)
                {
                    // If the copy from location comes from within the project we want to copy to, then don't do it.
                    // This allows us to have tileset images that are alreday in use by the Unity project
                    string saveToAssetsDir = unityAssetsDir.ToLower();
                    string copyFromDir     = image.AbsolutePath.ToLower();

                    if (copyFromDir.StartsWith(saveToAssetsDir))
                    {
                        XElement xmlInternalTexture = new XElement("InternalTexture");

                        // The path to the texture will be WRT to the Unity project root
                        string assetPath = image.AbsolutePath.Remove(0, exportToUnityProjectPath.Length);
                        assetPath = assetPath.TrimStart('\\');
                        assetPath = assetPath.TrimStart('/');
                        Program.WriteLine("InternalTexture : {0}", assetPath);

                        // Path to texture in the asset directory
                        xmlInternalTexture.SetAttributeValue("assetPath", assetPath);

                        // Transparent color key?
                        if (!String.IsNullOrEmpty(image.TransparentColor))
                        {
                            xmlInternalTexture.SetAttributeValue("alphaColorKey", image.TransparentColor);
                        }

                        // Are we using depth shaders on our materials?
                        if (Program.DepthBufferEnabled)
                        {
                            xmlInternalTexture.SetAttributeValue("usesDepthShaders", true);
                        }

                        elements.Add(xmlInternalTexture);
                    }
                    else
                    {
                        XElement xmlImportTexture = new XElement("ImportTexture");

                        // Note that compression is not available in Unity. Go with Base64 string. Blerg.
                        Program.WriteLine("ImportTexture : will import '{0}' to {1}", image.AbsolutePath, Path.Combine(unityAssetsDir, "Tiled2Unity\\Textures\\"));

                        // Is there a color key for transparency?
                        if (!String.IsNullOrEmpty(image.TransparentColor))
                        {
                            xmlImportTexture.SetAttributeValue("alphaColorKey", image.TransparentColor);
                        }

                        // Are we using depth shaders on our materials?
                        if (Program.DepthBufferEnabled)
                        {
                            xmlImportTexture.SetAttributeValue("usesDepthShaders", true);
                        }

                        // Bake the image file into the xml
                        xmlImportTexture.Add(new XAttribute("filename", Path.GetFileName(image.AbsolutePath)), FileToBase64String(image.AbsolutePath));

                        elements.Add(xmlImportTexture);
                    }
                }
            }

            return(elements);
        }