PhysicsUtils.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.InteropServices.ComTypes;
  6. using UnityEngine;
  7. public class PhysicsUtils : MonoBehaviour
  8. {
  9. public string exportName;
  10. public GameObject startPoint;
  11. public GameObject endPoint;
  12. /// <summary>
  13. /// 角度间隔
  14. /// </summary>
  15. public float angleInterval = 10.0f;
  16. /// <summary>
  17. /// 距离间隔
  18. /// </summary>
  19. public float distanceInterval = 0.1f;
  20. //private int[][] list;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. StartRaycast();
  25. }
  26. void StartRaycast()
  27. {
  28. float dis = Vector3.Distance(startPoint.transform.position, endPoint.transform.position);
  29. int disCount = (int)(dis / distanceInterval);
  30. int angleCount = (int)(360.0f / angleInterval);
  31. Debug.Log("开始导出:"+angleCount+"_"+ disCount);
  32. //list = new int[angleCount][];
  33. Vector3 pos = new Vector3();
  34. Vector3 direction = new Vector3();
  35. Quaternion quat = new Quaternion();
  36. pos.Set(startPoint.transform.position.x, startPoint.transform.position.y, startPoint.transform.position.z);
  37. string data = "{";
  38. //物理块的大小
  39. data += "\"angleInterval\":" + this.angleInterval.ToString();
  40. data += ",\"tileHeight\":" + this.distanceInterval.ToString();
  41. //物理数据
  42. data += ",\"data\":\r\n[";
  43. int rayState = 0;
  44. for (int zIndex = 0; zIndex < disCount; zIndex++)
  45. {
  46. //list[xIndex] = new int[disCount];
  47. if (zIndex > 0)
  48. {
  49. data += ","+"\r\n"+"[";
  50. }
  51. else
  52. {
  53. data += "\r\n"+"[";
  54. }
  55. for (int xIndex = 0; xIndex < angleCount; xIndex++)
  56. {
  57. //重置方向
  58. direction.Set(0, -1, 0);
  59. pos.z = startPoint.transform.position.z + zIndex * distanceInterval;
  60. //旋转向量
  61. direction = Quaternion.Euler(0, 0, xIndex * angleInterval) * direction;
  62. if (Physics.Raycast(pos, direction))
  63. {
  64. rayState = 0;
  65. }
  66. else
  67. {
  68. rayState = 1;
  69. }
  70. if (xIndex > 0)
  71. {
  72. data += ",";
  73. }
  74. //list[xIndex][zIndex] = rayState;
  75. data += rayState.ToString();
  76. }
  77. data += "]";
  78. }
  79. data += "]";
  80. data += "}";
  81. WriteFile("D:/cocos/StarRacing/LevelTools/"+ exportName+".json", data);
  82. Debug.Log("导出完成!");
  83. }
  84. /// <summary>
  85. /// path需要包含文件的后缀名
  86. /// </summary>
  87. /// <param name="path">路径</param>
  88. /// <param name="con">需要写入的数据</param>
  89. public void WriteFile(string path, string con = "")
  90. {
  91. string str = con;
  92. //若是直接在文件里添加数据,则采用重写方法,将append设置为true
  93. var file = new StreamWriter(path, false);
  94. //var file = new StreamWriter(path);
  95. //file.WriteLine(str);
  96. file.Write(str);
  97. file.Close();
  98. }
  99. // Update is called once per frame
  100. void Update()
  101. {
  102. }
  103. }