public void Setup(TMXFile tmxFile, string tmxFilePath, float pixelsPerUnit = -1) { this.tmxFilePath = tmxFilePath; this.tmxFile = tmxFile; this.pixelsPerUnit = pixelsPerUnit; Setup(); }
void DrawFilePanel() { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Reload")) { tmxFile = TMXFile.Load(path); tileMap.tileSetMaterials = TileMapEditor.GetMaterials(tmxFile, path); tileMap.tileSetSprites = TileMapEditor.GetSprites(tmxFile, path); tileMap.Setup(); treeView.Reload(); } if (GUILayout.Button("Save")) { tmxFile.Save(path); AssetDatabase.ImportAsset(path); } if (GUILayout.Button("Save As")) { tmxFile.Save( EditorUtility.SaveFilePanel( "Save as TMX", Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path), Path.GetExtension(path).TrimStart(new char[] { '.' }) ) ); AssetDatabase.Refresh(); AssetDatabase.ImportAsset(path); } EditorGUILayout.EndHorizontal(); }
private static void DragAndDropTMXFile(bool isSceneView = false, Transform parent = null) { EventType eventType = Event.current.type; if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (eventType == EventType.DragPerform) { bool foundTMX = false; foreach (UnityEngine.Object o in DragAndDrop.objectReferences) { string tmxFilePath = AssetDatabase.GetAssetPath(o); if (Path.GetExtension(tmxFilePath) == ".tmx") { float pixelsPerUnit = -1; AssetImporter importer = AssetImporter.GetAtPath(tmxFilePath); if (!string.IsNullOrEmpty(importer.userData)) { pixelsPerUnit = float.Parse(importer.userData); } string name = Path.GetFileNameWithoutExtension(tmxFilePath); GameObject map = new GameObject(name); TileMap tileMap = map.AddComponent <TileMap>(); TMXFile tmxFile = TMXFile.Load(tmxFilePath); tileMap.tileSetMaterials = TileMapEditor.GetMaterials(tmxFile, tmxFilePath); tileMap.Setup(tmxFile, tmxFilePath, pixelsPerUnit); if (isSceneView) { Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); float dist = 0; Plane plane = new Plane(Vector3.forward, tileMap.transform.position); if (plane.Raycast(ray, out dist)) { map.transform.position = ray.GetPoint(dist); } } if (parent != null) { map.transform.SetParent(parent); } Event.current.Use(); Selection.activeGameObject = map; Undo.RegisterCreatedObjectUndo(map, "Created '" + name + "' from TMX file."); foundTMX = true; } } if (foundTMX) { DragAndDrop.AcceptDrag(); Event.current.Use(); } } } }
public static Sprite[][] GetSprites(TMXFile tmxFile, string path) { Sprite[][] tileSetSprites = new Sprite[tmxFile.tileSets.Length][]; for (int i = 0; i < tmxFile.tileSets.Length; i++) { TileSet tileSet = tmxFile.tileSets[i]; if (tileSet != null && tileSet.image != null && !string.IsNullOrEmpty(tileSet.image.source)) { continue; } List <Sprite> sprites = new List <Sprite>(); foreach (Tile tile in tileSet.tiles) { if (tile.image == null || string.IsNullOrEmpty(tile.image.source)) { continue; } string texturePath = Path.Combine(Path.GetDirectoryName(path), tile.image.source); Texture2D tex = GetImageTexture(tile.image, texturePath); Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GetAssetPath(tex)); sprites.Add(sprite); } tileSetSprites[i] = sprites.ToArray(); } return(tileSetSprites); }
void UndoRedo() { if (target != null) { tileMap.tmxFile = TMXFile.Load(tileMap.tmxFileString, path); tileMap.ReloadMap(); treeView.Reload(); } }
public void OnAfterDeserialize() { if (string.IsNullOrEmpty(tmxFileString)) { _tmxFile = null; } else { tmxFile = TMXFile.Load(tmxFileString, tmxFilePath); } }
void OnEnable() { Undo.undoRedoPerformed += UndoRedo; if (tmxFile == null) { tileMap.tmxFile = TMXFile.Load(path); tileMap.Setup(); } if (treeViewState == null) { treeViewState = new TreeViewState(); } treeView = new TileMapTreeView(tileMap, treeViewState); }
public static TMXFile Load(XmlTextReader xmlReader, string path) { XmlSerializer deserializer = new XmlSerializer(typeof(TMXFile)); TMXFile map = (TMXFile)deserializer.Deserialize(xmlReader); bool hasDocType = false; for (int i = 0; i < 3; i++) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.DocumentType) { hasDocType = true; break; } } map.hasDocType = hasDocType; xmlReader.Close(); if (map.tileSets == null) { return(map); } for (int i = 0; i < map.tileSets.Length; i++) { TileSet tileSet = map.tileSets[i]; if (tileSet.hasSource) { string tsxPath = tileSet.source; tsxPath = Path.Combine(Path.GetDirectoryName(path), tsxPath); tsxPath = Path.GetFullPath(tsxPath); TileSet tsxFile = TileSet.Load(tsxPath); tsxFile.firstGID = tileSet.firstGID; tsxFile.source = tileSet.source; tileSet = tsxFile; } if ((tileSet.columns == 0 || tileSet.tileCount == 0) && tileSet.image != null) { tileSet.columns = (tileSet.image.width - 2 * tileSet.margin + tileSet.spacing) / (tileSet.tileWidth + tileSet.spacing); tileSet.rows = (tileSet.image.height - 2 * tileSet.margin + tileSet.spacing) / (tileSet.tileHeight + tileSet.spacing); } map.tileSets[i] = tileSet; } return(map); }
public void CreateSpriteTile(GameObject group, TileObject tileObject) { int tileID = (int)tileObject.gid; GameObject g = new GameObject(tileObject.name); g.transform.SetParent(group.transform); float y = tmxFile.height * tmxFile.tileHeight - tileObject.y; g.transform.localPosition = new Vector3(tileObject.x, y, 0) / pixelsPerUnit; g.transform.localEulerAngles = Vector3.forward * -tileObject.rotation; SpriteRenderer sprite = g.AddComponent <SpriteRenderer>(); sprite.flipX = TMXFile.FlippedHorizontally(tileObject.gid); sprite.flipY = TMXFile.FlippedVertically(tileObject.gid); TileSet tileSet = tmxFile.GetTileSetByTileID(tileID); if (tileSet != null && tileSet.image != null && !string.IsNullOrEmpty(tileSet.image.source)) { g.transform.localScale = new Vector3(tileObject.width, tileObject.height, pixelsPerUnit) / pixelsPerUnit; int tileSetIndex = System.Array.IndexOf(tmxFile.tileSets, tileSet); Material mat = tileSetMaterials[tileSetIndex]; Texture2D tex = mat.mainTexture as Texture2D; TileRect r = tileSet.GetTileSpriteRect(tileID); Rect rect = new Rect(r.x, r.y, r.width, r.height); Vector2 pivot = Vector2.zero; sprite.sprite = Sprite.Create(tex, rect, pivot, pixelsPerUnit, 0, SpriteMeshType.FullRect); } else { int tileSetIndex = System.Array.IndexOf(tmxFile.tileSets, tileSet); sprite.sprite = tileSetSprites[tileSetIndex][tileID - tileSet.firstGID]; } if (idToPhysics.ContainsKey(tileID)) { float yOff = tmxFile.tileHeight / pixelsPerUnit; Vector2[] path = System.Array.ConvertAll(idToPhysics[tileID], (p) => new Vector2(p.x, p.y + yOff)); PolygonCollider2D poly = g.AddComponent <PolygonCollider2D>(); poly.pathCount = 1; poly.SetPath(0, path); } SetProperties(g, tileObject.properties); }
public static Material[] GetMaterials(TMXFile tmxFile, string path) { Material[] materials = new Material[tmxFile.tileSets.Length]; for (int i = 0; i < tmxFile.tileSets.Length; i++) { TileSet tileSet = tmxFile.tileSets[i]; if (tileSet == null || tileSet.image == null || string.IsNullOrEmpty(tileSet.image.source)) { continue; } Material mat = null; if (tileSetMaterials.ContainsKey(tileSet.image.source)) { mat = tileSetMaterials[tileSet.image.source]; } else { string materialPath = Path.Combine(Path.GetDirectoryName(tileSet.image.source), "Materials"); materialPath = Path.Combine(materialPath, Path.GetFileNameWithoutExtension(tileSet.image.source) + ".mat"); materialPath = Path.Combine(Path.GetDirectoryName(path), materialPath); materialPath = Path.GetFullPath(materialPath); string materialDir = Path.GetDirectoryName(materialPath); Directory.CreateDirectory(materialDir); string dataPath = Path.GetFullPath(Application.dataPath); materialPath = materialPath.Replace(dataPath, "Assets"); mat = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material; if (mat == null) { mat = new Material(Shader.Find("Unlit/Transparent")); mat.mainTexture = GetTileSetTexture(tileSet, path); AssetDatabase.CreateAsset(mat, materialPath); } } if (mat != null) { tileSetMaterials[tileSet.image.source] = mat; } materials[i] = mat; } return(materials); }
public override void OnInspectorGUI() { DefaultAsset asset = AssetDatabase.LoadAssetAtPath(path, typeof(DefaultAsset)) as DefaultAsset; asset = EditorGUILayout.ObjectField("TMX File", asset, typeof(DefaultAsset), false) as DefaultAsset; string assetPath = AssetDatabase.GetAssetPath(asset); if (path != assetPath && Path.GetExtension(assetPath) == ".tmx") { Undo.RecordObject(target, "Assign new TMX file"); path = assetPath; tmxFile = TMXFile.Load(assetPath); tileMap.Setup(); } if (path != null) { DrawFilePanel(); } tileMap.pivot = EditorGUILayout.Vector2Field("Pivot", tileMap.pivot); DrawLayers(); DrawTileSets(); }
public bool RotatedHexagonal120(int index) { return(TMXFile.RotatedHexagonal120(tileFlags[index])); }
public bool FlippedAntiDiagonally(int index) { return(TMXFile.FlippedAntiDiagonally(tileFlags[index])); }
public bool FlippedVertically(int index) { return(TMXFile.FlippedVertically(tileFlags[index])); }
public bool FlippedHorizontally(int index) { return(TMXFile.FlippedHorizontally(tileFlags[index])); }