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

Correct way to use MaterialPropertyBlock.SetImage()

Discussion in 'Scripting' started by Mahgo, Oct 16, 2019.

  1. Mahgo

    Mahgo

    Joined:
    Dec 4, 2014
    Posts:
    52
    I'm using unity 2018.3

    I'm already successfully using MaterialPropertyBlock.SetColor() to combine draw calls of meshes that have the same material but a different color. I'm now trying to do the same for meshes with the same material but different texture. However, it is not combining the draw calls, and it gives me this reason: "Objects have different MaterialPropertyBlock set." What am I doing wrong?

    Code (CSharp):
    1.        
    2.         private MaterialPropertyBlock materialPropertyBlock;
    3.  
    4.         public void Awake()
    5.         {
    6.             this.materialPropertyBlock = new MaterialPropertyBlock();
    7.         }
    8.  
    9.         public void SetupMesh(byte[] textureData, MeshRenderer meshRenderer)
    10.         {
    11.             Texture2D texture = new Texture2D(1, 1, TextureFormat.RGB24, false);
    12.             texture.LoadImage(textureData);
    13.  
    14.             this.materialPropertyBlock.SetTexture("_MainTex", texture);
    15.             meshRenderer.SetPropertyBlock(this.materialPropertyBlock, 1);
    16.         }