I am having an issue where I am only able to store one slider value to playerprefs. I have 10 sliders and if I attach this script, they all inherit the saved value of the first slider in the hierarchy.
public Slider Slider;
public float valueofslider ;
void Start()
{
valueofslider = PlayerPrefs.GetFloat("valueofslider");
Slider.value = valueofslider;
}
void Update()
{
valueofslider = Slider.value;
if (Input.GetKeyDown(KeyCode.S))
{
PlayerPrefs.SetFloat("valueofslider", valueofslider);
Debug.Log("save");
}
}
}
Edited code using suggestions to save to Json, but not saving currently. Not sure if the order of operations is correct.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using LitJson;
using System.Text;
using System.Web;
using System;
public class jsonbuttonserialize : MonoBehaviour
{
[SerializeField]
public class Sliders
{
public float value;
public float minValue;
public float maxValue;
public bool wholeNumbers;
public string objName;
}
[SerializeField]
public class SliderInfo
{
public List<Sliders> sliders;
public SliderInfo()
{
sliders = new List<Sliders>();
}
public SliderInfo(Slider[] slider)
{
sliders = new List<Sliders>();
for (int i = 0; i < slider.Length; i++)
AddSlider(slider[i]);
}
public void AddSlider(Slider slider)
{
Sliders tempSlider = new Sliders();
tempSlider.value = slider.value;
tempSlider.minValue = slider.minValue;
tempSlider.maxValue = slider.maxValue;
tempSlider.wholeNumbers = slider.wholeNumbers;
tempSlider.objName = slider.name;
sliders.Add(tempSlider);
}
}
public class DataSaver
{
//Save Data
public static void saveData<T>(T dataToSave, string dataFileName)
{
string tempPath = Path.Combine(Application.persistentDataPath, "data");
tempPath = Path.Combine(tempPath, dataFileName + ".txt");
//Convert To Json then to bytes
string jsonData = JsonUtility.ToJson(dataToSave, true);
byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
}
//Debug.Log(path);
try
{
File.WriteAllBytes(tempPath, jsonByte);
Debug.Log("Saved Data to: " + tempPath.Replace("/", "\\"));
}
catch (Exception e)
{
Debug.LogWarning("Failed To PlayerInfo Data to: " + tempPath.Replace("/", "\\"));
Debug.LogWarning("Error: " + e.Message);
}
}
//Load Data
public static T loadData<T>(string dataFileName)
{
string tempPath = Path.Combine(Application.persistentDataPath, "data");
tempPath = Path.Combine(tempPath, dataFileName + ".txt");
//Exit if Directory or File does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Debug.LogWarning("Directory does not exist");
return default(T);
}
if (!File.Exists(tempPath))
{
Debug.Log("File does not exist");
return default(T);
}
//Load saved Json
byte[] jsonByte = null;
try
{
jsonByte = File.ReadAllBytes(tempPath);
Debug.Log("Loaded Data from: " + tempPath.Replace("/", "\\"));
}
catch (Exception e)
{
Debug.LogWarning("Failed To Load Data from: " + tempPath.Replace("/", "\\"));
Debug.LogWarning("Error: " + e.Message);
}
//Convert to json string
string jsonData = Encoding.ASCII.GetString(jsonByte);
//Convert to Object
object resultValue = JsonUtility.FromJson<T>(jsonData);
return (T)Convert.ChangeType(resultValue, typeof(T));
}
public static bool deleteData(string dataFileName)
{
bool success = false;
//Load Data
string tempPath = Path.Combine(Application.persistentDataPath, "data");
tempPath = Path.Combine(tempPath, dataFileName + ".txt");
//Exit if Directory or File does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Debug.LogWarning("Directory does not exist");
return false;
}
if (!File.Exists(tempPath))
{
Debug.Log("File does not exist");
return false;
}
try
{
File.Delete(tempPath);
Debug.Log("Data deleted from: " + tempPath.Replace("/", "\\"));
success = true;
}
catch (Exception e)
{
Debug.LogWarning("Failed To Delete Data: " + e.Message);
}
return success;
}
}
public class buttonsave : MonoBehaviour
{
SliderInfo loadedSliders;
Slider[] slider;
void Start()
{
//Load Slider Settings
loadedSliders = DataSaver.loadData<SliderInfo>("Sliders");
//Get current sliders in the Scene
slider = FindObjectsOfType(typeof(Slider)) as Slider[];
/*Loop over loadedSliders.sliders then compare the objName with
slider.name, if they match, assign the value*/
}
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
Slider[] slider = FindObjectsOfType(typeof(Slider)) as Slider[];
SliderInfo sliderInfo = new SliderInfo(slider);
//Save Sliders
DataSaver.saveData(sliderInfo, "Sliders");
}
}
}
}
Edited with updated code, saving a file called Sliders.txt but no data is recorded in the file...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using System;
public class JSONSerialize:MonoBehaviour {
[SerializeField]
public class Sliders {
public float value;
public float minValue;
public float maxValue;
public bool wholeNumbers;
public string objName;
}
SliderInfo loadedSliders;
Slider[] slider;
void Start() {
//Load Slider Settings
loadedSliders = DataSaver.loadData<SliderInfo>("Sliders");
//Get current sliders in the Scene
slider = FindObjectsOfType(typeof(Slider)) as Slider[];
}
void Update() {
if (Input.GetKeyDown(KeyCode.S)) {
Slider[] slider = FindObjectsOfType(typeof(Slider)) as Slider[];
SliderInfo sliderInfo = new SliderInfo(slider);
//Save Sliders
DataSaver.saveData(sliderInfo,"Sliders");
Debug.Log("hello");
}
}
[SerializeField]
public class SliderInfo {
public List<Sliders> sliders;
public SliderInfo() {
sliders = new List<Sliders>();
}
public SliderInfo(Slider[] slider) {
sliders = new List<Sliders>();
for (int i = 0; i < slider.Length; i++)
AddSlider(slider[i]);
}
public void AddSlider(Slider slider) {
Sliders tempSlider = new Sliders();
tempSlider.value = slider.value;
tempSlider.minValue = slider.minValue;
tempSlider.maxValue = slider.maxValue;
tempSlider.wholeNumbers = slider.wholeNumbers;
tempSlider.objName = slider.name;
sliders.Add(tempSlider);
}
}
public class DataSaver {
//Save Data
public static void saveData<T>(T dataToSave,string dataFileName) {
string tempPath = Path.Combine(Application.persistentDataPath,"data");
tempPath = Path.Combine(tempPath,dataFileName + ".txt");
//Convert To Json then to bytes
string jsonData = JsonUtility.ToJson(dataToSave,true);
byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath))) {
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
}
//Debug.Log(path);
try {
File.WriteAllBytes(tempPath,jsonByte);
Debug.Log("Saved Data to: " + tempPath.Replace("/","\\"));
} catch (Exception e) {
Debug.LogWarning("Failed To PlayerInfo Data to: " + tempPath.Replace("/","\\"));
Debug.LogWarning("Error: " + e.Message);
}
}
//Load Data
public static T loadData<T>(string dataFileName) {
string tempPath = Path.Combine(Application.persistentDataPath,"data");
tempPath = Path.Combine(tempPath,dataFileName + ".txt");
//Exit if Directory or File does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath))) {
Debug.LogWarning("Directory does not exist");
return default(T);
}
if (!File.Exists(tempPath)) {
Debug.Log("File does not exist");
return default(T);
}
//Load saved Json
byte[] jsonByte = null;
try {
jsonByte = File.ReadAllBytes(tempPath);
Debug.Log("Loaded Data from: " + tempPath.Replace("/","\\"));
} catch (Exception e) {
Debug.LogWarning("Failed To Load Data from: " + tempPath.Replace("/","\\"));
Debug.LogWarning("Error: " + e.Message);
}
//Convert to json string
string jsonData = Encoding.ASCII.GetString(jsonByte);
//Convert to Object
object resultValue = JsonUtility.FromJson<T>(jsonData);
return (T)Convert.ChangeType(resultValue,typeof(T));
}
public static bool deleteData(string dataFileName) {
bool success = false;
//Load Data
string tempPath = Path.Combine(Application.persistentDataPath,"data");
tempPath = Path.Combine(tempPath,dataFileName + ".txt");
//Exit if Directory or File does not exist
if (!Directory.Exists(Path.GetDirectoryName(tempPath))) {
Debug.LogWarning("Directory does not exist");
return false;
}
if (!File.Exists(tempPath)) {
Debug.Log("File does not exist");
return false;
}
try {
File.Delete(tempPath);
Debug.Log("Data deleted from: " + tempPath.Replace("/","\\"));
success = true;
} catch (Exception e) {
Debug.LogWarning("Failed To Delete Data: " + e.Message);
}
return success;
}
}
}