如何将相机预设保存到文本文档并在启动时加载?

时间:2022-06-08 07:23:37

I need code that would enable me to save my camera's presets and load it at the start or maybe when a GUI button is pressed.

我需要能够保存相机预设的代码,并在开始时或者按下GUI按钮时加载它。

void savePreset()
{
    //Create a txt file or replace own text file and write into it
    using (StreamWriter writetext = new StreamWriter("presets.txt"))
    {
        writetext.Write(player.transform.position.x + ", ");
        writetext.Write(player.transform.position.y + ", ");
        writetext.Write(player.transform.position.z + ", ");
        writetext.Write(player.transform.rotation.w + ", ");
        writetext.Write(player.transform.rotation.x + ", ");
        writetext.Write(player.transform.rotation.y + ", ");
        writetext.Write(player.transform.rotation.z + ", ");
        //Boolean to show that it is still saving
        saving = true;
        writetext.Close();
    }
}

This is a rough idea for saving the code onto a text document. How do I load this info and apply it on my camera?

这是将代码保存到文本文档的粗略想法。如何加载此信息并将其应用到我的相机上?

3 个解决方案

#1


1  

The best approach might be using PlayPrefs, this way you don't need to care about the path of the setting file, and it is also much faster than saving/loading data to and from the files, as in Mac this is saved to plist, and in windows, saved in registry.

最好的方法可能是使用PlayPrefs,这样你就不需要关心设置文件的路径了,它也比在文件中保存/加载数据快得多,就像在Mac中保存到plist一样,并在Windows中,保存在注册表中。

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

Camera cam;
Transform t;
void Start () {
    cam = Camera.main;
    t = cam.transform;
}
void OnGUI()
{
    if (GUILayout.Button ("Rotate (10,0,0)")) {
        t.Rotate(new Vector3(10,0,0));
        PlayerPrefs.SetFloat("rX", t.rotation.x); // Save
        PlayerPrefs.SetFloat("rY", t.rotation.y);
        PlayerPrefs.SetFloat("rZ", t.rotation.z);
        PlayerPrefs.Save ();                                          
    }
    if (GUILayout.Button ("Reload Camera Euler angle")) {

        var x= PlayerPrefs.GetFloat("rX"); // Load
        var y= PlayerPrefs.GetFloat("rY");
        var z= PlayerPrefs.GetFloat("rZ");
        cam.transform.rotation = Quaternion.Euler(x,y,z);                       
    }
}
}

如何将相机预设保存到文本文档并在启动时加载?如何将相机预设保存到文本文档并在启动时加载?如何将相机预设保存到文本文档并在启动时加载?

#2


0  

I've figured it out :D Here's the code for the loadpreset

我已经弄明白了:D这是loadpreset的代码

void loadPreset()
 {
  using (var reader = new StreamReader (File.OpenRead ("presets.txt")))
  {
   //while not end of text file
   while (!reader.EndOfStream) 
   {
    //reading each line
    var line = reader.ReadLine ();
    //dividing into values when "," detected
    var values = line.Split (',');

    //if number of values is 16
    if(values.Length == 7)
    {
     //Save the output value based on it's value position
     if (float.TryParse (values [0], NumberStyles.Float, CultureInfo.InvariantCulture, out tempPos.x)
         && float.TryParse (values [1], NumberStyles.Float, CultureInfo.InvariantCulture, out tempPos.y)
         && float.TryParse (values [2], NumberStyles.Float, CultureInfo.InvariantCulture, out tempPos.z)
         && float.TryParse (values [3], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.w)
         && float.TryParse (values [4], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.x)
         && float.TryParse (values [5], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.y)
         && float.TryParse (values [6], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.z)
         )
     {
      //Set the player position and rotation
      player.transform.position = tempPos;
      player.transform.rotation = tempRot;
     }
     else
      continue;
    }
   }
  }
 }

Thanks to all who tried to help

感谢所有试图提供帮助的人

#3


-1  

You can use StreamReader to read your txt file.

您可以使用StreamReader读取您的txt文件。

