我已完成以下操作以保存文件中瓶子的坐标。
public class bo:MonoBehaviour {
public GameObject[] bottle;
public void save()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath+"/Info.dat");
Playerdata data = new Playerdata ();
data.v[0] = bottle [0].transform.position.ToString();
bf.Serialize (file,data);
file.Close ();
}
public void load()
{
if (File.Exists (Application.persistentDataPath + "/Info.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/Initial.dat", FileMode.Open);
Playerdata data = (Playerdata)bf.Deserialize (file);
file.Close ();
Debug.Log("Value "+Vector3FromString(data.x[0]));//line giving runtime error
}
}
public Vector3 Vector3FromString(String s)
{
string[] parts = s.Split(new string[] { "," }, StringSplitOptions.None);
return new Vector3(
float.Parse(parts[0]),
float.Parse(parts[1]),
float.Parse(parts[2]));
}
[Serializable]
class Playerdata
{
public string v[] = new string[5];
}
这行在调用方法加载时给出了运行时错误。
Debug.Log("Value "+Vector3FromString(data.x[0]));
显示的错误是: - FormatException:Unknown char:
答案 0 :(得分:2)
我的矢量序列化功能:
public static string Vector3ToString(Vector3 v){ // change 0.00 to 0.0000 or any other precision you desire, i am saving space by using only 2 digits
return string.Format("{0:0.00},{1:0.00},{2:0.00}", v.x, v.y, v.z);
}
public static Vector3 Vector3FromString(String s){
string[] parts = s.Split(new string[] { "," }, StringSplitOptions.None);
return new Vector3(
float.Parse(parts[0]),
float.Parse(parts[1]),
float.Parse(parts[2]));
}
答案 1 :(得分:0)
Vector3 vector = new Vector3( 1,2,3 );
string s = vector.ToString();
string[] subs = s.Split( "(" )[1].Split( ")" )[0].Split ( "," );
Vector3 vector2 = new Vector3(
System.Convert.ToSingle( subs[0] ),
System.Convert.ToSingle( subs[1] ),
System.Convert.ToSingle( subs[2] )
);