Saving and loading cube data
I am trying to save all of the data of cubes as floats for x,y,z positioning and x,y,z rotation, and the name of the block as a string to save it for later. I am then loading all this data into a vector3 quaternion and a huge if statement line checking what type of block it is and loading it. It doesn't save the x,y,z positioning, and x,y,z rotation right and keeps making a gigantic ball of crap. Any ideas of how I can save each of the variables individually? Btw I am trying to save those variables separately for serialization, not trying to be a noob and fail when I serialize my data into some type of file. Wouldn't want that data exploited!
Answer by TBruce · Dec 17, 2016 at 12:40 AM
there are several ways to accomplish what you want, but it is best to use a library that will do it all for you. You can use the PlayerPrefsX.cs class written by community user Eric Haines, or you can use the script shown here that has the necessary code taken from PlayerPrefsX.cs (660 lines vs 292 lines).
Here is an example class that easily saves positions and quaternions from a list of transforms that works with either versions of the extended PlayerPrefs code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SaveLoad : MonoBehaviour
{
public List<Transform> transforms = new List<Transform>();
// this is just an example of saving a position and a quaternion using the PlayerPrefsExt class
public void SaveTransformData(string positionKey, string quaternionKey)
{
if (transforms.count > 0)
{
for int i = 0; i < transforms.count; i++)
{
PlayerPrefsExt.SetVector3(positionKey + "_" + i.ToString(), transforms[i].position);
PlayerPrefsExt.SetQuaternion(quaternionKey + "_" + i.ToString(), transforms[i].rotation);
}
}
}
// this is just an example of loading a position and a quaternion using the PlayerPrefsExt class
public void LoadTransformData(string positionKey, string quaternionKey)
{
if (transforms.count > 0)
{
for int i = 0; i < transforms.count; i++)
{
transforms[i].position = PlayerPrefsExt.GetVector3(positionKey + "_" + i.ToString(), new Vector3(0, 0, 0));
transforms[i].rotation = PlayerPrefsExt.GetQuaternion(quaternionKey + "_" + i.ToString(), Quaternion.Euler(0, 0, 0));
}
}
}
}
I was using PosX, PosY, and PosZ to save data. I then instantiated the objects based on those saved values. But the real problem seems to be it is saving incorrect values. I.E: Cube is at 1,1,1 with a rotation of 0,0,0 and saves as 0,0.987,1.25 and 90,42,78, any explination of why checking a gameobjects position with (Name of object variable).transform.localPosition.x; and doing it for all axis and rotations wont work?
Well, found the issue! When saving rotations don't forget the w argument! Very important you don't forget it or everything gets strange rotation! Thank you for making me double check my code for errors!
Answer by djgaven588 · Dec 17, 2016 at 12:57 AM
I want to avoid playerprefs at all costs, my experience with them was short and I would not like to use them again. They really lagged my first concept of my game making it stop for 3 second to save a three digit integer.
Your answer
Follow this Question
Related Questions
Is it normal that saving data to file makes game freeze for a long time? 1 Answer
reading and writing files in built project,Files 0 Answers
Help offline progression, works on windows but not on Android. 0 Answers
Avoiding Gimbal Lock with Vector3 EulerAngles and Applying Torque 0 Answers
Rotating 3d person character 0 Answers