Example #1
0
    string GetScenarioFolder()
    {
#if UNITY_EDITOR
        string path = Path.Combine(Path.Combine(Application.dataPath, "Editor"), "scenarios");
        if (Directory.CreateDirectory(path).Exists)
        {
            return(path);
        }
#elif UNITY_ANDROID
        try
        {
            using (AndroidJavaClass jc = new AndroidJavaClass("android.os.Environment"))
            {
                string docs   = jc.GetStatic <string>("DIRECTORY_DOCUMENTS");
                string root   = jc.CallStatic <AndroidJavaObject>("getExternalStoragePublicDirectory", docs).Call <string>("getPath");
                string folder = Path.Combine(root, FOLDER_NAME);
                if (Directory.CreateDirectory(folder).Exists)
                {
                    return(folder);
                }
                else
                {
                    DebugText.LogError("Could not create folder for scenarios");
                }
            }
        }
        catch (Exception e)
        {
            DebugText.LogError(e.Message);
        }
#endif
        return("");
    }
Example #2
0
    public void CreateExample()
    {
        if (dataFolder == "")
        {
            return;
        }
        string path = Path.Combine(dataFolder, "Example");

        if (!Directory.CreateDirectory(path).Exists)
        {
            DebugText.LogError("Could not create Example folder");
        }
        else
        {
            try
            {
                System.IO.File.WriteAllText(Path.Combine(path, "scenes.json"), exampleScenario.ToJson());
                System.IO.File.WriteAllText(Path.Combine(path, "instructions.txt"), exampleScenario.instructions);
                for (int i = 0; i < exampleScenario.images.Length; i++)
                {
                    if (exampleScenario.images[i].name != "" && exampleScenario.images[i].image != null)
                    {
                        SaveImage(exampleScenario.images[i].image, Path.Combine(path, exampleScenario.images[i].name));
                    }
                }
            }
            catch (Exception e)
            {
                DebugText.LogError("Could not save example scenario (" + e.GetType().ToString() + ")");
            }
        }
        RefreshFolder();
        DebugText.Log("Created Example Scenario");
    }
    public void SwitchScene(string name)
    {
        if (name == null)
        {
            SwitchScene((Scene)null);
            return;
        }
        if (name == "exit")
        {
            UnloadScenario();
            return;
        }

        Scene newscene = Array.Find <Scene>(scenario.scenes, (scene) => scene.name == name);

        if (newscene != null)
        {
            SwitchScene(newscene);
        }
        else
        {
            DebugText.LogError("Scene not defined: " + name);
            UnloadScenario();
            return;
        }
    }
Example #4
0
    public static Texture2D LoadImage(string path)
    {
        Texture2D tex = new Texture2D(0, 0);

        try
        {
            byte[] data = File.ReadAllBytes(path);
            tex.LoadImage(data, true);
        }
        catch (Exception e)
        {
            DebugText.LogError("Could not load image '" + path + "' (" + e.GetType().ToString() + ")");
        }
        return(tex);
    }
Example #5
0
 void Start()
 {
     dataFolder = GetScenarioFolder();
     if (dataFolder == "")
     {
         DebugText.LogError("Could not read/create the folder for scenarios");
     }
     else
     {
         foreach (Text t in insertFolderPathFormatted)
         {
             t.text = string.Format(t.text, dataFolder);
         }
         RefreshFolder();
     }
 }
Example #6
0
 public void RefreshFolder()
 {
     if (dataFolder == null || dataFolder == "")
     {
         Debug.LogError("Folder path for scenarios not found");
         return;
     }
     for (int i = scenarioButtonHolder.childCount - 1; i >= 0; i--)
     {
         Destroy(scenarioButtonHolder.GetChild(i).gameObject);
     }
     try
     {
         Directory.CreateDirectory(dataFolder);
         Directory.GetLastAccessTime(dataFolder);
         Directory.GetDirectories(dataFolder);
     }
     catch (UnauthorizedAccessException)
     {
         PermissionRequester.RequestPermisssion((req) =>
         {
             if (req)
             {
                 RefreshFolder();
             }
             else
             {
                 DebugText.LogError("Cannot access scenario folder without permission");
             }
         });
         return;
     }
     catch (Exception e)
     {
         DebugText.LogException("Cannot access scenario folder", e);
         return;
     }
     string[] dirs = Directory.GetDirectories(dataFolder);
     foreach (string dir in dirs)
     {
         GameObject button = GameObject.Instantiate <GameObject>(scenarioButton, scenarioButtonHolder, false);
         button.GetComponent <Button>().onClick.AddListener(() => {
             scenarioManager.LoadScenario(Path.Combine(dataFolder, dir));
         });
         button.GetComponentInChildren <Text>().text = Path.GetFileName(dir);
     }
 }
Example #7
0
 public static void SaveImage(Texture2D image, string path)
 {
     try
     {
         System.IO.File.WriteAllBytes(path, image.EncodeToPNG());
     }
     catch (Exception e)
     {
         if (e is SecurityException || e is UnauthorizedAccessException)
         {
             DebugText.LogError("App not allowed to save image to " + path);
         }
         else
         {
             DebugText.LogError("Could not save image to " + path + " (" + e.GetType().ToString() + ")");
         }
     }
 }
 public void Connect(string device)
 {
     if (bluetooth == null)
     {
         GetDevices();
     }
     if (bluetooth.Call <bool>("ConnectClient", device))
     {
         appState.EnterObserver();
         state = State.observer;
     }
     else
     {
         DebugText.LogError("Could not connect to " + device);
         Disconnect();
         DebugText.LogError("Could not connect to " + device);
     }
 }
 public void LoadScenario(string folder, bool observer = false)
 {
     scenarioFolder = folder;
     scenario       = null;
     this.observe   = observer;
     try {
         var  files  = Directory.GetFiles(folder);
         bool nojson = true;
         foreach (string f in files)
         {
             if (Path.GetExtension(f) == ".json")
             {
                 if (scenario == null)
                 {
                     scenario = Scenario.LoadJson(File.ReadAllText(Path.Combine(folder, f)));
                     nojson   = false;
                 }
                 else
                 {
                     scenario.AddJson(File.ReadAllText(Path.Combine(folder, f)));
                 }
             }
         }
         if (scenario == null)
         {
             if (nojson)
             {
                 DebugText.LogError("No scenario in folder: " + folder);
             }
             else
             {
                 DebugText.LogError("Could not load scenario from folder: " + folder);
             }
             UnloadScenario();
         }
     }
     catch (Exception e)
     {
         DebugText.LogError("Could not load scenario from folder: " + folder);
         Debug.LogException(e);
         UnloadScenario();
     }
     if (scenario == null)
     {
         UnloadScenario();
     }
     else if (scenario.scenes.Length == 0)
     {
         DebugText.LogError("No scenes found in folder: " + folder);
         UnloadScenario();
     }
     else
     {
         scenario.InitScenes();
         startScene = 0;
         if (!observer)
         {
             logger.StartLogging(scenarioFolder);
             stateManager.EnterScenario();
         }
         else
         {
             gameObject.SetActive(true);
         }
         SwitchScene((Scene)null);
     }
 }
Example #10
0
 void OnError(VideoPlayer cp, string e)
 {
     DebugText.LogError("Error playing video: " + e);
 }