Search Unity

writing to a resource text file?

Discussion in 'Scripting' started by EnsurdFrndship, Oct 21, 2014.

  1. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    Hello,
    I know that StreamWriter() is a way to write to files, and Resource.Load() is a way to read from files, but I also noticed how the files that I write to do not change unless I reload them either in Unity or inside a document editor somewhere. If the text file is not loaded anywherer, it will not change, and if it is already loaded, I have to close the .txt file and reload it before the changes take place. How do I fix this glitch? Also, will StreamWriter work in a published .exe file or in a web browser (due to security purposes) and how does THAT work?

    What I am trying to do is have a bunch of game settings I can store in a .txt file, and when I change a setting in a GUI control, it writes to a file. The GUI controls also read from the file to get the values of each setting, but if the file doesn't change when I write to it and I go back to the "settings screen", nothing in my settings change.
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You could use UnityEngine.PlayerPrefs to save/load data easily.

    You can't save files in Resources, atleast I don't think you can.
    So instead save the settings at Application.dataPath or use UnityEngine.PlayerPrefs.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It won't work in a Web Player build.
    It won't work writing to a Resources folder in a built exe
    It will work if you write to something like Application.dataPath and load with System.IO.
    The above won't work in a Web Player either. For Web Player you're stuck with PlayerPrefs or POSTing the data somewhere and retrieving it via some kind of web service or GET.
     
    IanOnWork and Magiichan like this.
  4. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    What about an idea of developing some "conventional temp memory" for the web player so resources can be virtually 'written' to that way, and when the user closes out of the web player, he/she gets a dialog box that says, "Would you like to save your changes to a .data file?" and if he/she clicks "Yes", a save dialog box comes up where he/she can save this information to their hard drive?
     
  5. ultraviol3nt

    ultraviol3nt

    Joined:
    Jan 17, 2010
    Posts:
    155
    You can use base C# classes to write to a file, call it a .cfg. Since .cfgs are just renamed text files, you would just need to parse the text. We did the same thing when we replaced the default Unity launcher with a custom patcher. We needed to save settings from the custom launcher into the game proper, and this is how we did it. Take a look at the code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System;
    6.  
    7.  
    8. public static class GameCFG : System.Object {
    9.     static string cfgDirectory = Directory.GetCurrentDirectory() + "/LiveWorldCfg.cfg";
    10.  
    11.     public static bool GetConfigBool() {
    12.         return false;
    13.     }
    14.  
    15.     public static void Initiate() {
    16.  
    17.         //Prepare for config
    18.         List<string> cfgLines = new List<string>();
    19.         string cfgDirectory = Directory.GetCurrentDirectory() + "/config.cfg";
    20.         try {
    21.             //Check if cfg currently exists
    22.             if (File.Exists(cfgDirectory)) {
    23.                 //Get cfg lines
    24.                 foreach (string _l in File.ReadAllLines(cfgDirectory)) {
    25.                     cfgLines.Add(_l);
    26.                 }
    27.                 //Process cfg lines
    28.                 ProcessConfigLines(cfgLines.ToArray());
    29.             } else {
    30.                 //create the config file
    31.                 Debug.LogWarning("WARNING: CONFIG FILE NOT FOUND. CREATING NEW FILE.");
    32.                 string[] defaultConfigOptions = new string[2];
    33.                 defaultConfigOptions[0] = "resolution=1366x768";
    34.                 defaultConfigOptions[1] = "fullscreen=false";
    35.                 defaultConfigOptions[1] = "quality=Simple";
    36.  
    37.                 File.WriteAllLines(cfgDirectory, defaultConfigOptions);
    38.             }
    39.         } catch (IOException e) {
    40.             Debug.LogException(e);
    41.         } catch (MissingReferenceException r) {
    42.             Debug.LogException(r);
    43.         } finally {
    44.             //Parse each line into eval
    45.             foreach (string line in cfgLines) {
    46.                 ProcessConfigLine(line);
    47.             }
    48.         }
    49.     }
    50.  
    51.     static void ProcessConfigLine(string line) {
    52.     }
    53.  
    54.     static string GetConfigString() {
    55.         return "Some String";
    56.     }
    57.     static int GetConfigInt(string[] cfgLines) {
    58.         return 1;
    59.     }
    60.     static bool GetConfigBool(string[] cfgLine, string TargetCommand) {
    61.         string _val = "";
    62.  
    63.         foreach (string line in cfgLine) {
    64.             string command = line.Substring(0, line.IndexOf('='));
    65.             if (command == TargetCommand) {
    66.                 _val = line.Substring(line.IndexOf('=') + 1, line.Length - line.IndexOf('=') - 1);
    67.             }
    68.         }
    69.         return _val == "true";
    70.     }
    71.  
    72.     static void ProcessConfigLines(string[] cfgLines) {
    73.  
    74.         foreach (string line in cfgLines) {
    75.             string _command = line.Substring(0, line.IndexOf('='));
    76.             string _value = line.Substring(line.IndexOf('=') + 1,
    77.                 line.Length - line.IndexOf('=') - 1);
    78.  
    79.             //Debug.Log(_value);
    80.             switch (_command) {
    81.  
    82.                 //read resolution config
    83.                 case "resolution":
    84.                     string[] numbers = _value.Split('x');
    85.  
    86.                     int _x = Convert.ToInt32(numbers[0]);
    87.                     int _y = Convert.ToInt32(numbers[1]);
    88.                     Screen.SetResolution(_x, _y, GetConfigBool(cfgLines, "fullscreen"));
    89.                     break;
    90.  
    91.                 //ready fullscreen config
    92.                 case "fullscreen":
    93.                     if (_value == "True")
    94.                     {
    95.                         if(!Screen.fullScreen)
    96.                         Screen.fullScreen = true;
    97.                     }
    98.                     else if(_value == "False")
    99.                     {
    100.                         if(Screen.fullScreen)
    101.                         Screen.fullScreen = false;
    102.                     }
    103.                     break;
    104.  
    105.                 //read quality config
    106.                 case "quality":
    107.                     switch (_value)
    108.                     {
    109.                         case "Fastest":
    110.                             QualitySettings.SetQualityLevel(0);
    111.                             break;
    112.                         case "Fast":
    113.                             QualitySettings.SetQualityLevel(1);
    114.                             break;
    115.                         case "Simple":
    116.                             QualitySettings.SetQualityLevel(2);
    117.                             break;
    118.                         case "Good":
    119.                             QualitySettings.SetQualityLevel(3);
    120.                             break;
    121.                         case "Beautiful":
    122.                             QualitySettings.SetQualityLevel(4);
    123.                             break;
    124.                         case "Fantastic":
    125.                             QualitySettings.SetQualityLevel(5);
    126.                             break;
    127.                     }
    128.                     break;
    129.             }
    130.         }
    131.     }
    132. }
    133.  
     
  6. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    Thank you. That was what I needed. Will it work in a web player though, to where a person who plays a game from a browser can directly save information to his computer through a save dialog box?