#if UNITY_EDITOR using UnityEngine; using System.Collections; using UnityEditor; using System.IO; using System.Collections.Generic; using System; using Random = UnityEngine.Random; using System.Globalization; public class PostImportTool : AssetPostprocessor { class Section { public int Offset; public int NumIndices; public int Materialindex; public string MaterialName; public Material Mat; } class MeshBinding { public Mesh mesh; public MeshRenderer renderer; } void GetFloatsFromLine( string Line, ref List Floats, GameObject gameobject ) { Floats.Clear(); string[] Tokens = Line.Split(new char[]{ ' ' } ); for( int u = 1; u < Tokens.Length; u++ ) { try { CultureInfo ci = (CultureInfo)CultureInfo.InvariantCulture.Clone(); ci.NumberFormat.CurrencyDecimalSeparator = "."; float Val = float.Parse( Tokens[u], ci.NumberFormat); Floats.Add( Val ); } catch( FormatException e ) { Debug.LogError( "float.Parse failed in " + gameobject.name + " input=" + Tokens[u] + " exception=" + e.Message ); } } } void GetIntsFromLine( string Line, ref List Ints, GameObject gameobject ) { Ints.Clear(); string[] Tokens = Line.Split(new char[]{ ' ', '/' } ); for( int u = 1; u < Tokens.Length; u++ ) { try { int Val = int.Parse( Tokens[u] ); Ints.Add( Val ); } catch( FormatException e ) { Debug.LogError( "int.Parse failed in " + gameobject.name + " input=" + Tokens[u] + " exception=" + e.Message ); } } } private void OnPostprocessModel(GameObject gameobject) { UnityEditor.ModelImporter importer = this.assetImporter as UnityEditor.ModelImporter; string Path = importer.assetPath; if( !Path.Contains( ".obj" ) ) return; string OBJFile = File.ReadAllText( Path ); List Floats = new List(); List Ints = new List(); List Vertices = new List(); List Colors = new List(); List Texcoords0 = new List(); List Texcoords1 = new List(); List Normals = new List(); List Tangents = new List(); bool ReplaceNormals = true; List Indices = new List(); List
Sections = new List
(); string[] Lines = OBJFile.Split( new char[]{ '\n' } ); for(int i=0; i 0 ) { if( m.vertices.Length != Colors.Count ) { Debug.LogError( " Mesh vertex count != color count for " + gameobject.name ); } else { m.colors = Colors.ToArray(); } } if ( Texcoords1.Count > 0 ) { if( m.vertices.Length != Texcoords1.Count ) { Debug.LogError( " Mesh vertex count != Texcoords1 count for " + gameobject.name ); } else { m.uv2 = Texcoords1.ToArray(); } } if( Sections.Count > 1 ) { SetupSubmeshes( m, Sections.ToArray(), gameobject.name ); } Material[] UsedMaterials = GetSectionMaterials( Sections.ToArray() ); Binding.renderer.sharedMaterials = UsedMaterials; if( ReplaceNormals && Normals.Count > 0 ) { if( m.vertices.Length != Normals.Count ) { Debug.LogError( " Mesh vertex count != Texcoords1 count " + gameobject.name ); } else { m.normals = Normals.ToArray(); } } if( Tangents.Count > 0 ) { if ( m.tangents.Length == 0 || m.tangents.Length == Tangents.Count ) { m.tangents = Tangents.ToArray(); } else { Debug.LogError( " Mesh vertex count != Tangents count " + gameobject.name ); } } m.RecalculateBounds(); } Material[] GetSectionMaterials( Section[] sections ) { Material[] AllMaterials = UnityEngine.Resources.FindObjectsOfTypeAll(); Material[] UsedMaterials = new Material[sections.Length]; for( int i = 0; i < sections.Length; i++ ) { Section s = sections[i]; if( !s.MaterialName.Equals( "None" ) ) { for( int m = 0; m < AllMaterials.Length; m++ ) { if( AllMaterials[m].name.Equals( s.MaterialName ) ) { s.Mat = AllMaterials[m]; UsedMaterials[i] = s.Mat; break; } } } } return UsedMaterials; } void SetupSubmeshes( Mesh mesh, Section[] sections, String Name ) { int[] AllIndices = mesh.GetIndices( 0 ); mesh.subMeshCount = sections.Length; for(int i = 0; i MeshArray = new List(); MeshFilter[] MFArray = gameObject.GetComponentsInChildren(); if( MFArray != null ) { for( int i = 0; i < MFArray.Length; i++ ) { MeshFilter MF = MFArray[i]; Mesh m = MFArray[i].sharedMesh; MeshBinding NewMeshBinding = new MeshBinding(); NewMeshBinding.mesh = m; NewMeshBinding.renderer = MF.gameObject.GetComponent(); MeshArray.Add( NewMeshBinding ); } } return MeshArray.ToArray(); } [MenuItem( "RE/PrintMesh" )] static void PrintMesh() { if( Selection.gameObjects.Length == 0 ) return; GameObject go = Selection.gameObjects[0]; MeshFilter MF = go.GetComponent(); Mesh mesh = MF.sharedMesh; string Data = ""; Data += "Vertices " + mesh.vertexCount + "\n"; for( int i = 0; i < mesh.subMeshCount; i++ ) { int[] Indices = mesh.GetTriangles( i ); uint BaseVertex = mesh.GetBaseVertex( i ); Data += "Section " + i + " Indices " + Indices.Length + " BaseVertex " + BaseVertex + "\n"; } Vector3[] vertices = mesh.vertices; for( int i = 0; i < vertices.Length; i++ ) { Data += "v " + vertices[i].x + " " + vertices[i].y + " " + vertices[i].z + "\n"; } for( int i = 0; i < mesh.subMeshCount; i++ ) { int[] Indices = mesh.GetTriangles( i ); Data += "Section " + i + "\n"; for( int u = 0; u < Indices.Length; u += 3 ) { int I1 = Indices[ u ] + 1; int I2 = Indices[ u + 1] + 1; int I3 = Indices[ u + 2] + 1; //Debug.Log( "f " + I1 + " " + I2 + " " + I3 ); Data += "f " + I3 + " " + I2 + " " + I1 + "\n"; } } File.WriteAllText( "C:/UnrealToUnity/Out.txt", Data ); } } #endif