<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bob Levesh</title>
    <description>The latest articles on DEV Community by Bob Levesh (@bob_levesh).</description>
    <link>https://dev.to/bob_levesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4114983%2F51971fe0-fd01-43d4-a397-f80de958fd2a.jpg</url>
      <title>DEV Community: Bob Levesh</title>
      <link>https://dev.to/bob_levesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bob_levesh"/>
    <language>en</language>
    <item>
      <title>Do Androids Dream of MaterialPropertyBlock?</title>
      <dc:creator>Bob Levesh</dc:creator>
      <pubDate>Tue, 08 Sep 2026 06:31:17 +0000</pubDate>
      <link>https://dev.to/bob_levesh/do-androids-dream-of-materialpropertyblock-ag6</link>
      <guid>https://dev.to/bob_levesh/do-androids-dream-of-materialpropertyblock-ag6</guid>
      <description>&lt;p&gt;I had been developing mobile games for over a decade and found myself adrift in the doldrums of game design.  Every mechanic had been used to exhaustion and was no longer a differentiator in the marketplace; so for someone who came to mobile gaming on that magnificent wave of brutal, punishing gameplay exemplified by Flappy Bird, I was in a real jam.  I had to look beyond game mechanics.  Instead of focusing on those silly things every purist eschews, like retention, CPI, or even playtime, I decided it would probably be a good idea if I learned a bit more about graphics and use that knowledge  to juice up my future games.  I got the basics down then went looking for inspiration.  I didn’t have to look for long.  Led Zeppelin had Muddy Waters, Kant had Hume, and I had ‘Pistol Whip’.  That VR game had me at the first shot.  For those that have never seen it, you don’t need to play the game to appreciate the graphics, everything in the game changes color, and it does so multiple times, and certain objects shake, split apart, flash white, and turn back to ‘normal’.  A very cool aesthetic in my opinion, and the kind of thing I wanted to ‘borrow’ for my new game idea.  &lt;/p&gt;

&lt;p&gt;I was making my game, as usual, using Unity game engine and I would be making it for mobile devices.  The idea was that you would be in a aircraft chasing down and shooting at alien spaceships.  The aliens would have a number displaying the number of hits needed to destroy them (nice of those aliens to give us that information) and it would be displayed on the ship (easy to keep track of the hit count without having to glance around the screen - this is the sort of thing we game developers think about).  Whenever the alien was hit I wanted it to flash white and shake apart then reform and return back to it’s original color very similar to the behavior I saw in ‘Pistol Whip’.  I also wanted the scenery to change color every time an alien spaceship was destroyed.  There would be multiple alien ships per level.  Well, I had a clear idea of the visuals and its dynamic behavior along with a great reference in ‘Pistol Whip’.  It was time to get started.&lt;/p&gt;

&lt;p&gt;My knowledge of graphics basics gave me the answer right away; I figured I’d have the graphics done before lunch.  Here is a basic primer: anything that you can see in a game made with Unity is called a game object (let’s forget about scriptable objects for now) and they can be seen thanks to a property they all have called ‘material’.  Materials also have properties, one of which is ‘color’.  So in a script I would change every game object’s material’s color when it was time.  All I needed was this code snippet: &lt;code&gt;GetComponent&amp;lt;Renderer&amp;gt;().material.color = [my color of choice];&lt;/code&gt;.    I was already thinking about lunch - maybe a double cheeseburger, I think I earned it.  Sometimes game development can be a breeze.  Unfortunately this wasn’t one of those times.&lt;/p&gt;

&lt;p&gt;Unity’s Profiler is supposed to be every game developer’s friend.  It’s the kind of friend that tells you what you need to hear and not what you want to hear.  It was telling me my game’s frame rate was dropping below 20 frames per second (fps).  Some friend.  Of course, 60 fps is an easy standard to achieve and one I would always get with my other games.  So why was it so low?  And when would I be able to eat that double cheeseburger?&lt;/p&gt;

&lt;p&gt;I found an answer to the former, and the answer to the latter would come once frame rate was fixed.&lt;/p&gt;

&lt;p&gt;Looking at the Profiler I saw there were too many draw calls, too many instances of game object Materials.  Batching was broken.  It turned out using renderer.material (remember my awesome code snippet?) was forcing Unity to instantiate unique material copies on the heap.  This forced the GPU to process every single mesh (the points in 3d space that define the object, like a skeleton) in its own separate draw call.  I couldn’t be more inefficient if I tried.  I had to find a solution.  Think.  Think.&lt;/p&gt;

&lt;p&gt;If the problem was with the graphics then that’s where the solution would be, right?  So I went in for a deep dive into game graphics and said bye-bye to that double cheeseburger - maybe some other day.&lt;/p&gt;

&lt;p&gt;Ok, so I knew that using renderer.material was creating clones of each material, which broke batching and increased memory usage and slowed down the game’s fps.  So I needed an alternative solution.  Thankfully, the big brains at Unity HQ (wherever that is) had already thought of one.  You guessed it. Enter MaterialPropertyBlock.  The long and short of it is that the MaterialPropertyBlock (let’s call it MPB now that we’re on familiar terms) allows the developer to change properties, like color, on objects that share the same shader without creating clones and breaking batching.  Caloo-Calay! Problem solved.  Let’s use that MPB.&lt;/p&gt;

&lt;p&gt;Documentation, once again, saved the day.  Examples of code always clear up any confusions in implementation.  For the MPB all I needed to do was create an MPB instance and then set the color, right?  Well not so fast.  Like the Trucker’s Hitch it looks easy but upon closer inspection your thoughts can’t follow the thread and you end up confusing yourself.  Let’s make sure we don’t get tied up in any knots.&lt;/p&gt;

&lt;p&gt;The renderer, remember we were directly changing its color earlier causing all this trouble in the first place, also has a method called ‘SetPropertyBlock’, and the method takes a MaterialPropertyBlock as an argument.  Now we’re cooking!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;MaterialPropertyBlock&lt;/span&gt; &lt;span class="n"&gt;_mpb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...setup and initializations &lt;/span&gt;

    &lt;span class="n"&gt;_mpb&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MaterialPropertyBlock&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SetObjColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Renderer&lt;/span&gt; &lt;span class="n"&gt;targetRenderer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;targetColor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Renderers register with this script&lt;/span&gt;

    &lt;span class="n"&gt;_mpb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"_Color"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;targetColor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;targetRenderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetPropertyBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_mpb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And like that, frame rate was back up to 60 fps!&lt;/p&gt;

&lt;p&gt;Recap:  I still use each game object’s renderer, remember I am giving game objects unique colors, but I no longer use renderer.material.color.  Instead of that slow, cumbersome code I streamlined with the MaterialPropertyBlock.  I create ONE instance of it and set its color, that’s something I can do continuously during gameplay - changing the color - and then apply that color to one object and NOT clone the material and NOT break batching.  How simple.  Sometimes game development really can be a breeze.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>csharp</category>
      <category>performance</category>
      <category>unity3d</category>
    </item>
  </channel>
</rss>
