Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by afot2020 · Aug 30, 2020 at 01:59 PM · loadsave datadata storage

Adding only once a new Value to a saved int List

In my game you can choose from different Avatars (Scriptable Objects). However, most of the Avatars are locked and need to be unlocked in the game. I save the unlocked Avatars in a List of my Data Manager. My Problem: How can I manually add new Avatars IDs (int values) to the List so the next time when I start the Game, a new unlocked Avatar is available? Right now I have a method called "UnlockAvatar" in the DataManager script which perfectly saves the ID of the new unlocked Avatar into the List of unlocked Avatars. This method is called in the Avatar Selection Menu when you purchase a new Avatar. I need it for the following scenario: in the next game update I will add a new Avatar which will have the new ID "100" BUT which is unlocked by default. I know that the easiest way would be to simply call the following method in the GameManager Start() Method:

     DataManager.UnlockAvatar(100);

But that way this method will be called every time the player starts the game, there must be a simpler solution, right?

This is my User Data Script:

 [Serializable]
 public class UserData
 {
     public List<int> unlockedAvatarIDs;
     public UserData()
         {
             unlockedAvatarIDs = new List<int>();
             unlockedAvatarIDs.Add(0); //the only unlocked Avatar when playing for the first time
         }
 }

And here comes my Data Manager Script:

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEditor;
 using UnityEngine;

 public static class DataManager
 {
     public static void UnlockAvatar(int index)
      {
             UserData userData = Load();
             foreach (var item in userData.unlockedAvatarIDs)
             {
                 if (item == index)
                 {
                     return; //Don't save if index is already saved in the list
                 }
             }
             userData.unlockedAvatarIDs.Add(index);
             Save(userData);
     }

 public static List<int> GetIDsOfUnlockedAvatars()
 {
     UserData userData = Load();
     return userData.unlockedAvatarIDs;
 }

 /// <summary>
 /// Sava user data in binary format
 /// </summary>
 /// <param name="data"></param>
 private static void Save(UserData data)
 {
     string path = GetDataFilePath();

     BinaryFormatter binaryFormatter = new BinaryFormatter();

     using (FileStream fileStream = File.Open(path, FileMode.OpenOrCreate))
     {
         binaryFormatter.Serialize(fileStream, data);
     }
 }

 /// <summary>
 /// Load user data 
 /// </summary>
 /// <returns></returns>
 public static UserData Load()
 {
     string path = GetDataFilePath();
     if (!File.Exists(path))
     {
         UserData userData = new UserData();
         Save(userData); //To create file if file doesn't exists
     }
     BinaryFormatter binaryFormatter = new BinaryFormatter();
     using (FileStream fileStream = File.Open(path, FileMode.Open))
     {
         return (UserData)binaryFormatter.Deserialize(fileStream);
         }
     }
 }


The only location where an Avatar is unlocked is the "Avatar Selection Page". Here is the script attached to the Page: using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.PlayerLoop; using UnityEngine.UI;

 public class ChooseAvatar : MonoBehaviour, IEndDragHandler, IDragHandler
 {
      [SerializeField]
     private GameObject unlockButton;

     [SerializeField]
     private List<Avatar> avatars;

     private int browsingIndex = 0; //browsingIndex gets updated based on the shown avatar on page

     private void OnEnable()
     {
         LoadAvatarLockStatus();
     }

 private void LoadAvatarLockStatus()
 {
     List<int> defaultUnlockedAvatarIDs = DataManager.GetIDsOfUnlockedAvatars();
     foreach (var item in avatars)
     {
         item.Locked = defaultUnlockedAvatarIDs.Contains(item.RocketID) ? false : true;
     }
 }

 public void PurchaseUnlock()
 {
         DataManager.UnlockAvatar(avatars[browsingIndex].RocketID); // Saving the ID of unlocked avatar
  }
 

}

I would appreciate any help!!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fail to load data from json to list of list. 1 Answer

How do I find a Prefab after I Replaced said Prefab? 1 Answer

Save and load an inventory 0 Answers

Which is the correct way to save character preferences like clothes? 1 Answer

Save and load data via Facebook SDK 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges