imagination XD

realizing dream

[Unity] Save Game with ArrayPrefs2

Leave a comment

I already use this script since Catapult. This is the best saving script I ever use, but this is not really easy to use. I also developing my own format but it’s not really better than this. My own format will going to be use in Personified Fear or above game. Works with indie don’t worry.

You can see the script here.

It can save any types, but the most i liked is the Array function. It’s easier and much more safe.

For example, if you want to save sound volumes setting information (Javascript for example)

  1. In your Menu GUI Script, put var volumes = new float[5];
  2. Also set the awake / default value of volumes if Player haven’t set the volumes. like if(!PlayerPrefs.HasKey(“KeyName”)){ awakeVolumes[0] = Camera.main.audio.volume; awakeVolumes[1] = AudioListener.volume; volumes = awakeVolumes; PlayerPrefsX.SetFloatArray(“KeyName”, volumes);}
  3. Then set on start / awake function, if(PlayerPrefs.HasKey(“KeyName”)){ volumes = PlayerPrefsX.GetFloatArray(“KeyName”, awakeVolumes); }
  4. On your Scroll bar GUI of main volume (volumes[1]) or music volume (volumes[0]), the scrollbar value must be volumes[0] for music volume scrollbar. Whatever you set it, when player press Save, just put PlayerPrefsX.SetFloatArray(“KeyName”,volumes);
  5. To delete user data, just simply put PlayerPrefs.DeleteKey(“KeyName”)

Okay, this is the script example of Menu GUI

var volumes = new float[5]; // volumes has 5 array of float with 0 value...
private var awakeVolumes = new float[5]; //must be same array with volumes above

function Awake(){

awakeVolumes[0] = Camera.main.audio.volume; //music volume
awakeVolumes[1] = AudioListener.volume;

if(HasKey("Game Volumes")){
volumes = PlayerPrefsX.GetFloatArray("Game Volumes", awakeVolumes);
Camera.main.audio.volume = volumes[0];
AudioListener.volume = volumes[1];
}else{
PlayerPrefsX.SetFloatArray("Game Volumes", awakeVolumes);
volumes = awakeVolumes;
}

function OnGUI(){

// let's skip making window process
GUILayout.Label("Music Volume");
volumes[0] = GUILayout.HorizontalScrollbar(volumes[0],0.0,1.0);
GUILayout.Label(volumes[0].ToString());

GUILayout.Label("Main Volume");
volumes[1] = GUILayout.HorizontalScrollbar(volumes[1],0.0,1.0);
GUILayout.Label(volumes[1].ToString());

if(GUILayout.Button("Save")){
PlayerPrefsX.SetFloatArray("Game Volumes", volumes);
}
if(GUILayout.Button("Delete")){
PlayerPrefs.DeleteKey("Game Volumes");
}

}

}

My New 2013 saver use something like XML but i’ll make own format to keep player to change every value on XML.

Advertisements

Author: IMGVERTEX

Started making games since 13 year old. 3D CGI Weeaboo | VFX Otaku | Anime Researcher | Indie Game Dev | VR Chuunibyou

FEED ME

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s