Answers for "save runtime list values unity"

0

save runtime list values unity

void Start ()
 {
     minValue = 6;
     maxValue = 19;
 }

 void Update ()
 {
 //    Debug.Log (UnityEngine.Random.Range (minValue, maxValue));

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
     {
         Serialize();

     }

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
     {
         Deserialize();
         
     }
 }

 public App ( string newName, int newValue)
 {
     name = newName;
     value = newValue;
 }
 
 
 public enum AttributeName
 {
     Strength,
     Agility,
     Constitution
 }


 void Serialize() 
 {
     // Create a hashtable of values that will eventually be serialized.
     Hashtable addresses = new Hashtable();

     /*
     addresses.Add("Eddie", "123 $$anonymous$$ain Street, Redmond, WA 98052");
     addresses.Add("Freddy", "987 Pine Road, Phila., PA 19116");
     addresses.Add("$$anonymous$$ary", "PO Box 112233, Palo Alto, CA 94301");
     */

     for (int cnt = 0; cnt < System.Enum.GetValues(typeof(AttributeName)).Length ; cnt ++)
     {
         Debug.Log ((AttributeName)cnt);
         string newName = ((AttributeName)cnt).ToString();
         diceRoll = UnityEngine.Random.Range (minValue, maxValue); 

         Debug.Log (diceRoll);
         addresses.Add (newName, diceRoll);            
     }
     // To serialize the hashtable and its key/value pairs,   
     // you must first open a stream for writing.  
     // In this case, use a file stream.
     FileStream fs = new FileStream("DataFile.dat", File$$anonymous$$ode.Create);
     
     // Construct a BinaryFormatter and use it to serialize the data to the stream.
     BinaryFormatter formatter = new BinaryFormatter();
     try 
     {
         formatter.Serialize(fs, addresses);
     }
     catch (SerializationException e) 
     {
         Console.WriteLine("Failed to serialize. Reason: " + e.$$anonymous$$essage);
         throw;
     }
     finally 
     {
         fs.Close();
         Debug.Log ("Done Ser");
     }

     foreach (DictionaryEntry de in addresses) 
     {
     
         print (de.$$anonymous$$ey + "/" + de.Value);
     }
 }
 
 
 static void Deserialize() 
 {


     // Declare the hashtable reference.
     Hashtable addresses  = null;
     
     // Open the file containing the data that you want to deserialize.
     FileStream fs = new FileStream("DataFile.dat", File$$anonymous$$ode.Open);
     try 
     {
         BinaryFormatter formatter = new BinaryFormatter();
         
         // Deserialize the hashtable from the file and  
         // assign the reference to the local variable.
         addresses = (Hashtable) formatter.Deserialize(fs);
     }
     catch (SerializationException e) 
     {
         Console.WriteLine("Failed to deserialize. Reason: " + e.$$anonymous$$essage);
         throw;
     }
     finally 
     {
         fs.Close();


     }
     

     {
         Console.WriteLine("{0} lives at {1}.", de.$$anonymous$$ey, de.Value);
         print (de.$$anonymous$$ey + "/" + de.Value);
     }
 }
Posted by: Guest on March-20-2021

Browse Popular Code Answers by Language