Пример #1
0
 private void OpenDemoWorld(string mapName, string templateName)
 {
     if (voxelArray == null || SelectedMap.Instance().mapName != mapName)
     {
         // create and load the file
         string filePath = WorldFiles.GetFilePath(mapName);
         if (!File.Exists(filePath))
         {
             TextAsset mapText = Resources.Load <TextAsset>(templateName);
             using (FileStream fileStream = File.Create(filePath))
             {
                 using (var sw = new StreamWriter(fileStream))
                 {
                     sw.Write(mapText.text);
                     sw.Flush();
                 }
             }
         }
         if (voxelArray != null)
         {
             voxelArray.GetComponent <EditorFile>().Save();
         }
         SelectedMap.Instance().mapName = mapName;
         SceneManager.LoadScene("editScene");
     }
     Destroy(this);
 }
Пример #2
0
    public void Write(Transform cameraPivot, VoxelArray voxelArray)
    {
        if (voxelArray.IsEmpty())
        {
            Debug.Log("World is empty! File will not be written.");
            return;
        }

        JSONObject root = new JSONObject();

        root["writerVersion"]    = VERSION;
        root["minReaderVersion"] = FILE_MIN_READER_VERSION;

        JSONObject camera = new JSONObject();

        camera["pan"]    = WriteVector3(cameraPivot.position);
        camera["rotate"] = WriteQuaternion(cameraPivot.rotation);
        camera["scale"]  = cameraPivot.localScale.z;
        root["camera"]   = camera;

        root["world"] = WriteWorld(voxelArray);

        string filePath = WorldFiles.GetFilePath(fileName);

        using (FileStream fileStream = File.Create(filePath))
        {
            using (var sw = new StreamWriter(fileStream))
            {
                sw.Write(root.ToString());
                sw.Flush();
            }
        }
    }
Пример #3
0
    private void ImportMap(string name)
    {
        if (name.Length == 0)
        {
            Close();
            return;
        }
        string newPath = WorldFiles.GetFilePath(name);

        if (File.Exists(newPath))
        {
            var dialog = DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            dialog.yesButtonHandler = Close;
            return;
        }

        try
        {
            using (FileStream fileStream = File.Create(newPath))
            {
                ShareMap.ReadSharedURLAndroid(fileStream);
            }
            Close();
        }
        catch (System.Exception e)
        {
            Debug.Log(e);
            var dialog = DialogGUI.ShowMessageDialog(gameObject, "Error importing world");
            dialog.yesButtonHandler = Close;
        }
    }
Пример #4
0
    private void CopyMap(string newName)
    {
        if (newName.Length == 0)
        {
            return;
        }
        string newPath = WorldFiles.GetFilePath(newName);

        if (File.Exists(newPath))
        {
            DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            return;
        }
        File.Copy(WorldFiles.GetFilePath(selectedWorld), newPath);
        UpdateMapList();
    }
Пример #5
0
    private void CreateWorldOverflowMenu(string fileName)
    {
        worldOverflowMenu       = gameObject.AddComponent <OverflowMenuGUI>();
        selectedWorld           = fileName;
        worldOverflowMenu.items = new OverflowMenuGUI.MenuItem[]
        {
            new OverflowMenuGUI.MenuItem("Play", GUIIconSet.instance.play, () => {
                MenuGUI.OpenMap(fileName, "playScene");
            }),
            new OverflowMenuGUI.MenuItem("Rename", GUIIconSet.instance.rename, () => {
                TextInputDialogGUI inputDialog = gameObject.AddComponent <TextInputDialogGUI>();
                inputDialog.prompt             = "Enter new name for " + fileName;
                inputDialog.handler            = RenameMap;
            }),
            new OverflowMenuGUI.MenuItem("Copy", GUIIconSet.instance.copy, () => {
                TextInputDialogGUI inputDialog = gameObject.AddComponent <TextInputDialogGUI>();
                inputDialog.prompt             = "Enter new world name...";
                inputDialog.handler            = CopyMap;
            }),
            new OverflowMenuGUI.MenuItem("Delete", GUIIconSet.instance.delete, () => {
                DialogGUI dialog        = gameObject.AddComponent <DialogGUI>();
                dialog.message          = "Are you sure you want to delete " + fileName + "?";
                dialog.yesButtonText    = "Yes";
                dialog.noButtonText     = "No";
                dialog.yesButtonHandler = () =>
                {
                    File.Delete(WorldFiles.GetFilePath(fileName));
                    UpdateMapList();
                };
            }),
#if UNITY_ANDROID
            new OverflowMenuGUI.MenuItem("Share", GUIIconSet.instance.share, () => {
                string path = WorldFiles.GetFilePath(fileName);
                ShareMap.ShareAndroid(path);
            })
#endif
        };
    }
Пример #6
0
    private void NewMap(string name)
    {
        if (name.Length == 0)
        {
            return;
        }
        string filePath = WorldFiles.GetFilePath(name);

        if (File.Exists(filePath))
        {
            DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            return;
        }
        using (FileStream fileStream = File.Create(filePath))
        {
            using (var sw = new StreamWriter(fileStream))
            {
                sw.Write(defaultMap.text);
                sw.Flush();
            }
        }
        UpdateMapList();
    }
Пример #7
0
    // return warnings
    public List <string> Read(Transform cameraPivot, VoxelArray voxelArray, bool editor)
    {
        this.editor = editor;
        if (missingMaterial == null)
        {
            // allowTransparency is true in case the material is used for an overlay, so the alpha value can be adjusted
            missingMaterial       = ResourcesDirectory.MakeCustomMaterial(ColorMode.UNLIT, true);
            missingMaterial.color = Color.magenta;
        }
        string jsonString;

        try
        {
            string filePath = WorldFiles.GetFilePath(fileName);
            using (FileStream fileStream = File.Open(filePath, FileMode.Open))
            {
                using (var sr = new StreamReader(fileStream))
                {
                    jsonString = sr.ReadToEnd();
                }
            }
        }
        catch (Exception e)
        {
            throw new MapReadException("An error occurred while reading the file", e);
        }

        JSONNode rootNode;

        try
        {
            rootNode = JSON.Parse(jsonString);
        }
        catch (Exception e)
        {
            throw new MapReadException("Invalid world file", e);
        }
        if (rootNode == null)
        {
            throw new MapReadException("Invalid world file");
        }
        JSONObject root = rootNode.AsObject;

        if (root == null || root["writerVersion"] == null || root["minReaderVersion"] == null)
        {
            throw new MapReadException("Invalid world file");
        }
        if (root["minReaderVersion"].AsInt > VERSION)
        {
            throw new MapReadException("This world file requires a newer version of the app");
        }
        fileWriterVersion = root["writerVersion"].AsInt;

        EntityReference.ResetEntityIds();

        try
        {
            if (editor && cameraPivot != null && root["camera"] != null)
            {
                ReadCamera(root["camera"].AsObject, cameraPivot);
            }
            if (root["world"] != null)
            {
                ReadWorld(root["world"].AsObject, voxelArray);
            }
        }
        catch (MapReadException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MapReadException("Error reading world file", e);
        }

        EntityReference.DoneLoadingEntities();
        return(warnings);
    }