using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine.Networking;
using System;
using System.Threading;
using UnityEngine.UI;
public class ObjToUnityNO : MonoBehaviour
{
public Material red;
public Text []AAA;
public static ObjToUnityNO Ins_ObjToUnityNo;
private Mesh dq_mesh;
Vector3[] _vertexArray;
ArrayList _vertexArrayList = new ArrayList();
Vector3[] _normalArray;
ArrayList _normalArrayList = new ArrayList();
Vector2[] _uvArray;
ArrayList _uvArrayList = new ArrayList();
int[] _triangleArray;
ArrayList _facesVertNormUV = new ArrayList();
private void Start()
{
Ins_ObjToUnityNo = this;
}
public void meshas()
{
//调用创建Mesh方法
Draw_mesh(Application.persistentDataPath + "/Sphere_0.obj");//安卓路径(可以)
//Draw_mesh(Application.streamingAssetsPath + "/Cube_0.obj");//PC路径
}
public void Draw_mesh(string Obj_Path)
{
//初始化mesh
GetComponent<MeshFilter>().mesh = dq_mesh = new Mesh();
dq_mesh.name = "Meshs";
//赋材质
//this.gameObject.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Diffuse"));
this.gameObject.GetComponent<MeshRenderer>().material = red;
//调用方法,获取点、面、法线
init_mesh(Obj_Path);
//点、面、法线赋值生成mesh
dq_mesh.vertices = _vertexArray;
dq_mesh.triangles = _triangleArray;
dq_mesh.normals = _normalArray;
}
void init_mesh(string Obj_Path)
{
if (File.Exists(Obj_Path))
{
//查找对应文件,加载读取
//UnityWebRequest uwr = UnityWebRequest.Get("file://" + Application.streamingAssetsPath + "/Cube_0.obj");
//UnityWebRequest uwr = UnityWebRequest.Get(Application.persistentDataPath + "/Cube_0.obj");
UnityWebRequest uwr = UnityWebRequest.Get("file://" + Obj_Path);
uwr.Send();
Thread.Sleep(2000);//时间等待
//yield return uwr.Send();或用协同程序形式
//分割读取的obj文件
if (uwr.isDone && string.IsNullOrEmpty(uwr.error))
{
string s = uwr.downloadHandler.text;
s = s.Replace(" ", " ");
s = s.Replace(" ", " ");
//调用方法,去分割好的string中找点、面、法线等,给相应的数组赋值
LoadFile(s);
}
else
{
Debug.Log("Don't DownLoad ");
}
}
}
public void LoadFile(string s)
{
string[] lines = s.Split("\n"[0]);
foreach (string item in lines)
{
//调用方法,去分割好的string中找点、面、法线等,给相应的数组赋值
ReadLine(item);
}
ArrayList tempArrayList = new ArrayList();
for (int i = 0; i < _facesVertNormUV.Count; ++i)
{
if (_facesVertNormUV[i] != null)
{
PlacesByIndex indextemp = new PlacesByIndex(i);
indextemp._places.Add(i);
for (int j = 0; j < _facesVertNormUV.Count; ++j)
{
if (_facesVertNormUV[j] != null)
{
if (i != j)
{
Vector3 iTemp = (Vector3)_facesVertNormUV[i];
Vector3 jTemp = (Vector3)_facesVertNormUV[j];
if (iTemp.x == jTemp.x && iTemp.y == jTemp.y)
{
indextemp._places.Add(j);
_facesVertNormUV[j] = null;
}
}
}
}
tempArrayList.Add(indextemp);
}
}
_vertexArray = new Vector3[tempArrayList.Count];
_uvArray = new Vector2[tempArrayList.Count];
_normalArray = new Vector3[tempArrayList.Count];
_triangleArray = new int[_facesVertNormUV.Count];
int teller = 0;
foreach (PlacesByIndex item in tempArrayList)
{
foreach (int item2 in item._places)
{
_triangleArray[item2] = teller;
}
Vector3 vTemp = (Vector3)_facesVertNormUV[item._index];
_vertexArray[teller] = (Vector3)_vertexArrayList[(int)vTemp.x - 1];
if (_uvArrayList.Count > 0)
{
Vector3 tVec = (Vector3)_uvArrayList[(int)vTemp.y - 1];
_uvArray[teller] = new Vector2(tVec.x, tVec.y);
}
if (_normalArrayList.Count > 0)
{
_normalArray[teller] = (Vector3)_normalArrayList[(int)vTemp.z - 1];
}
teller++;
}
}
//读文件内容
public void ReadLine(string s)
{
AAA[3].text = "ReadLine";
char[] charsToTrim = { ' ', '\n', '\t', '\r' };
s = s.TrimEnd(charsToTrim);
string[] words = s.Split(" "[0]);
foreach (string item in words)
item.Trim();
if (words[0] == "v")
_vertexArrayList.Add(new Vector3(System.Convert.ToSingle(words[1], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[2], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[3], CultureInfo.InvariantCulture)));
if (words[0] == "vn")
_normalArrayList.Add(new Vector3(System.Convert.ToSingle(words[1], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[2], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[3], CultureInfo.InvariantCulture)));
if (words[0] == "vt")
_uvArrayList.Add(new Vector3(System.Convert.ToSingle(words[1], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[2], CultureInfo.InvariantCulture)));
if (words[0] == "f")
{
ArrayList temp = new ArrayList();
ArrayList triangleList = new ArrayList();
for (int j = 1; j < words.Length; ++j)
{
Vector3 indexVector = new Vector3(0, 0);
string[] indices = words[j].Split("/"[0]);
indexVector.x = System.Convert.ToInt32(indices[0], CultureInfo.InvariantCulture);
if (indices.Length > 1)
{
if (indices[1] != "")
indexVector.y = System.Convert.ToInt32(indices[1], CultureInfo.InvariantCulture);
}
if (indices.Length > 2)
{
if (indices[2] != "")
indexVector.z = System.Convert.ToInt32(indices[2], CultureInfo.InvariantCulture);
}
temp.Add(indexVector);
}
for (int i = 1; i < temp.Count - 1; ++i)
{
triangleList.Add(temp[0]);
triangleList.Add(temp[i]);
triangleList.Add(temp[i + 1]);
}
foreach (Vector3 item in triangleList)
{
_facesVertNormUV.Add(item);
}
}
}
}
internal class PlacesByIndex
{
public PlacesByIndex(int index)
{
_index = index;
}
public int _index;
public ArrayList _places = new ArrayList();
}
注:此脚本根据obj格式大神代码,根据自己需求,进行读取obj文件,添加数组,构建mesh,进行实例化,可在安卓和PC端实现功能,其他平台未尝试,obj文件不要太大,否则会造成unity卡死情况。如有雷同或更好的方案,可在评论区留言讨论.........