SPUPTestScriptJSON.cs 995 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. public class SPUPTestScriptJSON : MonoBehaviour
  6. {
  7. [System.Serializable]
  8. public class SPAPItem
  9. {
  10. public float Right;
  11. public float Center;
  12. public float Left;
  13. }
  14. //var
  15. public SerialPortUtility.SerialPortUtilityPro serialPort = null;
  16. public KeyCode writeKey = KeyCode.Alpha1;
  17. private SPAPItem item = null;
  18. // Use this for initialization
  19. void Start () {}
  20. // Update is called once per frame
  21. void Update ()
  22. {
  23. if (serialPort != null)
  24. {
  25. if (Input.GetKeyDown(writeKey)) {
  26. SPAPItem itobj = new SPAPItem();
  27. itobj.Center = 0.0f;
  28. itobj.Right = 0.0f;
  29. itobj.Left = 0.0f;
  30. serialPort.WriteJSON(itobj);
  31. }
  32. }
  33. }
  34. //for class
  35. public void ReadComprateJSON(object data)
  36. {
  37. item = data as SPAPItem;
  38. //Example Read Data : {"Right":1.0,"Center":0.5,"Left":0.0}
  39. Debug.Log(string.Format("C:{0}, R:{1}, L:{2}",
  40. item.Center,
  41. item.Right,
  42. item.Left));
  43. }
  44. }