Пример #1
0
        private void HandleTiledAttributes(GameObject gameObject, XElement goXml)
        {
            // Add the TiledMap component
            TiledMap map = gameObject.AddComponent <TiledMap>();

            try
            {
                map.Orientation       = ImportUtils.GetAttributeAsEnum <TiledMap.MapOrientation>(goXml, "orientation");
                map.StaggerAxis       = ImportUtils.GetAttributeAsEnum <TiledMap.MapStaggerAxis>(goXml, "staggerAxis");
                map.StaggerIndex      = ImportUtils.GetAttributeAsEnum <TiledMap.MapStaggerIndex>(goXml, "staggerIndex");
                map.HexSideLength     = ImportUtils.GetAttributeAsInt(goXml, "hexSideLength");
                map.NumLayers         = ImportUtils.GetAttributeAsInt(goXml, "numLayers");
                map.NumTilesWide      = ImportUtils.GetAttributeAsInt(goXml, "numTilesWide");
                map.NumTilesHigh      = ImportUtils.GetAttributeAsInt(goXml, "numTilesHigh");
                map.TileWidth         = ImportUtils.GetAttributeAsInt(goXml, "tileWidth");
                map.TileHeight        = ImportUtils.GetAttributeAsInt(goXml, "tileHeight");
                map.ExportScale       = ImportUtils.GetAttributeAsFloat(goXml, "exportScale");
                map.MapWidthInPixels  = ImportUtils.GetAttributeAsInt(goXml, "mapWidthInPixels");
                map.MapHeightInPixels = ImportUtils.GetAttributeAsInt(goXml, "mapHeightInPixels");
            }
            catch
            {
                Debug.LogWarning(String.Format("Error adding TiledMap component. Are you using an old version of Tiled2Unity in your Unity project?"));
                GameObject.DestroyImmediate(map);
            }
        }
        private void AddCollidersTo(GameObject gameObject, XElement xml)
        {
            // Box colliders
            foreach (XElement xmlBoxCollider2D in xml.Elements("BoxCollider2D"))
            {
                BoxCollider2D collider = gameObject.AddComponent <BoxCollider2D>();
                float         width    = ImportUtils.GetAttributeAsFloat(xmlBoxCollider2D, "width");
                float         height   = ImportUtils.GetAttributeAsFloat(xmlBoxCollider2D, "height");
                collider.size   = new Vector2(width, height);
                collider.center = new Vector2(width * 0.5f, -height * 0.5f);
            }

            // Circle colliders
            foreach (XElement xmlCircleCollider2D in xml.Elements("CircleCollider2D"))
            {
                CircleCollider2D collider = gameObject.AddComponent <CircleCollider2D>();
                float            radius   = ImportUtils.GetAttributeAsFloat(xmlCircleCollider2D, "radius");
                collider.radius = radius;
                collider.center = new Vector2(radius, -radius);
            }

            // Edge colliders
            foreach (XElement xmlEdgeCollider2D in xml.Elements("EdgeCollider2D"))
            {
                EdgeCollider2D collider = gameObject.AddComponent <EdgeCollider2D>();
                string         data     = xmlEdgeCollider2D.Element("Points").Value;

                // The data looks like this:
                //  x0,y0 x1,y1 x2,y2 ...
                var points = from pt in data.Split(' ')
                             let x                 = Convert.ToSingle(pt.Split(',')[0])
                                             let y = Convert.ToSingle(pt.Split(',')[1])
                                                     select new Vector2(x, y);

                collider.points = points.ToArray();
            }

            // Polygon colliders
            foreach (XElement xmlPolygonCollider2D in xml.Elements("PolygonCollider2D"))
            {
                PolygonCollider2D collider = gameObject.AddComponent <PolygonCollider2D>();

                var paths = xmlPolygonCollider2D.Elements("Path").ToArray();
                collider.pathCount = paths.Count();

                for (int p = 0; p < collider.pathCount; ++p)
                {
                    string data = paths[p].Value;

                    // The data looks like this:
                    //  x0,y0 x1,y1 x2,y2 ...
                    var points = from pt in data.Split(' ')
                                 let x                 = Convert.ToSingle(pt.Split(',')[0])
                                                 let y = Convert.ToSingle(pt.Split(',')[1])
                                                         select new Vector2(x, y);

                    collider.SetPath(p, points.ToArray());
                }
            }
        }
Пример #3
0
        private void HandleTiledAttributes(GameObject gameObject, XElement goXml, Tiled2Unity.ImportBehaviour importComponent)
        {
            // Add the TiledMap component
            TiledMap map = gameObject.AddComponent <TiledMap>();

            try
            {
                map.Orientation       = ImportUtils.GetAttributeAsEnum <TiledMap.MapOrientation>(goXml, "orientation");
                map.StaggerAxis       = ImportUtils.GetAttributeAsEnum <TiledMap.MapStaggerAxis>(goXml, "staggerAxis");
                map.StaggerIndex      = ImportUtils.GetAttributeAsEnum <TiledMap.MapStaggerIndex>(goXml, "staggerIndex");
                map.HexSideLength     = ImportUtils.GetAttributeAsInt(goXml, "hexSideLength");
                map.NumLayers         = ImportUtils.GetAttributeAsInt(goXml, "numLayers");
                map.NumTilesWide      = ImportUtils.GetAttributeAsInt(goXml, "numTilesWide");
                map.NumTilesHigh      = ImportUtils.GetAttributeAsInt(goXml, "numTilesHigh");
                map.TileWidth         = ImportUtils.GetAttributeAsInt(goXml, "tileWidth");
                map.TileHeight        = ImportUtils.GetAttributeAsInt(goXml, "tileHeight");
                map.ExportScale       = ImportUtils.GetAttributeAsFloat(goXml, "exportScale");
                map.MapWidthInPixels  = ImportUtils.GetAttributeAsInt(goXml, "mapWidthInPixels");
                map.MapHeightInPixels = ImportUtils.GetAttributeAsInt(goXml, "mapHeightInPixels");
                map.BackgroundColor   = ImportUtils.GetAttributeAsColor(goXml, "backgroundColor", Color.black);
            }
            catch
            {
                importComponent.RecordWarning("Couldn't add TiledMap component. Are you using an old version of Tiled2Unity in your Unity project?");
                GameObject.DestroyImmediate(map);
            }
        }
Пример #4
0
        private void HandleTiledAttributes(GameObject gameObject, XElement goXml, int previousMapID, MetadataSettings previousMetadata)
        {
            // Add the TiledMap component
            TiledMap map = gameObject.AddComponent <TiledMap>();

            try
            {
                map.NumTilesWide      = ImportUtils.GetAttributeAsInt(goXml, "numTilesWide");
                map.NumTilesHigh      = ImportUtils.GetAttributeAsInt(goXml, "numTilesHigh");
                map.TileWidth         = ImportUtils.GetAttributeAsInt(goXml, "tileWidth");
                map.TileHeight        = ImportUtils.GetAttributeAsInt(goXml, "tileHeight");
                map.ExportScale       = ImportUtils.GetAttributeAsFloat(goXml, "exportScale");
                map.MapWidthInPixels  = ImportUtils.GetAttributeAsInt(goXml, "mapWidthInPixels");
                map.MapHeightInPixels = ImportUtils.GetAttributeAsInt(goXml, "mapHeightInPixels");
                if (previousMapID == 0)
                {
                    map.MapID = IDHandler.getNextMapID(EditorApplication.currentScene);
                }
                else
                {
                    map.MapID = previousMapID;
                }
                map.gameObject.AddComponent <MetadataSettings>();
                if (previousMetadata != null)
                {
                    map.gameObject.GetComponent <MetadataSettings>().Copy(previousMetadata);
                }
            }
            catch
            {
                Debug.LogWarning(String.Format("Error adding TiledMap component. Are you using an old version of Tiled2Unity in your Unity project?"));
                GameObject.DestroyImmediate(map);
            }
        }
        private void CreatePrefab(XElement xmlPrefab, Tiled2Unity.ImportBehaviour importComponent)
        {
            var customImporters = GetCustomImporterInstances(importComponent);

            // Part 1: Create the prefab
            string     prefabName  = xmlPrefab.Attribute("name").Value;
            float      prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f);
            GameObject tempPrefab  = new GameObject(prefabName);

            HandleTiledAttributes(tempPrefab, xmlPrefab, importComponent);
            HandleCustomProperties(tempPrefab, xmlPrefab, customImporters);

            // 한도영!
            {
                tempPrefab.tag = "Map";
                tempPrefab.AddComponent <Map>();
            }

            // Part 2: Build out the prefab
            // We may have an 'isTrigger' attribute that we want our children to obey
            bool isTrigger = ImportUtils.GetAttributeAsBoolean(xmlPrefab, "isTrigger", false);

            AddGameObjectsTo(tempPrefab, xmlPrefab, isTrigger, importComponent, customImporters);

            // Part 3: Allow for customization from other editor scripts to be made on the prefab
            // (These are generally for game-specific needs)
            CustomizePrefab(tempPrefab, customImporters);

            // Part 3.5: Apply the scale only after all children have been added
            tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale);

            // Part 4: Save the prefab, keeping references intact.
            string resourcePath = ImportUtils.GetAttributeAsString(xmlPrefab, "resourcePath", "");
            bool   isResource   = !String.IsNullOrEmpty(resourcePath) || ImportUtils.GetAttributeAsBoolean(xmlPrefab, "resource", false);
            string prefabPath   = GetPrefabAssetPath(prefabName, isResource, resourcePath);
            string prefabFile   = Path.GetFileName(prefabPath);

            // Keep track of the prefab file being imported
            if (!importComponent.ImportWait_Prefabs.Contains(prefabFile, StringComparer.OrdinalIgnoreCase))
            {
                importComponent.ImportWait_Prefabs.Add(prefabFile);
                importComponent.ImportingAssets.Add(prefabPath);
            }

            UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));

            if (finalPrefab == null)
            {
                // The prefab needs to be created
                ImportUtils.ReadyToWrite(prefabPath);
                finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
            }

            // Replace the prefab, keeping connections based on name. This imports the prefab asset as a side-effect.
            PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased);

            // Destroy the instance from the current scene hiearchy.
            UnityEngine.Object.DestroyImmediate(tempPrefab);
        }
