Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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,324
  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:
    77
    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.