Unity is a powerful game engine that supports multi-platform development for video games and interactive content. It uses C# for scripting and provides real-time rendering and a rich Asset Store. Widely used for its versatility, Unity has applications in fields beyond gaming, including architecture and education, due to its large community and extensive documentation.
Unity offers ways to store scores locally or online for sharing across devices.
1. Local Storage with PlayerPrefs
PlayerPrefs stores data on the device, ideal for simple use cases.
int currentScore = 100; // Example score
int bestScore = PlayerPrefs.GetInt("BestScore", 0);
if (currentScore > bestScore) {
PlayerPrefs.SetInt("BestScore", currentScore);
PlayerPrefs.Save();
}
// To display the best score:
int bestScore = PlayerPrefs.GetInt("BestScore", 0);
scoreText.text = "Best Score: " + bestScore.ToString();
Limitations: PlayerPrefs only stores integers, floats, and strings and is unsuitable for sensitive data.
2. Remote Storage with Server/Database
Use a backend, such as Firebase, to store scores online, allowing sharing and synchronization.
IEnumerator SaveBestScore(int score) {
WWWForm form = new WWWForm();
form.AddField("score", score);
using (UnityWebRequest www = UnityWebRequest.Post("https://your-api-endpoint.com/savescore", form)) {
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) {
Debug.Log("Error: " + www.error);
} else {
Debug.Log("Score saved successfully");
}
}
}
IEnumerator GetBestScore() {
using (UnityWebRequest www = UnityWebRequest.Get("https://your-api-endpoint.com/getscore")) {
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) {
Debug.Log("Error: " + www.error);
} else {
int bestScore = int.Parse(www.downloadHandler.text);
scoreText.text = "Best Score: " + bestScore.ToString();
}
}
}
For Android devices, Unity’s integration with the native Share Intent allows score sharing on social platforms.
#if UNITY_ANDROID
string shareMessage = "I scored " + bestScore + " points! Can you beat my high score?";
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareMessage);
intentObject.Call<AndroidJavaObject>("setType", "text/plain");
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("startActivity", intentObject);
#endif
If you want to hire a Unity Game developer, contact us. For consultation, you can take schedule from this routine.
Face2Face coffee meeting with clients
Satisfied Happy Clients all over Bangladesh
Successful live projects...
Peoples team who works dedicatedly