Пример #6
0
        private void CreatePrefab(XElement xmlPrefab, string objPath)
        {
            var customImporters = GetCustomImporterInstances();

            // Part 1: Create the prefab
            string     prefabName  = xmlPrefab.Attribute("name").Value;
            float      prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f);
            GameObject tempPrefab  = new GameObject(prefabName);

            // Part 2: Build out the prefab
            // We may have an 'isTrigger' attribute that we want our children to obey
            bool isTrigger = ImportUtils.GetAttributeAsBoolean(xmlPrefab, "isTrigger", false);

            AddGameObjectsTo(tempPrefab, xmlPrefab, isTrigger, objPath, customImporters);

            // Part 3: Allow for customization from other editor scripts to be made on the prefab
            // (These are generally for game-specific needs)
            CustomizePrefab(tempPrefab, customImporters);

            // Part 3.5: Apply the scale only after all children have been added
            tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale);

            // Part 4: Save the prefab, keeping references intact.
            string resourcePath = ImportUtils.GetAttributeAsString(xmlPrefab, "resourcePath", "");
            bool   isResource   = !String.IsNullOrEmpty(resourcePath) || ImportUtils.GetAttributeAsBoolean(xmlPrefab, "resource", false);
            string prefabPath   = GetPrefabAssetPath(prefabName, isResource, resourcePath);

            UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));

            //Part 5:  Handle properities (uses info regarding whether or not the prefab was already defined)
            int previousMapId = 0;
            MetadataSettings previousMetadata = null;

            if (finalPrefab != null)
            {
                TiledMap map = ((GameObject)AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject))).GetComponent <TiledMap>();
                previousMapId    = map.MapID;
                previousMetadata = ((GameObject)AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject))).GetComponent <MetadataSettings>();
            }
            HandleTiledAttributes(tempPrefab, xmlPrefab, previousMapId, previousMetadata);
            HandleCustomProperties(tempPrefab, xmlPrefab, customImporters);

            if (finalPrefab == null)
            {
                // The prefab needs to be created
                ImportUtils.ReadyToWrite(prefabPath);
                finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
            }

            // Replace the prefab, keeping connections based on name.
            PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased);

            // Destroy the instance from the current scene hiearchy.
            UnityEngine.Object.DestroyImmediate(tempPrefab);
        }
Пример #7
0
        private void AddGameObjectsTo(GameObject parent, XElement xml, bool isParentTrigger, string objPath, IList <ICustomTiledImporter> customImporters)
        {
            foreach (XElement goXml in xml.Elements("GameObject"))
            {
                string name     = ImportUtils.GetAttributeAsString(goXml, "name", "");
                string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", "");

                GameObject child = null;
                if (!String.IsNullOrEmpty(copyFrom))
                {
                    child = CreateCopyFromMeshObj(copyFrom, objPath);
                    if (child == null)
                    {
                        // We're in trouble. Errors should already be in the log.
                        return;
                    }
                }
                else
                {
                    child = new GameObject();
                }

                if (!String.IsNullOrEmpty(name))
                {
                    child.name = name;
                }

                float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0);
                float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0);
                child.transform.position = new Vector3(x, y, 0);

                // Assign the child to the parent
                child.transform.parent = parent.transform;

                // Add any tile animators
                AddTileAnimatorsTo(child, goXml);

                // Do we have any collision data?
                // Check if we are setting 'isTrigger' for ourselves or for our childen
                bool isTrigger = ImportUtils.GetAttributeAsBoolean(goXml, "isTrigger", isParentTrigger);
                AddCollidersTo(child, isTrigger, goXml);

                // Do we have any children of our own?
                AddGameObjectsTo(child, goXml, isTrigger, objPath, customImporters);

                // Does this game object have a tag?
                AssignTagTo(child, goXml);

                // Does this game object have a layer?
                AssignLayerTo(child, goXml);

                // Are there any custom properties?
                HandleCustomProperties(child, goXml, customImporters);
            }
        }
