Ok, say I want to buy a lot of item and save it into the PlayerPref, how should i do it???
var totalitem: int;
//so first i try to get the totalitem from the player pref "totalitem" at first it will return 0 as there are nothing inside
totalitem = PlayerPref.GetInt("totalitem");
//then, say i click buy item 1, so i save the item 1 into player pref "item1"
PlayerPrefs.SetInt("item"+totalitem, 1);
totalitem+=1;
//if i click multiple time buy item1, it will then save item 1 into "item0", "item1","item2"
PlayerPrefs.SetInt("totalitem",totalitem);
//EXAMPLE LET SAY I CLICK BUY ITEM "1" for 4 times, it will save it inside playerpref as below
//totalitem=4
//item1=1,item2=1,item3=1,item4=1
//SAVING IS DONE
Now I want to do the loading, I need to load the stuff back into the selling screen, since i only save number "1" in the "item1", "item2", "item3", i can load it back as below
var myitem: ArrayList;
function Start ()
{
myitem=new ArrayList();
totalitem=PlayerPrefs.GetInt("totalitem");
for( i=0 ; i < totalitem ; i++)
{
myitem.Add(PlayerPrefs.GetInt("item"+i));
}
}
GUI.Label(new Rect(50,100,100,100),"My Item: "+myitem[0]);
GUI.Label(new Rect(50,170,100,100),"My Item: "+myitem[1]);
GUI.Label(new Rect(50,240,100,100),"My Item: "+myitem[2]);
GUI.Label(new Rect(50,310,100,100),"My Item: "+myitem[3]);
No comments:
Post a Comment