Search Unity

Save mesh as prefab with Mesh Renderer material

Discussion in 'Prefabs' started by valter-home, Jan 26, 2019.

  1. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    118
    Hi everyone,
    can I save a mesh as a prefab keeping the material information?
    With this code I save the mesh as mesh.asset and as mesh.prefab but the material is lost.
    Code (CSharp):
    1. GameObject newMesh = GameObject.Find("mymesh");
    2. Mesh msh = newMesh.GetComponent<MeshFilter>().sharedMesh;
    3. AssetDatabase.CreateAsset(msh, assetpath);
    4. PrefabUtility.SaveAsPrefabAsset(newMesh, prefabpath);
    5. AssetDatabase.SaveAssets();
    6. AssetDatabase.Refresh();
    Is there any way to keep this information?
    Would it be possible to add the material to the prefab without it being present in the Hierarchy (directly on the prefab on the disk)? I looked at the Unity documentation and it would seem possible to add components but I did not find anything about the material.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    RakNet likes this.
  3. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    118
    Thanks for your answer.
    I try your suggestion.
     
    Last edited: Feb 2, 2019
  4. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    118
    I'm afraid I'm only halfway, I find these steps a bit difficult.
    I followed your instructions but I only managed to add the material as "sub-object" to the prefab.
    Materials in Mesh Renderer is always empty, you need to drag it into the field.

    Code (CSharp):
    1. GameObject newMesh = GameObject.Find("mymesh");
    2. Mesh msh = newMesh.GetComponent<MeshFilter>().sharedMesh;
    3. AssetDatabase.CreateAsset(msh, assetpath);
    4. PrefabUtility.SaveAsPrefabAsset(newMesh, prefabpath);
    5. Material material = new Material(Shader.Find("Standard"));
    6. AssetDatabase.AddObjectToAsset(material, prefabpath);
    Also I read that it is necessary to re-import the prefab for the changes to be visible without having to save the project.
    Unlike the example, however, I do not create the asset but I use an object in the Hierarchy.

    So I tried to reload it but it's not the right way.

    Code (CSharp):
    1. Object newObj = (Object) AssetDatabase.LoadAssetAtPath(prefabpath, typeof(Object));
    2. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newObj));
    Could someone tell me the steps to save a mesh as a prefab with all its components or add them later?
    I'm looking for two days in the forums, so it's impossible?
    Thanks
     
  5. nawash

    nawash

    Joined:
    Jan 29, 2010
    Posts:
    166
    Hi all
    The solution can be found in the sample located at
    https://docs.unity3d.com/ScriptReference/PrefabUtility.html

    The key is to use
    Object prefab = PrefabUtility.CreatePrefab(localPath, obj);
    PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);

     
    Last edited: May 7, 2019