<?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>script &#8211; INCD021</title>
	<atom:link href="https://incd021.com/tag/script-2/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>
		<item>
		<title>Unity touch input</title>
		<link>https://incd021.com/2013/09/10/unity-touch-input/</link>
					<comments>https://incd021.com/2013/09/10/unity-touch-input/#comments</comments>
		
		<dc:creator><![CDATA[INC $D021]]></dc:creator>
		<pubDate>Tue, 10 Sep 2013 18:52:07 +0000</pubDate>
				<category><![CDATA[Game]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Touch]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Unity Touch Input]]></category>
		<guid isPermaLink="false">http://incd021.com/?p=402</guid>

					<description><![CDATA[My first encounter with Unity touch input didn&#8217;t go as I wanted it to.. After some failed attempt and a test scene in Unity, it did started to make sense.....]]></description>
										<content:encoded><![CDATA[<p>My first encounter with Unity touch input didn&#8217;t go as I wanted it to.. After some failed attempt and a test scene in Unity, it did started to make sense. And finding out that touch has phases and FingerId&#8217;s helped a lot. <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;" /> So here&#8217;s my take on a controller and firing script for my current project.</p>
<pre class="brush: csharp; gutter: true">private shieldFingerId = -1;

...
// If nothing is touched, the shield controller has been released
// Know I should test for the shieldFingerId to be released, but this works for my purpose.
if (Input.touchCount == 0)
{
	shieldFingerId = -1;
}

foreach (Touch touch in Input.touches)
{
	// Fire
	if(touch.phase == TouchPhase.Began &amp;&amp; !withinShieldController)
	{
		Fire();
	}

	// &quot;Grap&quot; shield
	if (touch.phase == TouchPhase.Began &amp;&amp; withinShieldController)
	{
		shieldFingerId = touch.fingerId;
		UpdateSheldAngle(touch.position);
	}

	// Move shield
	if (touch.phase == TouchPhase.Moved &amp;&amp; touch.fingerId == shieldFingerId)
	{
		UpdateSheldAngle(touch.position);
	}
}</pre>
<p>As you can see, when I got hold of the information about touch.phase and .fingerId, everything turned out to be very simple indeed.</p>
<p>I found the Unity Script Reference pages to be very limited on the subject of touch input, e.g. information about touch.fingerId only says &#8220;The unique index for touch.&#8221; no examples, or explanation. But by asking Google, and searching the Unity forums, great resource I might add, it finally made sense. <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;" /></p>
<p>Working with touch has forced me to use my LG E975 mobile for testing, which is a slow process. :/ I guess I have to figure out how to make Unity Remote work with my phone. If not, development is going to take forever!</p>
<p>Hope the above code makes sense? Now go make games! <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;" /></p>
<p>&#8211; Henning</p>
]]></content:encoded>
					
					<wfw:commentRss>https://incd021.com/2013/09/10/unity-touch-input/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Player movement script for the 3&#215;3 POC</title>
		<link>https://incd021.com/2013/06/07/player-movement-script-for-the-3x3-poc/</link>
					<comments>https://incd021.com/2013/06/07/player-movement-script-for-the-3x3-poc/#comments</comments>
		
		<dc:creator><![CDATA[INC $D021]]></dc:creator>
		<pubDate>Fri, 07 Jun 2013 15:32:58 +0000</pubDate>
				<category><![CDATA[3x3]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[POC]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Unity Script]]></category>
		<category><![CDATA[Unity3D]]></category>
		<guid isPermaLink="false">http://incd021.com/?p=277</guid>

					<description><![CDATA[I just wanted to show how I chose to do the player control script. After playing the game some more, I&#8217;ve decided that the game isn&#8217;t going to work on....]]></description>
										<content:encoded><![CDATA[<p>I just wanted to show how I chose to do the player control script. After playing the game some more, I&#8217;ve decided that the game isn&#8217;t going to work on the mobile devises. The swipe to move the player would be very counter intuitive after level 8.</p>
<p>For the movement script, I wanted the player to move in a continuous motion and not in discreet jumps. The player should also be able to keep moving in one direction holding down a direction button.</p>
<p>To keep track of what the player is doing, I first made an enum with the possible motion states.<br />
I guess the enums are self explanatory <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;" /></p>
<pre class="brush: actionscript3; gutter: true">enum Moving
{
    NO,
    UP,
    DOWN,
    LEFT,
    RIGHT
};</pre>
<p>Now the the logic. If the player isn&#8217;t moving, check for player input.</p>
<pre class="brush: actionscript3; gutter: true">
        if (direction == Moving.NO)
        {
            if (Input.GetKey(KeyCode.A))
            {
                direction = Moving.LEFT;
            }
            if (Input.GetKey(KeyCode.D))
            {
                direction = Moving.RIGHT;
            }
            if (Input.GetKey(KeyCode.W))
            {
                direction = Moving.UP;
            }
            if (Input.GetKey(KeyCode.S))
            {
                direction = Moving.DOWN;
            }
        }</pre>
<p>Now if the player is moving, the enum will be holding the direction of motion and we can move the player.</p>
<pre class="brush: actionscript3; gutter: true">            switch (direction)
            {
                case Moving.UP:
                    if (moveLeft &gt; 0f)
                    {
                        Vector3 v3 = transform.position;
                        v3.y += movementSpeed;
                        moveLeft -= +movementSpeed;
                        if (v3.y &gt; 1f)
                        {
                            v3.y = 1f;
                            moveLeft = 1.0f;
                            direction = Moving.NO;
                        }
                        transform.position = v3;
                    }
                    else
                    {
                        moveLeft = 1.0f;
                        direction = Moving.NO;
                    }
                    break;
                case Moving.DOWN:
                    if (moveLeft &gt; 0f)
                    {
                        Vector3 v3 = transform.position;
                        v3.y -= movementSpeed;
                        moveLeft -= +movementSpeed;
                        if (v3.y &lt; -1f)
                        {
                            v3.y = -1f;
                            moveLeft = 1.0f;
                            direction = Moving.NO;
                        }
                        transform.position = v3;
                    }
                    else
                    {
                        moveLeft = 1.0f;
                        direction = Moving.NO;
                    }
                    break;
                case Moving.LEFT:
                    if (moveLeft &gt; 0f)
                    {
                        Vector3 v3 = transform.position;
                        v3.x -= movementSpeed;
                        moveLeft -= +movementSpeed;
                        if (v3.x &lt; -1f)
                        {
                            v3.x = -1f;
                            moveLeft = 1.0f;
                            direction = Moving.NO;
                        } transform.position = v3;
                    }

                    else
                    {
                        moveLeft = 1.0f;
                        direction = Moving.NO;
                    }
                    break;
                case Moving.RIGHT:
                    if (moveLeft &gt; 0f)
                    {
                        Vector3 v3 = transform.position;
                        v3.x += movementSpeed;
                        moveLeft -= +movementSpeed;
                        if (v3.x &gt; 1f)
                        {
                            v3.x = 1f;
                            moveLeft = 1.0f;
                            direction = Moving.NO;
                        }
                        transform.position = v3;
                    }
                    else
                    {
                        moveLeft = 1.0f;
                        direction = Moving.NO;
                    }
                    break;
                default:
                    break;
            }</pre>
<p>That&#8217;s it, not pretty but it works. If I have the time, I would like to make the motion even more fluent. The state change if you keep a key down makes the player give a little jump/jitter as the state goes through NO state before returning to a moving state. But for now it gets the job done <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;" /></p>
<p>All feedback is welcome, I can&#8217;t change if I don&#8217;t know what to change. <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;" /></p>
<p>&#8211; Henning</p>
]]></content:encoded>
					
					<wfw:commentRss>https://incd021.com/2013/06/07/player-movement-script-for-the-3x3-poc/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Page Caching using Disk: Enhanced 

Served from: incd021.com @ 2025-05-18 01:44:54 by W3 Total Cache
-->