Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question It is possible to get texture name from material in an FBX via script?

Discussion in 'Asset Importing & Exporting' started by RedVonix, Nov 1, 2022.

  1. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    421
    I'm working with an in-editor script. I want to be able to take an FBX file, and find the materials inside of it - and then get the texture name (or texture path) that was assigned to that material within its original editor (such as Maya or Cinema4D). I don't need to extract the texture, I just need the filename or file path.

    Does the FBX even contain this information, and if so, is there a way we can acquire it via script in Unity?

    Thank you!
     
  2. bastien_humeau

    bastien_humeau

    Unity Technologies

    Joined:
    Jun 14, 2017
    Posts:
    191
    Hey there,
    I don't think it's possible to access this information after the FBX import is finished.
    If you need to keep that path somewhere and fetch it later, here's what I would do:

    - Create an AssetPostprocessor script that implements OnPreprocessMaterialDescription and OnPostprocessModel.
    - From the material description callback, read the material properties you're interested in (all the textures paths in your case) and save them locally in your script for each material.
    - From the OnPostprocessModel, create a new ScriptableObject of your own type that will store all of the data previously gathered, and add this object in the import result using context.AddObjectToAsset().

    Now that you have this script, every FBX file in your project should re-import and have this ScriptableObject available as a subasset.
    You can load it using the standard AssetDatabase.LoadAssetAtPath<>() and get that data whenever you need to in the editor. If nothing references it from any scene or prefab, this data will be ignored during your build so it won't impact the final player size.