Advertisement
Guest User

Untitled

a guest
May 24th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5.  
  6. [Serializable]
  7. public class ComponentProperty
  8. {
  9.     // Name of the property
  10.     public string Key;
  11.     // Units to display after the value, such as kg, m/s, m, etc.
  12.     public string Units;
  13.     [SerializeField]
  14.     private string value;
  15.  
  16.     public string GetStringValue()
  17.     {
  18.         return value;
  19.     }
  20.  
  21.     public float GetFloatValue()
  22.     {
  23.         float outValue = float.NaN;
  24.         float.TryParse(value, out outValue);
  25.         return outValue;
  26.     }
  27.  
  28.     public int GetIntValue()
  29.     {
  30.         int outValue = int.MinValue;
  31.         int.TryParse(value, out outValue);
  32.         return outValue;
  33.     }
  34.  
  35.     public bool GetBoolValue()
  36.     {
  37.         bool outValue = false;
  38.         bool.TryParse(value, out outValue);
  39.         return outValue;
  40.     }
  41.  
  42.     public void SetValue(string inValue)
  43.     {
  44.         value = inValue;
  45.     }
  46.  
  47.     public void SetValue(float inValue)
  48.     {
  49.         value = inValue.ToString();
  50.     }
  51.  
  52.     public void SetValue(int inValue)
  53.     {
  54.         value = inValue.ToString();
  55.     }
  56.  
  57.     public void SetValue(bool inValue)
  58.     {
  59.         value = inValue.ToString();
  60.     }
  61. }
  62. [Serializable]
  63. public class HardwareComponent
  64. {
  65.     public string Identifier;
  66.     public string Name;
  67.     public string Description;
  68.  
  69.     [SerializeField]
  70.     private List<ComponentProperty> properties;
  71.  
  72.     public List<ComponentProperty> Properties
  73.     {
  74.         get
  75.         {
  76.             if (properties == null)
  77.                 properties = new List<ComponentProperty>();
  78.             return properties;
  79.         }
  80.     }
  81. }
  82.  
  83. [Serializable]
  84. public class JSONTest : MonoBehaviour
  85. {
  86.     public List<HardwareComponent> Components;
  87.     public List<string> strings;
  88.  
  89.     // Use this for initialization
  90.     void Start ()
  91.     {
  92.         Components = new List<HardwareComponent>();
  93.         strings = new List<string>();
  94.         for (int i = 0; i < 10; i++)
  95.         {
  96.             HardwareComponent comp = new HardwareComponent();
  97.             comp.Identifier = string.Format("Component_{0}", i);
  98.             comp.Name = string.Format("Component {0}", i);
  99.             comp.Description = "Dummy Description";
  100.             ComponentProperty newProp = new ComponentProperty();
  101.             newProp.Key = "String Property";
  102.             newProp.Units = "kg";
  103.             newProp.SetValue("A String");
  104.             comp.Properties.Add(newProp);
  105.             ComponentProperty newProp2 = new ComponentProperty();
  106.             newProp2.Key = "Float Property";
  107.             newProp2.Units = "m";
  108.             newProp2.SetValue(1.14f);
  109.             comp.Properties.Add(newProp2);
  110.             strings.Add(string.Format("Component_{0}", i));
  111.             // This works
  112.             print(JsonUtility.ToJson(comp));
  113.         }
  114.         // This doesn't
  115.         print(JsonUtility.ToJson(Components));
  116.         // Neighter does this
  117.         print(JsonUtility.ToJson(strings));
  118.  
  119.     }
  120.  
  121.     // Update is called once per frame
  122.     void Update ()
  123.     {
  124.        
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement