Daniel Lip
Daniel Lip

Reputation: 11319

How can I write data to text file one once is the same data ? And how to read back and assign?

if (GUILayout.Button("Copy settings"))
        {
            var selection = Selection.gameObjects.ToList();

            for (int i = selection.Count - 1; i >= 0; --i)
            {
                var selected = selection[i];

                WriteDataToFile("Transform " + i.ToString() + "  Name ==> " + selected.name);
                WriteDataToFile("************************" +
                    "********************");
                if (selected.transform.parent != null)
                    WriteDataToFile(selected.transform.parent.ToString());
                WriteDataToFile("local position " + selected.transform.localPosition.ToString());
                WriteDataToFile("local rotation " + selected.transform.localRotation.ToString());
                WriteDataToFile("local scale " + selected.transform.localScale.ToString());
                WriteDataToFile("************************" +
                    "********************");
                WriteDataToFile(" ");
            }
        }

And the WriteDataToFile:

private void WriteDataToFile(string line)
    {
        string path = "Assets/Resources/test.txt";

        StreamWriter writer = new StreamWriter(path, true);
        writer.WriteLine(line);
        writer.Close();
    }

First I want to check that if I click more then once on the button and it's the same data: name position rotation scale don't write again.

Second how can I read back the lines of the data and assign them into a object ? Also the name. So a new object will be created in the same name parent if the original was parent position rotation and scale.

This is how I'm reading now:

void ReadString()
{
    string path = "Assets/Resources/test.txt";

    StreamReader reader = new StreamReader(path); 
    Debug.Log(reader.ReadToEnd());
    reader.Close();
}

Upvotes: 0

Views: 44

Answers (1)

Daniel Loudon
Daniel Loudon

Reputation: 799

To add new data to the file that doesn't already exist:

private void WriteDataToFile(string line)
{
    string path = "Assets/Resources/test.txt";
    string[] text = new string[];
    if(File.Exists(path))
    {
       text = File.ReadAllLines(path);
       if(!text.Contains(line))
          File.AppendAllText(path, Line);
    }

}

If you are not limited to using a text file for storing and retrieving data then I recommend finding a way to write all this data:

WriteDataToFile("Transform " + i.ToString() + "  Name ==> " + selected.name);
            WriteDataToFile("************************" +
                "********************");
            if (selected.transform.parent != null)
                WriteDataToFile(selected.transform.parent.ToString());
            WriteDataToFile("local position " + selected.transform.localPosition.ToString());
            WriteDataToFile("local rotation " + selected.transform.localRotation.ToString());
            WriteDataToFile("local scale " + selected.transform.localScale.ToString());
            WriteDataToFile("************************" +
                "********************");
            WriteDataToFile(" ");

in less writes because opening and closing the file could be expensive. Maybe something like this:

var selected = selection[i].transform;
string toWrite = $"{parent}:{localPosition}:{localRotation}:{localScale}";
WriteDataToFile(toWrite);

This would mean retrieval would be simply - (not sure the type)

private gameObject GetObjectFromFile(Path, Id)
{
   string[] text = new string[];
   if(File.Exists(path))
   {
   text = File.ReadAllLines(path);
   foreach(string s in text)
   {
      if(s.Split(':')[0] == Id.ToString())
      {
         text = s.Split(':');
         break;
      }
   }
   var Id = Convert.ToInt32(text[0]);
   var localPosition = Convert.ToInt32(text[1]);
   var localRotation = Convert.ToInt32(text[2]);
   var localScale = Convert.ToInt32(text[3]);
   return new gameObject(Id, localPosition, localRotation, localScale);
}

Upvotes: 1

Related Questions