Hello,
i’ve got a class:
class SoldierAsset{
var weaponName : String ="";
var weaponTexture : Texture;
var leftInStorage : float;
}
and my question is, how can i save this class with PlayerPrefs?
Hello,
i’ve got a class:
class SoldierAsset{
var weaponName : String ="";
var weaponTexture : Texture;
var leftInStorage : float;
}
and my question is, how can i save this class with PlayerPrefs?
You can only save ints, floats, and strings to playerprefs. You don’t want to store things like textures in playerprefs. You could create methods to save and load the class to playerprefs:
public void saveToPlayerPrefs(string key)
{
PlayerPrefs.setString(key + "_weaponName", weaponName);
//instead of storing a texture, store the resource path of the record.
//Then you can manually dynamically load it.
PlayerPrefs.setString(key + "_teaxurePath", texturePath);
PlayerPrefs.setString(key + "_leftInStroage", leftInStorage);
}
public void loadFromPlayerPrefs(string key)
{
weaponName = PlayerPrefs.getString(key + "_weaponName");
texturePath = PlayerPrefs.getString(key + "_teaxurePath");
loadTexture();
leftInStorage = PlayerPrefs.getString(key + "_leftInStroage");
}
loadTexture()
{
weaponTexture = Resources.Load(texturePath, typeof(Texture));
}
i use JS. and i have got 3 var with SoldierAsset class.I want save them like PlayerPrefs.SetSoldierAsset("M4a1",M4a1); is it possible?
– Fr0stbiteYou could do it with a class extension but you can still only store strings, ints, and floats.
– SpinnernicholasWhy are you storing it in PlayerPrefs, are you letting people create their own weapons?
– SpinnernicholasYou can edit the weapon and the clothes of your soldiers and i want to storage the textures and the item count together.
– Fr0stbiteAre you allowing them to use their own textures or just select textures that are already in your game?
– Spinnernicholas