Search Unity

Question Can we save a Mesh with Material so that it uses its material when dropped in the scene?

Discussion in 'Asset Database' started by CodeSmile, Aug 1, 2022.

  1. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    I've managed to save a generated mesh and added its material to it with AssetDatabase.AddObjectToAsset():
    upload_2022-8-1_17-2-7.png

    When I drop the mesh in the scene, the material is not automatically assigned to the MeshRenderer. It's kind of expected and I know I should be creating a Prefab for that.

    Still ... maybe there's some way to get the Mesh to automatically use the Material that is embedded in it?

    By that I mean:
    1. without creating a prefab (separate asset file) - I know this works, already did it
    2. without writing an editor script that watches for "objects added to scene" - I assume this could be possible, but feels intrusive and out of context (generating mesh assets)
    3. without exporting as FBX/OBJ or similar - I assume this should work provided the mesh is added to the scene with material assigned, right?
    If you have any pros, cons, or notes regarding solutions 1-3 I'd like to hear about them.

    Thank you!