Пример #8
0
 private void FillBaseTmxObjectProperties(Tiled2Unity.TmxObject tmxComponent, XElement xml)
 {
     tmxComponent.TmxId         = ImportUtils.GetAttributeAsInt(xml, "tmx-object-id", -1);
     tmxComponent.TmxName       = ImportUtils.GetAttributeAsString(xml, "tmx-object-name", "");
     tmxComponent.TmxType       = ImportUtils.GetAttributeAsString(xml, "tmx-object-type", "");
     tmxComponent.TmxPosition.x = ImportUtils.GetAttributeAsFloat(xml, "tmx-object-x", 0);
     tmxComponent.TmxPosition.y = ImportUtils.GetAttributeAsFloat(xml, "tmx-object-y", 0);
     tmxComponent.TmxSize.x     = ImportUtils.GetAttributeAsFloat(xml, "tmx-object-width", 0);
     tmxComponent.TmxSize.y     = ImportUtils.GetAttributeAsFloat(xml, "tmx-object-height", 0);
     tmxComponent.TmxRotation   = ImportUtils.GetAttributeAsFloat(xml, "tmx-object-rotation", 0);
 }
Пример #9
0
        private void AddTileObjectComponentsTo(GameObject gameObject, XElement goXml)
        {
            var tileXml = goXml.Element("TileObjectComponent");

            if (tileXml != null)
            {
                TileObject tileObject = gameObject.AddComponent <TileObject>();
                tileObject.TileWidth  = ImportUtils.GetAttributeAsFloat(tileXml, "width");
                tileObject.TileHeight = ImportUtils.GetAttributeAsFloat(tileXml, "height");
            }
        }
        private void CreatePrefab(XElement xmlPrefab, string objPath)
        {
            var customImporters = GetCustomImporterInstances();

            // Part 1: Create the prefab
            string     prefabName  = xmlPrefab.Attribute("name").Value;
            float      prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f);
            GameObject tempPrefab  = new GameObject(prefabName);

            tempPrefab.AddComponent <LevelInfo>();
            HandleTiledAttributes(tempPrefab, xmlPrefab);
            HandleCustomProperties(tempPrefab, xmlPrefab, customImporters);

            // Part 2: Build out the prefab
            // We may have an 'isTrigger' attribute that we want our children to obey
            bool isTrigger = ImportUtils.GetAttributeAsBoolean(xmlPrefab, "isTrigger", false);

            AddGameObjectsTo(tempPrefab, xmlPrefab, isTrigger, objPath, customImporters);

            // Part 3: Allow for customization from other editor scripts to be made on the prefab
            // (These are generally for game-specific needs)
            CustomizePrefab(tempPrefab, customImporters);

            // Part 3.5: Apply the scale only after all children have been added
            tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale);

            // Part 4: Save the prefab, keeping references intact.
            // Modified By:
            // Kurtis Thiessen - June 2

            // Save into levels
            string prefabPath = String.Format("Assets/Resources/Levels/{0}.prefab", prefabName);

            //string prefabPath = ImportUtils.GetPrefabPathFromName(prefabName);

            UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));

            if (finalPrefab == null)
            {
                // The prefab needs to be created
                ImportUtils.ReadyToWrite(prefabPath);
                finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
            }

            // Replace the prefab, keeping connections based on name.
            PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased);

            // Destroy the instance from the current scene hiearchy.
            UnityEngine.Object.DestroyImmediate(tempPrefab);
        }
        // Modified By:
        // Kurtis Thiessen - June 2
        private void AddWeatherScriptsTo(GameObject gameObject, XElement goXml)
        {
            foreach (var animXml in goXml.Elements("TileAnimator"))
            {
                WeatherTile weatherTile = gameObject.AddComponent <WeatherTile>();

                foreach (var frameXml in animXml.Elements("Frame"))
                {
                    WeatherTile.Frame tileFrame = new WeatherTile.Frame();
                    tileFrame.Vertex_z = ImportUtils.GetAttributeAsFloat(frameXml, "vertex_z");
                    weatherTile.frames.Add(tileFrame);
                }
            }
        }
