using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;
public class PhysicsUtils : MonoBehaviour
{
public string exportName;
public GameObject startPoint;
public GameObject endPoint;
///
/// 角度间隔
///
public float angleInterval = 10.0f;
///
/// 距离间隔
///
public float distanceInterval = 0.1f;
//private int[][] list;
// Start is called before the first frame update
void Start()
{
StartRaycast();
}
void StartRaycast()
{
float dis = Vector3.Distance(startPoint.transform.position, endPoint.transform.position);
int disCount = (int)(dis / distanceInterval);
int angleCount = (int)(360.0f / angleInterval);
Debug.Log("开始导出:"+angleCount+"_"+ disCount);
//list = new int[angleCount][];
Vector3 pos = new Vector3();
Vector3 direction = new Vector3();
Quaternion quat = new Quaternion();
pos.Set(startPoint.transform.position.x, startPoint.transform.position.y, startPoint.transform.position.z);
string data = "{";
//物理块的大小
data += "\"angleInterval\":" + this.angleInterval.ToString();
data += ",\"tileHeight\":" + this.distanceInterval.ToString();
//物理数据
data += ",\"data\":\r\n[";
int rayState = 0;
for (int zIndex = 0; zIndex < disCount; zIndex++)
{
//list[xIndex] = new int[disCount];
if (zIndex > 0)
{
data += ","+"\r\n"+"[";
}
else
{
data += "\r\n"+"[";
}
for (int xIndex = 0; xIndex < angleCount; xIndex++)
{
//重置方向
direction.Set(0, -1, 0);
pos.z = startPoint.transform.position.z + zIndex * distanceInterval;
//旋转向量
direction = Quaternion.Euler(0, 0, xIndex * angleInterval) * direction;
if (Physics.Raycast(pos, direction))
{
rayState = 0;
}
else
{
rayState = 1;
}
if (xIndex > 0)
{
data += ",";
}
//list[xIndex][zIndex] = rayState;
data += rayState.ToString();
}
data += "]";
}
data += "]";
data += "}";
WriteFile("D:/cocos/StarRacing/StarRacingClient/assets/res/mainPackage/configs/" + exportName+".json", data);
Debug.Log("导出完成!");
}
///
/// path需要包含文件的后缀名
///
/// 路径
/// 需要写入的数据
public void WriteFile(string path, string con = "")
{
string str = con;
//若是直接在文件里添加数据,则采用重写方法,将append设置为true
var file = new StreamWriter(path, false);
//var file = new StreamWriter(path);
//file.WriteLine(str);
file.Write(str);
file.Close();
}
// Update is called once per frame
void Update()
{
}
}