Пример #1
0
        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();
                    }
                }
            }
        }
Пример #2
0
 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();
 }
Пример #3
0
 void UndoRedo()
 {
     if (target != null)
     {
         tileMap.tmxFile = TMXFile.Load(tileMap.tmxFileString, path);
         tileMap.ReloadMap();
         treeView.Reload();
     }
 }
Пример #4
0
 public void OnAfterDeserialize()
 {
     if (string.IsNullOrEmpty(tmxFileString))
     {
         _tmxFile = null;
     }
     else
     {
         tmxFile = TMXFile.Load(tmxFileString, tmxFilePath);
     }
 }
Пример #5
0
 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);
 }
Пример #6
0
        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();
        }