Пример #12
0
        private void AddTileObjectComponentsTo(GameObject gameObject, XElement goXml)
        {
            var tileXml = goXml.Element("TileObjectComponent");

            if (tileXml != null)
            {
                TileObject tileObject = gameObject.AddComponent <TileObject>();
                FillBaseTmxObjectProperties(tileObject, tileXml);
                tileObject.TmxFlippingHorizontal = ImportUtils.GetAttributeAsBoolean(tileXml, "tmx-tile-flip-horizontal", false);
                tileObject.TmxFlippingVertical   = ImportUtils.GetAttributeAsBoolean(tileXml, "tmx-tile-flip-vertical", false);
                tileObject.TileWidth             = ImportUtils.GetAttributeAsFloat(tileXml, "width");
                tileObject.TileHeight            = ImportUtils.GetAttributeAsFloat(tileXml, "height");
            }
        }
Пример #13
0
        private void AddTileAnimatorsTo(GameObject gameObject, XElement goXml)
        {
            foreach (var animXml in goXml.Elements("TileAnimator"))
            {
                TileAnimator tileAnimator = gameObject.AddComponent <TileAnimator>();

                foreach (var frameXml in animXml.Elements("Frame"))
                {
                    TileAnimator.Frame frame = new TileAnimator.Frame();
                    frame.Vertex_z   = ImportUtils.GetAttributeAsFloat(frameXml, "vertex_z");
                    frame.DurationMs = ImportUtils.GetAttributeAsInt(frameXml, "duration");
                    tileAnimator.frames.Add(frame);
                }
            }
        }
Пример #14
0
        private void AssignOpacityTo(GameObject gameObject, XElement xml, ImportBehaviour importComponent)
        {
            float opacity = ImportUtils.GetAttributeAsFloat(xml, "opacity", 1.0f);

            if (opacity == 1.0f)
            {
                return;
            }

#if UNITY_5_6_OR_NEWER
            // Add a component that will control our instanced shader properties
            Tiled2Unity.GPUInstancing instancing = gameObject.GetOrAddComponent <Tiled2Unity.GPUInstancing>();
            instancing.Opacity = opacity;
#endif
        }
 private void HandleTiledAttributes(GameObject gameObject, XElement goXml)
 {
     // Add the TiledMap component
     TiledMap map = gameObject.AddComponent<TiledMap>();
     try
     {
         map.NumTilesWide = ImportUtils.GetAttributeAsInt(goXml, "numTilesWide");
         map.NumTilesHigh = ImportUtils.GetAttributeAsInt(goXml, "numTilesHigh");
         map.TileWidth = ImportUtils.GetAttributeAsInt(goXml, "tileWidth");
         map.TileHeight = ImportUtils.GetAttributeAsInt(goXml, "tileHeight");
         map.ExportScale = ImportUtils.GetAttributeAsFloat(goXml, "exportScale");
     }
     catch
     {
         Debug.LogWarning(String.Format("Error adding TiledMap component. Are you using an old version of Tiled2Unity in your Unity project?"));
         GameObject.DestroyImmediate(map);
     }
 }
        private void CreatePrefab(XElement xmlPrefab, string objPath)
        {
            var customImporters = GetCustomImporterInstances();

            // Part 1: Create the prefab
            string     prefabName  = xmlPrefab.Attribute("name").Value;
            float      prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f);
            GameObject tempPrefab  = new GameObject(prefabName);

            HandleCustomProperties(tempPrefab, xmlPrefab, customImporters);

            // Part 2: Build out the prefab
            AddGameObjectsTo(tempPrefab, xmlPrefab, objPath, customImporters);

            // Part 3: Allow for customization from other editor scripts to be made on the prefab
            // (These are generally for game-specific needs)
            CustomizePrefab(tempPrefab, customImporters);

            // Part 3.5: Apply the scale only after all children have been added
            tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale);

            // Part 4: Save the prefab, keeping references intact.
            string prefabPath = ImportUtils.GetPrefabPath(prefabName);

            UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));

            if (finalPrefab == null)
            {
                // The prefab needs to be created
                ImportUtils.ReadyToWrite(prefabPath);
                finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
            }

            // Replace the prefab, keeping connections based on name.
            PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased);

            // Destroy the instance from the current scene hiearchy.
            UnityEngine.Object.DestroyImmediate(tempPrefab);
        }
