Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using UnityEngine;
- [Serializable]
- public class ComponentProperty
- {
- // Name of the property
- public string Key;
- // Units to display after the value, such as kg, m/s, m, etc.
- public string Units;
- [SerializeField]
- private string value;
- public string GetStringValue()
- {
- return value;
- }
- public float GetFloatValue()
- {
- float outValue = float.NaN;
- float.TryParse(value, out outValue);
- return outValue;
- }
- public int GetIntValue()
- {
- int outValue = int.MinValue;
- int.TryParse(value, out outValue);
- return outValue;
- }
- public bool GetBoolValue()
- {
- bool outValue = false;
- bool.TryParse(value, out outValue);
- return outValue;
- }
- public void SetValue(string inValue)
- {
- value = inValue;
- }
- public void SetValue(float inValue)
- {
- value = inValue.ToString();
- }
- public void SetValue(int inValue)
- {
- value = inValue.ToString();
- }
- public void SetValue(bool inValue)
- {
- value = inValue.ToString();
- }
- }
- [Serializable]
- public class HardwareComponent
- {
- public string Identifier;
- public string Name;
- public string Description;
- [SerializeField]
- private List<ComponentProperty> properties;
- public List<ComponentProperty> Properties
- {
- get
- {
- if (properties == null)
- properties = new List<ComponentProperty>();
- return properties;
- }
- }
- }
- [Serializable]
- public class JSONTest : MonoBehaviour
- {
- public List<HardwareComponent> Components;
- public List<string> strings;
- // Use this for initialization
- void Start ()
- {
- Components = new List<HardwareComponent>();
- strings = new List<string>();
- for (int i = 0; i < 10; i++)
- {
- HardwareComponent comp = new HardwareComponent();
- comp.Identifier = string.Format("Component_{0}", i);
- comp.Name = string.Format("Component {0}", i);
- comp.Description = "Dummy Description";
- ComponentProperty newProp = new ComponentProperty();
- newProp.Key = "String Property";
- newProp.Units = "kg";
- newProp.SetValue("A String");
- comp.Properties.Add(newProp);
- ComponentProperty newProp2 = new ComponentProperty();
- newProp2.Key = "Float Property";
- newProp2.Units = "m";
- newProp2.SetValue(1.14f);
- comp.Properties.Add(newProp2);
- strings.Add(string.Format("Component_{0}", i));
- // This works
- print(JsonUtility.ToJson(comp));
- }
- // This doesn't
- print(JsonUtility.ToJson(Components));
- // Neighter does this
- print(JsonUtility.ToJson(strings));
- }
- // Update is called once per frame
- void Update ()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement