Search Unity

[SOLVED] MaterialPropertyBlock Changes in Editor

Discussion in 'Shaders' started by flogelz, Jul 24, 2019.

  1. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    142
    Does somebody has an idea on how to preview custom settings, for example changing the texture via a material property block, inside of the editor? Yes, all these changes would start to work at runtime, but building your level without the corresponding textures doesn't seem like a proper solution-
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    [ExecuteInEditMode]?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I use a lot of material property blocks "in scene" that I apply on level load via per component scripts. All of them use [ExecuteInEditMode] to ensure it sets values on awake both in editor, and in play mode.
     
  4. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    142
    OH! I guessed, that changing values that use gpu instancing dont work in editor, just like batching only starts working, when the scene is in playmode- naive me.

    I just read that the update on [ExecuteInEditMode] only gets called, when i change something, which is pretty nice.
    Is there a function that can make the update function only executable in edit mode? Cause i wouldn't want these changes constantly at runtime.

    Code (CSharp):
    1.         props = new MaterialPropertyBlock();    
    2.         props.SetInt("_TextureIndex", TextureIndex);
    3.  
    4.         rend = GetComponent<MeshRenderer>();
    5.         rend.SetPropertyBlock(props);
    Also would i have to delete the created Material property block everytime? Not that those blocks are kept in the system. (I would just set this whole code snippet in the update).
     
  5. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    142
    Ah, for the only in editor, i found this code line, which seems to work well.
    Code (CSharp):
    1. if (Application.isEditor && !Application.isPlaying) {}
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I use something like this:
    Code (csharp):
    1. void Awake()
    2. {
    3.     // set material property block
    4. }
    5.  
    6. #if UNITY_EDITOR
    7. void Update()
    8. {
    9.     if (EditorApplication.isPlayingOrWillChangePlaymode)
    10.         return;
    11.     Awake();
    12. }
    13. #endif
    This ensures the update only happens in the editor, and for standalone builds the code isn’t there at all.
     
    Roman200333, ExtraCat and flogelz like this.
  7. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    142
    Thanks Bgolus! Works like a charm:) Also added a quick bool input, to only refresh at runtime, when i want too!
     
  8. Samhayne

    Samhayne

    Joined:
    Jun 15, 2009
    Posts:
    45
    I'm using OnValidate() as an alternative.

    Hm. Anyone knows a trick how to fix the rendering of prefabs in the preview window when using MaterialPropertyBlocks?
    It's no big issue of course... but I feel a bit dirty with a preview window just showing vertex colors....

    upload_2020-10-12_19-36-1.png