Search Unity

Mesh colliders and Prefabs

Discussion in 'Physics' started by gurayg, Mar 10, 2016.

  1. gurayg

    gurayg

    Joined:
    Nov 28, 2013
    Posts:
    269
    Hello everyone,
    I'm working on a script that utilizes AssetPostProcessor.
    I'd like to automatically create prefabs out of my imported FBX file by looking at its Game Object structure.​
    It works up to a point; I attach mesh colliders and attach references to Mesh Collider's sharedMesh in scene but if I create a prefab out of it, I lost all Mesh Collider meshes.
    I'm collecting all Mesh Filters in certain criteria.Then create Go's, attach meshcollider component for all.​
    then copy the mesh filters to mesh collider using:

    Code (CSharp):
    1. Mesh _sharedMesh = Mesh.Instantiate(_navMeshFilters[i].sharedMesh) as Mesh;
    2. MeshCollider    _collider = newGameObject.GetComponent<MeshCollider> ();
    3. _collider.sharedMesh = _sharedMesh;
    That works in scene view but not if I create a prefab out of it.
    It is clear that I'm missing a step.
    Hope someone with more experience can point me to right direction.
     
  2. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    396
    It's only guess, because your description is confusing, but I think it's because you referencing to instantiated meshes, that exist only in memory, you need to reference fbx model's meshes or save your instantiated meshes as assets, and reference them to mesh colliders.
     
  3. gurayg

    gurayg

    Joined:
    Nov 28, 2013
    Posts:
    269
    Thank you very much for your post.
    I was trying to keep my post short but it looks like, that made it confusing.

    I'm collecting all Mesh Filters in an array called "_navMeshFilters[]" thinking they're references from the fbx file's meshes.
    Here is the code responsible for preparing the GameObjects and building the prefab.

    Code (CSharp):
    1.     void    PrepareNavMeshes(Transform incoming_transform)
    2.     {
    3.         _navmeshfilters = incoming_transform.GetComponentsInChildren<MeshFilter> ();
    4.         GameObject newPrefabGO = new GameObject(m_masterTransform.name+"_"+incoming_transform.name + "_nav");
    5.  
    6.         for (int i = 0; i < _navmeshfilters.Length; i++)
    7.         {
    8.             GameObject newGameObject=new    GameObject();
    9.             newGameObject.AddComponent<MeshCollider>();
    10.             Mesh _sharedMesh = Mesh.Instantiate(_navmeshfilters[i].sharedMesh) as Mesh;
    11.             MeshCollider    _collider = newGameObject.GetComponent<MeshCollider> ();
    12.             _collider.sharedMesh = _sharedMesh;
    13.         }
    14.         //Create new prefab
    15.         string newPrefabPath = assetImporter.assetPath.Substring(0, assetImporter.assetPath.LastIndexOf('/') + 1);
    16.         newPrefabPath += newPrefabGO.name + ".prefab";
    17.  
    18.         UnityEngine.Object existingPrefab = AssetDatabase.LoadAssetAtPath(newPrefabPath, typeof(UnityEngine.Object));
    19.         UnityEngine.Object newPrefab = existingPrefab ?? PrefabUtility.CreateEmptyPrefab(newPrefabPath);
    20.  
    21.         GameObject prefab = PrefabUtility.ReplacePrefab(newPrefabGO, newPrefab, ReplacePrefabOptions.Default);
    22.     }
    I read on Unity Answers people having all sorts of problems while assigning meshes to Mesh colliders. Tried all their solutions but couldn't make it work yet.