Author Topic: Substance plugin defect - causing error in the Material Inspector - pretty bad  (Read 629 times)

Hi all,

I've been trying to workaround this very annoying and frustrating error and I'm able to narrow it down to the Substance plugin for Unity.

Simply put, I have a simple script on a game object that only has a mesh renderer with no material set.  I have a button on the script that when clicked (be it in play mode or in editor) does the following:

Code: [Select]
public Material TestMaterial;

public void onClick()
{
    var material = new Material(TestMaterial);
    var meshRenderer = GetComponent<MeshRenderer>();
    meshRenderer.material = material;
}

When you inspect the game object and click the button which runs the simple script, you will see the material/shader in the inspector.



When you try to expand it, nothing shows up and you get the following error showing up in the console over and over (while the material/shader is expanded).



I did this on a brand new project without the Substance plugin and it works fine.  As soon as I import the Substance plugin, it happens.

Other notes:
* I'm using the latest Unity 2019.3.15 version.
* Running on MacOS
* Also working with URP.
* This only happens when you instantiate a new material.  If you just reference an existing material, then it's fine.  For example:
Code: [Select]
   meshRenderer.material = TestMaterial;



It's very frustrating.  Tracing through the Unity code, it appears that the LightMapSettings object is not initialized in the MaterialEditor - in fact, the real issue is that the onEnable of the MaterialEditor is never run when you expand the Material/Shader in the inspector which is causing the LightMapSettings to not be initialized thereby causing the NullReferenceException.  My guess is that the Substance plugin is overriding this behaviour and not running base.



Last Edit: June 06, 2020, 09:07:21 pm

Found the issue:

Code: [Select]
namespace Substance.Editor
{
  [CustomEditor(typeof (Material))]
  [CanEditMultipleObjects]
  public class InspectorMaterial : MaterialEditor
  {
    private string myShaderName = "";
    private bool mModified;
    private bool mWorkflowModified;
    private GUITool guiTool;

    public override void OnEnable()
    {
      string assetPath = AssetDatabase.GetAssetPath(this.target);
      if (assetPath == "")
        return;
      base.OnEnable();
      if (!assetPath.Contains(Globals.SBSAR))
        return;
      this.myShaderName = "";
    }

I guess the AssetPath is not set for instantiated materials done at runtime.

EDIT:
I used a Harmony patch to forcibly call the base.OnEnable and it works. 

It doesn't make sense for the override Substance code to short circuit this method and NOT call the base.OnEnable.  Should always call it.  If the assetPath is needed for Substance then do the if check after. 



Last Edit: June 07, 2020, 06:14:56 pm