using LitJson; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; public class CustomMeshCreator : MonoBehaviour { string filePath = "C:\\Users\\36299\\model\\"; // Input your vertex data here (example data) public Vector3[] vertexPositions; public Vector3[] vertexNormals; public Vector2[] vertexTextureCoords; public bool q; public bool w; public bool e; public bool r; public bool y; public bool u; public bool a; public bool c; public List list; public IEnumerator Download() { GameObject objp = new GameObject(); // while(true) // { Debug.Log("刷新"); if(list!=null) { for (int l = 0; l < list.Count; l++) { Destroy(list[l]); } } list = new List(); for (int k = 0; k < 72; k++) { try { int qq = q == true ? 1 : -1; int ww = w == true ? 1 : -1; int ee = e == true ? 1 : -1; int rr = r == true ? 1 : -1; int yy = y == true ? 1 : -1; int uu = u == true ? 1 : -1; int aa = a == true ? 1 : -1; int cc = c == true ? 1 : -1; List p = new List(); List n = new List(); List t = new List(); List tt = new List(); // Check if the file exists if (File.Exists(filePath + "obj" + k + ".bin")) { // Read all bytes from the binary file string binaryData = File.ReadAllText(filePath + "obj" + k + ".bin"); JsonData data = JsonMapper.ToObject(binaryData); for (int i = 2; i < data["vertexPositions"].Count; i += 3) { p.Add(new Vector3(float.Parse(data["vertexPositions"][i - 2].ToString()) * qq, -float.Parse(data["vertexPositions"][i - 1].ToString()) * ww, ee * float.Parse(data["vertexPositions"][i].ToString()))); } for (int i = 2; i < data["vertexNormals"].Count; i += 3) { n.Add(new Vector3(rr * float.Parse(data["vertexNormals"][i - 2].ToString()), -yy * float.Parse(data["vertexNormals"][i - 1].ToString()), uu * float.Parse(data["vertexNormals"][i].ToString()))); } for (int i = 1; i < data["vertexTextureCoords"].Count; i += 2) { t.Add(new Vector2(aa * (float.Parse(data["vertexTextureCoords"][i-1 ].ToString())), cc * (float.Parse(data["vertexTextureCoords"][i].ToString())))); } for (int i = 0; i < data["indices"].Count; i++) { tt.Add(int.Parse(data["indices"][i].ToString())); } } else { Debug.LogError("File not found: " + filePath); } vertexPositions = p.ToArray(); vertexNormals = n.ToArray(); vertexTextureCoords = t.ToArray(); // Create a new mesh Mesh mesh = new Mesh(); GameObject nobj = new GameObject(); nobj.transform.parent = objp.transform; nobj.name = "obj" + k; list.Add(nobj); // Assign vertex data to the mesh mesh.vertices = vertexPositions; mesh.normals = vertexNormals; mesh.uv = vertexTextureCoords; // Create triangles (assuming a simple structure without submeshes) int[] triangles = new int[vertexPositions.Length]; mesh.triangles = tt.ToArray(); // Assign the mesh to the MeshFilter component MeshFilter meshFilter = nobj.GetComponent(); if (meshFilter == null) { meshFilter = nobj.AddComponent(); } meshFilter.mesh = mesh; if (!Directory.Exists("Assets/SavedMeshes")) AssetDatabase.CreateFolder("Assets", "SavedMeshes"); // 设置保存路径(可以根据需要修改路径) string savePath = "Assets/SavedMeshes/" + nobj.name + ".asset"; AssetDatabase.CreateAsset(mesh, savePath); AssetDatabase.SaveAssets(); // Add MeshRenderer component for visualization (optional) MeshRenderer meshRenderer = nobj.GetComponent(); if (meshRenderer == null) { meshRenderer = nobj.AddComponent(); meshRenderer.material = mat; } } catch { } } CreatePrefab(objp, "objp"); yield return new WaitForSeconds(3f); // } } public MeshFilter meshFilter; // 引用你的MeshFilter组件 public Renderer renderer; // 引用你的Renderer组件 void SaveMesh(GameObject go) { // 获取MeshFilter组件 MeshFilter meshFilter = go.GetComponent(); // 检查MeshFilter是否存在 if (meshFilter != null && meshFilter.sharedMesh != null) { // 获取Mesh Mesh mesh = meshFilter.sharedMesh; // 创建一个新的Mesh实例,防止修改原始Mesh Mesh clonedMesh = Instantiate(mesh); // 设置保存路径(可以根据需要修改路径) string savePath = "Assets/SavedMeshes/"+ go.name+ ".asset"; // 保存Mesh为Asset文件 AssetDatabase.CreateAsset(clonedMesh, savePath); AssetDatabase.SaveAssets(); Debug.Log("Mesh saved at: " + savePath); } else { Debug.LogError("MeshFilter or Mesh is missing."); } } public void CreatePrefab(GameObject prefabObject, string prefabPath) { if (!Directory.Exists("Assets/Prefabs")) AssetDatabase.CreateFolder("Assets", "Prefabs"); string localPath = "Assets/Prefabs/" + gameObject.name + ".prefab"; // Make sure the file name is unique, in case an existing Prefab has the same name. localPath = AssetDatabase.GenerateUniqueAssetPath(localPath); // 保存预制体 PrefabUtility.SaveAsPrefabAsset(prefabObject, localPath, out bool success); // 销毁临时游戏对象 DestroyImmediate(prefabObject); if (success) { Debug.Log("Prefab created at: " + prefabPath); } else { Debug.LogError("Failed to create prefab."); } } public Material mat; void Start() { StartCoroutine(Download()); } void ProcessBinaryData(byte[] binaryData) { // Process the binary data here // You can interpret the bytes based on the structure of your data // For example, if it represents integers or floats, you need to interpret them accordingly // Example: int value = BitConverter.ToInt32(binaryData, startIndex); } }