Пример #17
0
        private void AddGameObjectsTo(GameObject parent, XElement xml, bool isParentTrigger, string objPath, IList <ICustomTiledImporter> customImporters)
        {
            foreach (XElement goXml in xml.Elements("GameObject"))
            {
                string name = ImportUtils.GetAttributeAsString(goXml, "name", "");

                // The normal map layer does not need to be included in the mesh. It is only used so that the texture gets imported from tiled to Unity by Tiled2Unity tool
                if (name.Contains(ImportTiled2Unity.TiledGameObjectName_NormalMapLayer))
                {
                    continue;
                }

                string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", "");

                GameObject child = null;
                if (!String.IsNullOrEmpty(copyFrom))
                {
                    child = CreateCopyFromMeshObj(copyFrom, objPath);
                    if (child == null)
                    {
                        // We're in trouble. Errors should already be in the log.
                        return;
                    }
                }
                else
                {
                    child = new GameObject();
                }

                if (!String.IsNullOrEmpty(name))
                {
                    child.name = name;
                }

                // Set the position
                float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0);
                float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0);
                child.transform.position = new Vector3(x, y, 0);

                // Set the rotation
                float r = ImportUtils.GetAttributeAsFloat(goXml, "rotation", 0);
                if (r != 0)
                {
                    // Use negative 'r' because of change in coordinate systems between Tiled and Unity
                    child.transform.eulerAngles = new Vector3(0, 0, -r);
                }

                // Assign the child to the parent
                child.transform.parent = parent.transform;

                // Add any tile animators
                AddTileAnimatorsTo(child, goXml);

                // Do we have any collision data?
                // Check if we are setting 'isTrigger' for ourselves or for our childen
                bool isTrigger = ImportUtils.GetAttributeAsBoolean(goXml, "isTrigger", isParentTrigger);
                AddCollidersTo(child, isTrigger, goXml);

                // Do we have any children of our own?
                AddGameObjectsTo(child, goXml, isTrigger, objPath, customImporters);

                // Does this game object have a tag?
                AssignTagTo(child, goXml);

                // Does this game object have a layer?
                AssignLayerTo(child, goXml);

                // Are there any custom properties?
                HandleCustomProperties(child, goXml, customImporters);
            }
        }
        private void AddGameObjectsTo(GameObject parent, XElement xml, bool isParentTrigger, string objPath, IList <ICustomTiledImporter> customImporters)
        {
            foreach (XElement goXml in xml.Elements("GameObject"))
            {
                string name     = ImportUtils.GetAttributeAsString(goXml, "name", "");
                string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", "");

                GameObject child = null;
                if (!String.IsNullOrEmpty(copyFrom))
                {
                    float opacity = ImportUtils.GetAttributeAsFloat(goXml, "opacity", 1);
                    child = CreateCopyFromMeshObj(copyFrom, objPath, opacity);
                    if (child == null)
                    {
                        // We're in trouble. Errors should already be in the log.
                        return;
                    }

                    // Apply the sorting to the renderer of the mesh object we just copied into the child
                    Renderer renderer = child.GetComponent <Renderer>();

                    string sortingLayer = ImportUtils.GetAttributeAsString(goXml, "sortingLayerName", "");
                    if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer))
                    {
                        Debug.LogError(string.Format("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer));
                        renderer.sortingLayerName = "Default";
                    }
                    else
                    {
                        renderer.sortingLayerName = sortingLayer;
                    }

                    // Set the sorting order
                    renderer.sortingOrder = ImportUtils.GetAttributeAsInt(goXml, "sortingOrder", 0);
                }
                else
                {
                    child = new GameObject();
                }

                if (!String.IsNullOrEmpty(name))
                {
                    child.name = name;
                }

                // Assign the child to the parent
                child.transform.parent = parent.transform;

                // Set the position
                float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0);
                float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0);
                child.transform.localPosition = new Vector3(x, y, 0);

                // Add any tile animators
                AddTileAnimatorsTo(child, goXml);

                // Do we have any collision data?
                // Check if we are setting 'isTrigger' for ourselves or for our childen
                bool isTrigger = ImportUtils.GetAttributeAsBoolean(goXml, "isTrigger", isParentTrigger);
                AddCollidersTo(child, isTrigger, goXml);

                // Do we have any children of our own?
                AddGameObjectsTo(child, goXml, isTrigger, objPath, customImporters);

                // Does this game object have a tag?
                AssignTagTo(child, goXml);

                // Does this game object have a layer?
                AssignLayerTo(child, goXml);

                // Are there any custom properties?
                HandleCustomProperties(child, goXml, customImporters);

                // Set scale and rotation *after* children are added otherwise Unity will have child+parent transform cancel each other out
                float sx = ImportUtils.GetAttributeAsFloat(goXml, "scaleX", 1.0f);
                float sy = ImportUtils.GetAttributeAsFloat(goXml, "scaleY", 1.0f);
                child.transform.localScale = new Vector3(sx, sy, 1.0f);

                // Set the rotation
                // Use negative rotation on the z component because of change in coordinate systems between Tiled and Unity
                Vector3 localRotation = new Vector3();
                localRotation.x             = (ImportUtils.GetAttributeAsBoolean(goXml, "flipY", false) == true) ? 180.0f : 0.0f;
                localRotation.y             = (ImportUtils.GetAttributeAsBoolean(goXml, "flipX", false) == true) ? 180.0f : 0.0f;
                localRotation.z             = -ImportUtils.GetAttributeAsFloat(goXml, "rotation", 0);
                child.transform.eulerAngles = localRotation;
            }
        }
