private void Export()
        {
                        #if UNITY_WEBPLAYER
            ACDebug.LogWarning("Game text cannot be exported in WebPlayer mode - please switch platform and try again.");
                        #else
            if (variablesManager == null || exportColumns == null || exportColumns.Count == 0)
            {
                return;
            }

            if (variableLocation == VariableLocation.Local && allScenes)
            {
                bool canProceed = EditorUtility.DisplayDialog("Export variables", "AC will now go through your game, and collect all variables to be exported.\n\nIt is recommended to back up your project beforehand.", "OK", "Cancel");
                if (!canProceed)
                {
                    return;
                }

                if (!UnityEditor.SceneManagement.EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
                {
                    return;
                }
            }

            string suggestedFilename = "";
            if (AdvGame.GetReferences().settingsManager)
            {
                suggestedFilename = AdvGame.GetReferences().settingsManager.saveFileName + " - ";
            }
            if (variableLocation == VariableLocation.Local && allScenes)
            {
                suggestedFilename += " All ";
            }
            suggestedFilename += variableLocation.ToString() + " Variables.csv";

            string fileName = EditorUtility.SaveFilePanel("Export variables", "Assets", suggestedFilename, "csv");
            if (fileName.Length == 0)
            {
                return;
            }

            List <string[]> output = new List <string[]>();

            List <string> headerList = new List <string>();
            headerList.Add("ID");
            foreach (ExportColumn exportColumn in exportColumns)
            {
                headerList.Add(exportColumn.GetHeader());
            }
            output.Add(headerList.ToArray());

            // Global
            if (variableLocation == VariableLocation.Global)
            {
                List <GVar> exportVars = new List <GVar>();
                foreach (GVar globalVariable in variablesManager.vars)
                {
                    exportVars.Add(new GVar(globalVariable));
                }

                foreach (GVar exportVar in exportVars)
                {
                    List <string> rowList = new List <string>();
                    rowList.Add(exportVar.id.ToString());
                    foreach (ExportColumn exportColumn in exportColumns)
                    {
                        string cellText = exportColumn.GetCellText(exportVar, VariableLocation.Global, replaceForwardSlashes);
                        rowList.Add(cellText);
                    }
                    output.Add(rowList.ToArray());
                }
            }

            // Local
            else if (variableLocation == VariableLocation.Local)
            {
                if (allScenes)
                {
                    string   originalScene = UnityVersionHandler.GetCurrentSceneFilepath();
                    string[] sceneFiles    = AdvGame.GetSceneFiles();
                    foreach (string sceneFile in sceneFiles)
                    {
                        UnityVersionHandler.OpenScene(sceneFile);

                        if (FindObjectOfType <LocalVariables>())
                        {
                            LocalVariables localVariables = FindObjectOfType <LocalVariables>();
                            if (localVariables != null)
                            {
                                string sceneName = UnityVersionHandler.GetCurrentSceneName();
                                output = GatherOutput(output, localVariables.localVars, sceneName);
                            }
                        }
                    }

                    if (string.IsNullOrEmpty(originalScene))
                    {
                        UnityVersionHandler.NewScene();
                    }
                    else
                    {
                        UnityVersionHandler.OpenScene(originalScene);
                    }
                }
                else
                {
                    string sceneName = UnityVersionHandler.GetCurrentSceneName();
                    output = GatherOutput(output, KickStarter.localVariables.localVars, sceneName);
                }
            }

            // Component
            else if (variableLocation == VariableLocation.Component)
            {
                string sceneName = UnityVersionHandler.GetCurrentSceneName();
                if (variables != null)
                {
                    output = GatherOutput(output, variables.vars, sceneName);
                }
            }

            string fileContents = CSVReader.CreateCSVGrid(output);
            if (!string.IsNullOrEmpty(fileContents) && Serializer.SaveFile(fileName, fileContents))
            {
                int numExported = output.Count - 1;
                if (numExported == 1)
                {
                    ACDebug.Log("1 " + variableLocation + " variable exported.");
                }
                else
                {
                    ACDebug.Log(numExported.ToString() + " " + variableLocation + " variables exported.");
                }
            }
                        #endif
        }
Esempio n. 2
0
        private void Export()
        {
                        #if UNITY_WEBPLAYER
            ACDebug.LogWarning("Game text cannot be exported in WebPlayer mode - please switch platform and try again.");
                        #else
            if (variablesManager == null || exportColumns == null || exportColumns.Count == 0)
            {
                return;
            }

            if (variableLocation == VariableLocation.Local && allScenes)
            {
                bool canProceed = EditorUtility.DisplayDialog("Export variables", "AC will now go through your game, and collect all variables to be exported.\n\nIt is recommended to back up your project beforehand.", "OK", "Cancel");
                if (!canProceed)
                {
                    return;
                }

                if (!UnityVersionHandler.SaveSceneIfUserWants())
                {
                    return;
                }
            }

            string suggestedFilename = "";
            if (AdvGame.GetReferences().settingsManager)
            {
                suggestedFilename = AdvGame.GetReferences().settingsManager.saveFileName + " - ";
            }
            if (variableLocation == VariableLocation.Local && allScenes)
            {
                suggestedFilename += " All ";
            }
            suggestedFilename += variableLocation.ToString() + " Variables.csv";

            string fileName = EditorUtility.SaveFilePanel("Export variables", "Assets", suggestedFilename, "csv");
            if (fileName.Length == 0)
            {
                return;
            }

            bool            fail   = false;
            List <string[]> output = new List <string[]>();

            List <string> headerList = new List <string>();
            headerList.Add("ID");
            foreach (ExportColumn exportColumn in exportColumns)
            {
                headerList.Add(exportColumn.GetHeader());
            }
            output.Add(headerList.ToArray());

            // Global
            if (variableLocation == VariableLocation.Global)
            {
                List <GVar> exportVars = new List <GVar>();
                foreach (GVar globalVariable in variablesManager.vars)
                {
                    exportVars.Add(new GVar(globalVariable));
                }

                foreach (GVar exportVar in exportVars)
                {
                    List <string> rowList = new List <string>();
                    rowList.Add(exportVar.id.ToString());
                    foreach (ExportColumn exportColumn in exportColumns)
                    {
                        string cellText = exportColumn.GetCellText(exportVar, VariableLocation.Global, replaceForwardSlashes);
                        rowList.Add(cellText);

                        if (cellText.Contains(CSVReader.csvDelimiter))
                        {
                            fail = true;
                            ACDebug.LogError("Cannot export variables since global variable " + exportVar.id.ToString() + " (" + exportVar.label + ") contains the character '" + CSVReader.csvDelimiter + "'.");
                        }
                    }
                    output.Add(rowList.ToArray());
                }
            }

            // Local
            else if (variableLocation == VariableLocation.Local)
            {
                if (allScenes)
                {
                    string   originalScene = UnityVersionHandler.GetCurrentSceneFilepath();
                    string[] sceneFiles    = AdvGame.GetSceneFiles();
                    foreach (string sceneFile in sceneFiles)
                    {
                        UnityVersionHandler.OpenScene(sceneFile);

                        if (FindObjectOfType <LocalVariables>())
                        {
                            LocalVariables localVariables = FindObjectOfType <LocalVariables>();
                            if (localVariables != null)
                            {
                                string sceneName = UnityVersionHandler.GetCurrentSceneName();
                                output = GatherOutput(output, localVariables.localVars, sceneName);
                            }
                        }
                    }

                    if (originalScene == "")
                    {
                        UnityVersionHandler.NewScene();
                    }
                    else
                    {
                        UnityVersionHandler.OpenScene(originalScene);
                    }
                }
                else
                {
                    string sceneName = UnityVersionHandler.GetCurrentSceneName();
                    output = GatherOutput(output, KickStarter.localVariables.localVars, sceneName);
                }
            }

            // Component
            else if (variableLocation == VariableLocation.Component)
            {
                string sceneName = UnityVersionHandler.GetCurrentSceneName();
                if (variables != null)
                {
                    output = GatherOutput(output, variables.vars, sceneName);
                }
            }

            if (!fail)
            {
                int length = output.Count;

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                for (int j = 0; j < length; j++)
                {
                    sb.AppendLine(string.Join(CSVReader.csvDelimiter, output[j]));
                }

                if (Serializer.SaveFile(fileName, sb.ToString()))
                {
                    int numExported = output.Count - 1;
                    if (numExported == 1)
                    {
                        ACDebug.Log("1 " + variableLocation + " variable exported.");
                    }
                    else
                    {
                        ACDebug.Log(numExported.ToString() + " " + variableLocation + " variables exported.");
                    }
                }
            }
                        #endif
        }