pengzhuang.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using LitJson;
  4. using UnityEngine;
  5. public class pengzhuang : MonoBehaviour
  6. {
  7. public List<GameObject> golist;
  8. public int index;
  9. private void Start()
  10. {
  11. StartCoroutine(sendInfo());
  12. }
  13. void OnTriggerEnter(Collider collider)
  14. {
  15. //进入触发器执行的代码
  16. }
  17. void OnCollisionEnter(Collision collision)
  18. {
  19. Debug.Log("OnCollisionEnter" + index);
  20. server.SendMessage(getData("Enter", collision.gameObject));
  21. }
  22. void OnCollisionExit(Collision collision)
  23. {
  24. Debug.Log("OnCollisionExit" + index);
  25. server.SendMessage(getData("Exit", collision.gameObject));
  26. }
  27. private IEnumerator sendInfo()
  28. {
  29. while(true)
  30. {
  31. for (int i = 0; i < golist.Count; i++)
  32. {
  33. server.SendMessage(getData("None", golist[i]));
  34. }
  35. yield return new WaitForSeconds(0.5f);
  36. }
  37. }
  38. JsonData getData(string msg,GameObject go)
  39. {
  40. JsonData data = new JsonData();
  41. data["index"] = index;
  42. data["type"] = msg;
  43. data["distance"] = Vector3.Distance(this.transform.position,go.transform.position);
  44. data["size"] =new JsonData();
  45. data["size"]["x"] = go.GetComponent<BoxCollider>().size.x*go.transform.localScale.x;
  46. data["size"]["y"] = go.GetComponent<BoxCollider>().size.y * go.transform.localScale.y;
  47. data["size"]["z"] = go.GetComponent<BoxCollider>().size.z * go.transform.localScale.z;
  48. // Debug.Log("getData===>"+data.ToJson());
  49. return data;
  50. }
  51. }