コード例 #1
0
ファイル: SpriteCache.cs プロジェクト: zhangzhibin/UnityTiled
        public SpriteCache(Map map)
        {
            foreach (var tileset in map.tileSets)
            {
                var info = new TilesetInfo();
                info.firstGid  = tileset.firstGid;
                info.assetPath = TiledHelpers.GetAssetPath(tileset.source);

                var sprites = AssetDatabase.LoadAllAssetsAtPath(info.assetPath)
                              .OfType <Sprite>()
                              .ToArray();

                info.lastGid = (uint)(info.firstGid + sprites.Length - 1);
                _tilesetInfo.Add(info);
            }
        }
コード例 #2
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            var map = (TiledMap)target;

            if (GUILayout.Button("Locate TMX Map"))
            {
                var file = EditorUtility.OpenFilePanel("Choose TMX File", Application.dataPath, "tmx,xml");
                if (!string.IsNullOrEmpty(file))
                {
                    map.tmxFile = TiledHelpers.GetAssetPath(file);
                }
            }

            if (string.IsNullOrEmpty(map.tmxFile) && !File.Exists(map.tmxFile))
            {
                EditorGUILayout.HelpBox("TMX file path required.", MessageType.Error);
                GUI.enabled = false;
            }
            else
            {
                if (GUILayout.Button("Import"))
                {
                    for (int i = map.transform.childCount - 1; i >= 0; i--)
                    {
                        DestroyImmediate(map.transform.GetChild(i).gameObject);
                    }

                    var tmxMap      = Map.FromFile(map.tmxFile);
                    var spriteCache = new SpriteCache(tmxMap);

                    var allCreatedObjects = new List <GameObject>();

                    var layerZ = 0;
                    foreach (var layer in tmxMap.layers)
                    {
                        var layerObject = new GameObject(layer.name);
                        layerObject.isStatic = true;
                        layerObject.transform.SetParent(map.transform);
                        layerObject.transform.SetAsFirstSibling();
                        layerObject.transform.localPosition = new Vector3(0, 0, layerZ--);

                        var tileLayer = layer as TileLayer;
                        if (tileLayer != null)
                        {
                            CreateSpritesForLayer(spriteCache, layerObject, tileLayer, tmxMap, allCreatedObjects);
                        }

                        var objectGroup = layer as ObjectGroup;
                        if (objectGroup != null)
                        {
                            CreateObjects(tmxMap, layer, layerObject, objectGroup, allCreatedObjects);
                        }
                    }

                    // Inform each created prefab individually know that the map was imported
                    SendOnTmxMapImported(allCreatedObjects, true);

                    // When sending the message to the map, don't send it to all the children again
                    SendOnTmxMapImported(map.gameObject, false);

                    // Remove all TileProperties components; they're just created for post processing the import process
                    var tilePropertyComponents = map.GetComponentsInChildren <TileProperties>().ToList();
                    foreach (var comp in tilePropertyComponents)
                    {
                        DestroyImmediate(comp);
                    }
                }
            }
        }