Пример #19
0
 private void SetLayerComponentProperties(Tiled2Unity.Layer layer, XElement xml)
 {
     layer.Offset = new Vector2 {
         x = ImportUtils.GetAttributeAsFloat(xml, "offsetX", 0), y = ImportUtils.GetAttributeAsFloat(xml, "offsetY", 0)
     };
 }
Пример #20
0
        private void AddCollidersTo(GameObject gameObject, bool isTrigger, XElement xml)
        {
            // Box colliders
            foreach (XElement xmlBoxCollider2D in xml.Elements("BoxCollider2D"))
            {
                BoxCollider2D collider = gameObject.AddComponent <BoxCollider2D>();
                collider.isTrigger = isTrigger;
                float width  = ImportUtils.GetAttributeAsFloat(xmlBoxCollider2D, "width");
                float height = ImportUtils.GetAttributeAsFloat(xmlBoxCollider2D, "height");
                collider.size = new Vector2(width, height);

#if T2U_IS_UNITY_4
                collider.center = new Vector2(width * 0.5f, -height * 0.5f);
#else
                collider.offset = new Vector2(width * 0.5f, -height * 0.5f);
#endif
                // Apply the offsets (if any)
                float offset_x = ImportUtils.GetAttributeAsFloat(xmlBoxCollider2D, "offsetX", 0);
                float offset_y = ImportUtils.GetAttributeAsFloat(xmlBoxCollider2D, "offsetY", 0);

#if T2U_IS_UNITY_4
                collider.center += new Vector2(offset_x, offset_y);
#else
                collider.offset += new Vector2(offset_x, offset_y);
#endif
            }

            // Circle colliders
            foreach (XElement xmlCircleCollider2D in xml.Elements("CircleCollider2D"))
            {
                CircleCollider2D collider = gameObject.AddComponent <CircleCollider2D>();
                collider.isTrigger = isTrigger;
                float radius = ImportUtils.GetAttributeAsFloat(xmlCircleCollider2D, "radius");
                collider.radius = radius;
#if T2U_IS_UNITY_4
                collider.center = new Vector2(radius, -radius);
#else
                collider.offset = new Vector2(radius, -radius);
#endif

                // Apply the offsets (if any)
                float offset_x = ImportUtils.GetAttributeAsFloat(xmlCircleCollider2D, "offsetX", 0);
                float offset_y = ImportUtils.GetAttributeAsFloat(xmlCircleCollider2D, "offsetY", 0);

#if T2U_IS_UNITY_4
                collider.center += new Vector2(offset_x, offset_y);
#else
                collider.offset += new Vector2(offset_x, offset_y);
#endif
            }

            // Edge colliders
            foreach (XElement xmlEdgeCollider2D in xml.Elements("EdgeCollider2D"))
            {
                EdgeCollider2D collider = gameObject.AddComponent <EdgeCollider2D>();
                collider.isTrigger = isTrigger;
                string data = xmlEdgeCollider2D.Element("Points").Value;

                // The data looks like this:
                //  x0,y0 x1,y1 x2,y2 ...
                var points = from pt in data.Split(' ')
                             let x                 = Convert.ToSingle(pt.Split(',')[0], CultureInfo.InvariantCulture)
                                             let y = Convert.ToSingle(pt.Split(',')[1], CultureInfo.InvariantCulture)
                                                     select new Vector2(x, y);

                collider.points = points.ToArray();

                // Apply the offsets (if any)
                float offset_x = ImportUtils.GetAttributeAsFloat(xmlEdgeCollider2D, "offsetX", 0);
                float offset_y = ImportUtils.GetAttributeAsFloat(xmlEdgeCollider2D, "offsetY", 0);

#if T2U_IS_UNITY_4
                // This is kind of a hack for Unity 4.x which doesn't support offset/center on the edge collider
                var offsetPoints = from pt in points
                                   select new Vector2(pt.x + offset_x, pt.y + offset_y);
                collider.points = offsetPoints.ToArray();
#else
                collider.offset += new Vector2(offset_x, offset_y);
#endif
            }

            // Polygon colliders
            foreach (XElement xmlPolygonCollider2D in xml.Elements("PolygonCollider2D"))
            {
                PolygonCollider2D collider = gameObject.AddComponent <PolygonCollider2D>();
                collider.isTrigger = isTrigger;

                // Apply the offsets (if any)
                float offset_x = ImportUtils.GetAttributeAsFloat(xmlPolygonCollider2D, "offsetX", 0);
                float offset_y = ImportUtils.GetAttributeAsFloat(xmlPolygonCollider2D, "offsetY", 0);

                var paths = xmlPolygonCollider2D.Elements("Path").ToArray();
                collider.pathCount = paths.Count();

                for (int p = 0; p < collider.pathCount; ++p)
                {
                    string data = paths[p].Value;

                    // The data looks like this:
                    //  x0,y0 x1,y1 x2,y2 ...
                    var points = from pt in data.Split(' ')
                                 let x                 = Convert.ToSingle(pt.Split(',')[0], CultureInfo.InvariantCulture)
                                                 let y = Convert.ToSingle(pt.Split(',')[1], CultureInfo.InvariantCulture)
#if T2U_IS_UNITY_4
                                                         // Hack for Unity 4.x
                                                         select new Vector2(x + offset_x, y + offset_y);
#else
                                                         select new Vector2(x, y);
#endif

                    collider.SetPath(p, points.ToArray());
                }

#if !T2U_IS_UNITY_4
                collider.offset += new Vector2(offset_x, offset_y);
#endif
            }
        }
