public void MeshImported(string objPath)
        {
            // Find the import behaviour that was waiting on this mesh to be imported
            string          asset           = System.IO.Path.GetFileName(objPath);
            ImportBehaviour importComponent = ImportBehaviour.FindImportBehavior_ByWaitingMesh(asset);

            if (importComponent != null)
            {
                // The mesh has finished loading. Keep track of that status.
                if (!importComponent.ImportComplete_Meshes.Contains(asset))
                {
                    importComponent.ImportComplete_Meshes.Add(asset);
                }

                // Are we done importing all meshes? If so then start importing prefabs.
                if (importComponent.IsMeshImportingCompleted())
                {
                    ImportAllPrefabs(importComponent, objPath);
                }
            }
        }
Пример #2
0
        // We need to call this while the renderers on the model is having its material assigned to it
        // This is invoked for every submesh in the .obj wavefront mesh
        public UnityEngine.Material FixMaterialForMeshRenderer(string objName, Renderer renderer)
        {
            // Find the import behaviour that is waiting for the mesh to be imported.
            string          assetName      = objName + ".obj";
            ImportBehaviour importBehavior = ImportBehaviour.FindImportBehavior_ByWaitingMesh(assetName);

            // The mesh to match
            string meshName = renderer.name;

            // Find an assignment that matches the mesh renderer
            var      assignMaterials = importBehavior.XmlDocument.Root.Elements("AssignMaterial");
            XElement match           = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName);

            if (match == null)
            {
                // The names of our meshes in the AssignMaterials elements may be wrong
                // This happened before when Unity replaced whitespace with underscore in our named meshes
                // That case is handled now, but there may be others
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name);
                string choices = String.Join("\n  ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray());
                builder.AppendFormat("Choices are:\n  {0}", choices);

                Debug.LogError(builder.ToString());
                return(null);
            }

            string materialName = match.Attribute("material").Value + ".mat";
            string materialPath = GetExistingMaterialAssetPath(materialName);

            // Assign the material
            UnityEngine.Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(UnityEngine.Material)) as UnityEngine.Material;
            if (material == null)
            {
                Debug.LogError(String.Format("Could not find material: {0}", materialName));
            }

            return(material);
        }