C# Lesson: How To Save & Load Game Data on a Local Device
Tutorial, Uncategorized, Unity
0
Save data like username and score on a local device using PlayerPrefs. You can save strings, integers and floats. A basic example would look something like this:
Save:
PlayerPrefs.SetString(“playerName”, “Oxmond”);
Load:
PlayerPrefs.GetString(“playerName”);
❤️ Subscribe to Oxmond Tutorials. Stay ahead of the game!
https://bit.ly/SubscribeOxmondTutorials
✅ Free Assets from the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k
😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE
Basic SaveAndLoadData script:
/* .-------. .--. .-------. .--. .--. .--. | |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----. | - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --| |_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____| © 2019 OXMOND / www.oxmond.com */ using UnityEngine; public class SaveAndLoadData : MonoBehaviour { public void SaveData() { PlayerPrefs.SetString("playerName", "Oxmond"); PlayerPrefs.SetInt("Score", 2000)); } public void LoadData() { string _userName = PlayerPrefs.GetString("playerName"); int _score = PlayerPrefs.GetInt("Score"); } }
SaveAndLoadData script used in the video:
/* .-------. .--. .-------. .--. .--. .--. | |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----. | - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --| |_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____| © 2019 OXMOND / www.oxmond.com */ using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class SaveAndLoadData : MonoBehaviour { public TMP_InputField userName; public TMP_InputField score; public TMP_Text output; public void SaveData() { PlayerPrefs.SetString("playerName", userName.text); PlayerPrefs.SetInt("Score", int.Parse(score.text)); } public void LoadData() { print(PlayerPrefs.GetString("playerName")); output.text = "Name: " + PlayerPrefs.GetString("playerName") + " " +"Score: " + PlayerPrefs.GetInt("Score").ToString(); } }
Tags In
2019
between
between scenes
coding pirates
counter
data
device
display
easy
file
float
gamedata
Hans Oxmond
how to
integer
Intermediate Tutorial Unity 2019
learn
lesson
load
local
mastering
name
Oxmond Unity Tutorials
playerprefs
save
Save & Load Game Data on Local Device
save data
save write and load from file
scenes
score
state
store
string
system
tip
Unity
Unity3d
variables
video games
write