Пример #21
0
        private void AddGameObjectsTo(GameObject parent, XElement xml, bool isParentTrigger, ImportBehaviour importComponent, IList <ICustomTiledImporter> customImporters)
        {
            foreach (XElement goXml in xml.Elements("GameObject"))
            {
                string name     = ImportUtils.GetAttributeAsString(goXml, "name", "");
                string copyFrom = ImportUtils.GetAttributeAsString(goXml, "copy", "");
                string mesh     = ImportUtils.GetAttributeAsString(goXml, "mesh", "");

                GameObject child = null;

                if (!String.IsNullOrEmpty(mesh))
                {
                    child = CreateGameObjectWithMesh(mesh, importComponent);
                }
                else if (!String.IsNullOrEmpty(copyFrom))
                {
                    child = CreateCopyFromMeshObj(copyFrom, importComponent);
                }
                else
                {
                    child = new GameObject();
                }

                if (child == null)
                {
                    importComponent.RecordError("Error creating child object '{0}'", name);
                    return;
                }

                // Assign the given name to our child
                if (!String.IsNullOrEmpty(name))
                {
                    child.name = name;
                }

                // Assign the child to the parent
                child.transform.parent = parent.transform;

                // Set the position
                float x = ImportUtils.GetAttributeAsFloat(goXml, "x", 0);
                float y = ImportUtils.GetAttributeAsFloat(goXml, "y", 0);
                float z = ImportUtils.GetAttributeAsFloat(goXml, "z", 0);
                child.transform.localPosition = new Vector3(x, y, z);

                // Add any layer components
                AddTileLayerComponentsTo(child, goXml);
                AddObjectLayerComponentsTo(child, goXml);
                AddGroupLayerComponentsTo(child, goXml);

                // Add any object group items
                AddTmxObjectComponentsTo(child, goXml);
                AddRectangleObjectComponentsTo(child, goXml);
                AddCircleObjectComponentsTo(child, goXml);
                AddPolygonObjectComponentsTo(child, goXml);
                AddPolylineObjectComponentsTo(child, goXml);
                AddTileObjectComponentsTo(child, goXml);

                // Add any tile animators
                AddTileAnimatorsTo(child, goXml);

                // Do we have any collision data?
                // Check if we are setting 'isTrigger' for ourselves or for our children
                bool isTrigger = ImportUtils.GetAttributeAsBoolean(goXml, "isTrigger", isParentTrigger);
                AddCollidersTo(child, isTrigger, goXml);

                // Do we have any children of our own?
                AddGameObjectsTo(child, goXml, isTrigger, importComponent, customImporters);

                // Does this game object have a tag?
                AssignTagTo(child, goXml, importComponent);

                // Does this game object have a layer?
                AssignLayerTo(child, goXml, importComponent);

                // Assign from various attributes
                AssignOpacityTo(child, goXml, importComponent);
                AssignSortingLayerNameTo(child, goXml, importComponent);
                AssignSortingOrderTo(child, goXml, importComponent);

                // Set scale and rotation *after* children are added otherwise Unity will have child+parent transform cancel each other out
                float sx = ImportUtils.GetAttributeAsFloat(goXml, "scaleX", 1.0f);
                float sy = ImportUtils.GetAttributeAsFloat(goXml, "scaleY", 1.0f);
                child.transform.localScale = new Vector3(sx, sy, 1.0f);

                // Set the rotation
                // Use negative rotation on the z component because of change in coordinate systems between Tiled and Unity
                Vector3 localRotation = new Vector3();
                localRotation.z             = -ImportUtils.GetAttributeAsFloat(goXml, "rotation", 0);
                child.transform.eulerAngles = localRotation;

                // Are there any custom properties? (This comes last - after all transformations have taken place.)
                HandleCustomProperties(child, goXml, customImporters);
            }
        }