- Home /
PlayerPrefs and own Class
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?
Answer by Spinnernicholas · Dec 15, 2013 at 07:45 PM
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("$$anonymous$$4a1",$$anonymous$$4a1); is it possible?
You could do it with a class extension but you can still only store strings, ints, and floats.
Why are you storing it in PlayerPrefs, are you letting people create their own weapons?
You can edit the weapon and the clothes of your soldiers and i want to storage the textures and the item count together.
Are you allowing them to use their own textures or just select textures that are already in your game?