Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

How to verify texture imported as normal map?

Discussion in 'Immediate Mode GUI (IMGUI)' started by DougRichardson, Oct 24, 2017.

  1. DougRichardson

    DougRichardson

    Joined:
    Oct 7, 2017
    Posts:
    74
    I'm writing a custom Editor for a ScriptableObject, and one of the fields I have is for a normal map. I'd like to check that the texture was imported with the Texture Type set to Normal Map, just like the Material inspector does for the normal map field in the Standard shader.

    However, the field I have gets a Texture2D, and it is not clear to me how to get the texture type the importer used from the Texture2D.

    How can I verify the texture dropped in my EditorGUILayout.ObjectField was imported as a normal map?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,650
  3. DougRichardson

    DougRichardson

    Joined:
    Oct 7, 2017
    Posts:
    74
    Thanks Peter. AssetImporter.GetAtPath plus AssetDatabase.GetAssetPath got me from Texture2D to TextureImporter.
     
  4. NathanJSmith

    NathanJSmith

    Joined:
    May 11, 2018
    Posts:
    57
    Hi,
    Is there anyway to restrict the field to normal map only? For example:
    Code (CSharp):
    1.     [CreateAssetMenu(fileName = "MyData", menuName = "Custom/MyData", order = 1)]
    2.     public class MyData : ScriptableObject
    3.     {
    4.         [SerializeField]
    5.         private Texture2D[] m_arrBumpMaps; //How to restrict this field to Normal map only (in case I forgot to import it as Normal map)
     
  5. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    117
    One way you might restrict a Texture2D field in the inspector to only being normal maps is by using the OnValidate() method of MonoBehaviour or ScriptableObject. In that method you could get the TextureImporter and check if its textureType is set to normal map. If not, you could set it to null or log an error or log a warning, whatever you want.