<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ExecuteInEditMode &#8211; INCD021</title>
	<atom:link href="https://incd021.com/tag/executeineditmode/feed/" rel="self" type="application/rss+xml" />
	<link>https://incd021.com</link>
	<description>Programming, thoughts, life  and art.</description>
	<lastBuildDate>Mon, 16 Sep 2013 15:42:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>Unity Draw Call Batching</title>
		<link>https://incd021.com/2013/09/16/unity-draw-call-batching/</link>
					<comments>https://incd021.com/2013/09/16/unity-draw-call-batching/#respond</comments>
		
		<dc:creator><![CDATA[INC $D021]]></dc:creator>
		<pubDate>Mon, 16 Sep 2013 15:42:33 +0000</pubDate>
				<category><![CDATA[Earth Guardian]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[ExecuteInEditMode]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[texture]]></category>
		<category><![CDATA[Unity draw call batching]]></category>
		<category><![CDATA[Unity draw calls]]></category>
		<category><![CDATA[UV]]></category>
		<guid isPermaLink="false">http://incd021.com/?p=410</guid>

					<description><![CDATA[I was having problems with Unity draw call batching. I&#8217;ve started on a small 2d game (Earth Guardian) and had made a small mockup and was already having problems with....]]></description>
										<content:encoded><![CDATA[<p>I was having problems with Unity draw call batching. I&#8217;ve started on a small 2d game (Earth Guardian) and had made a small mockup and was already having problems with the number of draw calls <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f641.png" alt="🙁" class="wp-smiley" style="height: 1em; max-height: 1em;" /> It looked like I was getting an ekstra draw call for each sprite I created. So I didn&#8217;t know what I was doing..! So I had to find out what I was doing wrong.</p>
<p>Asking Google I found three possible things, that could be the problem.</p>
<ol>
<li>When creating my mesh I set mainTextureOffset and mainTextureScale.</li>
<li>My sprites use the build in transparent shader, which apperently could also kill the dynamic batching.</li>
<li>I set the material for all meshes to my spriteatlas texture. Not using shared materials.</li>
</ol>
<p><span style="line-height: 1.5;">The Code is as follows.</span></p>
<pre class="brush: actionscript3; gutter: true">public static GameObject CreateSprite(float x, float y, float width, float height, float scale, Material material)
    {
        y = materialHeight - y;
        float uvWidthUnit = 1f / materialWidth;
        float uvHeightUnit = 1f / materialHeight;

        float vertHeightScale = scale * (height / width);

        Mesh mesh = new Mesh();

        Vector3[] verts = new Vector3[4];
        Vector2[] uvs = new Vector2[4];
        int[] tris = new int[6] { 0, 1, 2, 2, 1, 3 };

        verts[0] = -Vector3.right * scale + Vector3.up * vertHeightScale;
        verts[1] = Vector3.right * scale + Vector3.up * vertHeightScale;
        verts[2] = -Vector3.right * scale - Vector3.up * vertHeightScale;
        verts[3] = Vector3.right * scale - Vector3.up * vertHeightScale;

        uvs[0] = new Vector2(0.0f, 1.0f);
        uvs[1] = new Vector2(1.0f, 1.0f);
        uvs[2] = new Vector2(0.0f, 0.0f);
        uvs[3] = new Vector2(1.0f, 0.0f);

        mesh.vertices = verts;
        mesh.triangles = tris;
        mesh.uv = uvs;

        mesh.RecalculateNormals();

        GameObject newMeshObject = new GameObject();
        newMeshObject.AddComponent&lt;MeshFilter&gt;().mesh = mesh;
        newMeshObject.AddComponent&lt;MeshRenderer&gt;();
        newMeshObject.renderer.material = material;
        newMeshObject.renderer.material.mainTextureOffset = new Vector2(uvWidthUnit * x, uvHeightUnit * y);
        newMeshObject.renderer.material.mainTextureScale = new Vector2(uvWidthUnit * width, uvWidthUnit * height);
        newMeshObject.AddComponent&lt;BoxCollider&gt;();
        Vector3 boxColliderSize = newMeshObject.GetComponent&lt;BoxCollider&gt;().size;
        boxColliderSize.z = 0.1f;
        newMeshObject.GetComponent&lt;BoxCollider&gt;().size = boxColliderSize;
        newMeshObject.AddComponent&lt;Rigidbody&gt;();
        Rigidbody rigidbody = newMeshObject.GetComponent&lt;Rigidbody&gt;();
        rigidbody.constraints = RigidbodyConstraints.FreezeAll;
        newMeshObject.isStatic = true;
        return newMeshObject;
    }</pre>
<p>So first I tried setting UV for the vertices instead of changing mainTextureOffset and mainTextureScale. For the first test I just removed the two lines, to see if the number of draw calls decreased.</p>
<p>The number of draw calls, on my main game screen, went down from 42 to 9 (I have some GUI taking up some calls).. <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> And when I started shooting, a lot, the number of draw calls stayed solid at 9. So a real improvement. <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Of cause now I had to test if changing the UV coordinates for each frame changes anything&#8230;. It didn&#8217;t effect the number of draw calls <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> This post from the <a href="http://answers.unity3d.com/questions/428183/modifying-maintextureoffset-increases-draw-calls.html">Unity Forum</a> says it all. So modifying mainTextureOffset and mainTextureScale is <strong>not</strong> the way to do it!  So no more testing, the UV way of doing things must be the way to go.</p>
<pre class="brush: actionscript3; gutter: true">[ExecuteInEditMode]
public class UVSetter : MonoBehaviour
{

    public float pixX;
    public float pixY;
    public float pixW;
    public float pixH;
    public float matrialSizeX;
    public float matrialSizeY;
    private Mesh theMesh;
    public bool updatable;
    // Use this for initialization
    void Start()
    {
        theMesh = transform.GetComponent&lt;MeshFilter&gt;().mesh;

        float uvx = pixX / matrialSizeX;
        float uvy = 1f - (pixY / matrialSizeY);
        float uvw = pixW / matrialSizeX;
        float uvh = pixH / matrialSizeY;

        Vector2[] newUVs = new Vector2[4];
        newUVs[0] = new Vector2(uvx, uvy);
        newUVs[3] = new Vector2(uvx, uvy + uvh);
        newUVs[2] = new Vector2(uvx + uvw, uvy);
        newUVs[1] = new Vector2(uvx + uvw, uvy + uvh);
        theMesh.uv = newUVs;
    }

    // Update is called once per frame
    void Update()
    {
        if (!updatable)
        {
            return; 
        }
        float uvx = pixX / matrialSizeX;
        float uvy = 1f - (pixY / matrialSizeY);
        float uvw = pixW / matrialSizeX;
        float uvh = pixH / matrialSizeY;

        Vector2[] newUVs = new Vector2[4];
        newUVs[0] = new Vector2(uvx, uvy);
        newUVs[3] = new Vector2(uvx, uvy + uvh);
        newUVs[2] = new Vector2(uvx + uvw, uvy);
        newUVs[1] = new Vector2(uvx + uvw, uvy + uvh);
        theMesh.uv = newUVs;
    }
}</pre>
<p>On a final note I just want to mention that, to get the editor to show the correct UV in the editor, I just added [ExecuteInEditMode] decoration to my UVSetter test class..</p>
<p>Now, go make games! <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>&#8211; Henning</p>
]]></content:encoded>
					
					<wfw:commentRss>https://incd021.com/2013/09/16/unity-draw-call-batching/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Page Caching using Disk: Enhanced 

Served from: incd021.com @ 2025-05-22 02:31:41 by W3 Total Cache
-->