void Load()
{
    using (StreamReader reader = new StreamReader("presets.txt"))
    {
        string[] paras = reader.ReadLine().Split(',');

        Vector3 pos = new Vector3();
        pos.x = float.Parse (paras[0]);
        pos.y = float.Parse (paras[1]);
        pos.z = float.Parse (paras[2]);

        Quaternion q = new Quaternion();
        q.w = float.Parse (paras[3]);
        q.x = float.Parse (paras[4]);
        q.y = float.Parse (paras[5]);
        q.z = float.Parse (paras[6]);

        player.transform.position = pos;
        player.transform.rotation = q;
    }
}

#1


1  

The best approach might be using PlayPrefs, this way you don't need to care about the path of the setting file, and it is also much faster than saving/loading data to and from the files, as in Mac this is saved to plist, and in windows, saved in registry.

最好的方法可能是使用PlayPrefs,这样你就不需要关心设置文件的路径了,它也比在文件中保存/加载数据快得多,就像在Mac中保存到plist一样,并在Windows中,保存在注册表中。

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

Camera cam;
Transform t;
void Start () {
    cam = Camera.main;
    t = cam.transform;
}
void OnGUI()
{
    if (GUILayout.Button ("Rotate (10,0,0)")) {
        t.Rotate(new Vector3(10,0,0));
        PlayerPrefs.SetFloat("rX", t.rotation.x); // Save
        PlayerPrefs.SetFloat("rY", t.rotation.y);
        PlayerPrefs.SetFloat("rZ", t.rotation.z);
        PlayerPrefs.Save ();                                          
    }
    if (GUILayout.Button ("Reload Camera Euler angle")) {

        var x= PlayerPrefs.GetFloat("rX"); // Load
        var y= PlayerPrefs.GetFloat("rY");
        var z= PlayerPrefs.GetFloat("rZ");
        cam.transform.rotation = Quaternion.Euler(x,y,z);                       
    }
}
}

如何将相机预设保存到文本文档并在启动时加载?如何将相机预设保存到文本文档并在启动时加载?如何将相机预设保存到文本文档并在启动时加载?

#2


0  

I've figured it out :D Here's the code for the loadpreset

我已经弄明白了:D这是loadpreset的代码

void loadPreset()
 {
  using (var reader = new StreamReader (File.OpenRead ("presets.txt")))
  {
   //while not end of text file
   while (!reader.EndOfStream) 
   {
    //reading each line
    var line = reader.ReadLine ();
    //dividing into values when "," detected
    var values = line.Split (',');

    //if number of values is 16
    if(values.Length == 7)
    {
     //Save the output value based on it's value position
     if (float.TryParse (values [0], NumberStyles.Float, CultureInfo.InvariantCulture, out tempPos.x)
         && float.TryParse (values [1], NumberStyles.Float, CultureInfo.InvariantCulture, out tempPos.y)
         && float.TryParse (values [2], NumberStyles.Float, CultureInfo.InvariantCulture, out tempPos.z)
         && float.TryParse (values [3], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.w)
         && float.TryParse (values [4], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.x)
         && float.TryParse (values [5], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.y)
         && float.TryParse (values [6], NumberStyles.Float, CultureInfo.InvariantCulture, out tempRot.z)
         )
     {
      //Set the player position and rotation
      player.transform.position = tempPos;
      player.transform.rotation = tempRot;
     }
     else
      continue;
    }
   }
  }
 }

Thanks to all who tried to help

感谢所有试图提供帮助的人

#3


-1  

You can use StreamReader to read your txt file.

您可以使用StreamReader读取您的txt文件。

void Load()
{
    using (StreamReader reader = new StreamReader("presets.txt"))
    {
        string[] paras = reader.ReadLine().Split(',');

        Vector3 pos = new Vector3();
        pos.x = float.Parse (paras[0]);
        pos.y = float.Parse (paras[1]);
        pos.z = float.Parse (paras[2]);

        Quaternion q = new Quaternion();
        q.w = float.Parse (paras[3]);
        q.x = float.Parse (paras[4]);
        q.y = float.Parse (paras[5]);
        q.z = float.Parse (paras[6]);

        player.transform.position = pos;
        player.transform.rotation = q;
    }
}