<?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/"
	xmlns:series="http://organizeseries.com/"
	>

<channel>
	<title>Unity Cookie &#187; Scripting</title>
	<atom:link href="http://cgcookie.com/unity/category/tutorials/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://cgcookie.com/unity</link>
	<description>Unity Game Engine Tutorials and Resources</description>
	<lastBuildDate>Fri, 24 May 2013 19:15:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Using C# Static Members in Unity</title>
		<link>http://cgcookie.com/unity/2013/05/21/using-c-static-members-in-unity/</link>
		<comments>http://cgcookie.com/unity/2013/05/21/using-c-static-members-in-unity/#comments</comments>
		<pubDate>Wed, 22 May 2013 00:49:11 +0000</pubDate>
		<dc:creator>Wes McDermott</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[unity]]></category>
		<category><![CDATA[unity 3d]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=6114</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/05/unity_scripting_static_feature-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="unity_scripting_static_feature" /></div>Learn how to use C# Static ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/05/unity_scripting_static_feature-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="unity_scripting_static_feature" /></div><h2>Learn how to use C# Static Members to share data between scenes.</h2>
<p>In this Unity Cookie Quick Tip, we&#8217;ll take a look at sharing data between scenes using <strong><a href="http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx" target="_blank">C# Static Members</a></strong> in a Scene Manager script. I use a global scene manager to share data and between scenes that doesn&#8217;t need to be saved to disk. By using Static members, I can call my scene manager script without having to have a reference to it. The idea is that I am using my scene manager as <a href="http://csharpindepth.com/articles/general/singleton.aspx" target="_blank"><strong>Singleton</strong></a>. My scene manager is only instanced once and provides utility functions for managing global aspects of my game.</p>
<p>In this video, I will show how to commit user-defined data to the Scene Manager and recall that data in a separate scene. I also wrap the Application.LoadLevel into the Scene Manager to create a helper function for loading scenes.</p>
<h3>Scene Manager Script</h3>
<p></p><pre class="crayon-plain-tag">using UnityEngine;
using System.Collections;

public class SceneManager : MonoBehaviour {

	#region STATIC VARIABLES
	public static string userData;
	#endregion

	//STATIC FUNCTION
	public static void OpenScene(string sceneToLoad)
	{
		Application.LoadLevel(sceneToLoad);
	}
}</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2013/05/21/using-c-static-members-in-unity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Custom Pivots in Unity</title>
		<link>http://cgcookie.com/unity/2013/05/14/creating-custom-pivots-in-unity/</link>
		<comments>http://cgcookie.com/unity/2013/05/14/creating-custom-pivots-in-unity/#comments</comments>
		<pubDate>Tue, 14 May 2013 13:00:16 +0000</pubDate>
		<dc:creator>Wes McDermott</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Modeling]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[Gizmos]]></category>
		<category><![CDATA[pivot]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=6091</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/05/unity_editor_customPivots_feature-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="unity_editor_customPivots_feature" /></div>Creating Custom Pivots in Unity
In this ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/05/unity_editor_customPivots_feature-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="unity_editor_customPivots_feature" /></div><h2>Creating Custom Pivots in Unity</h2>
<p>In this Unity Cookie quick tip, we&#8217;ll take a look at creating custom pivots. You can&#8217;t directly manipulate pivots in Unity. You can either choose to transform an object using it&#8217;s center or pivot, but you can&#8217;t change the pivot. Most of the time this is not an issue as you&#8217;ll be working with 3D content from your 3D application and Unity will respect the pivot position as imported from the 3D object. However, there are times when you are working with native Unity objects where you need to adjust or create a new pivot.</p>
<p>Using a parent pivot object will allow you to not only create a custom pivot, but will also allow you to place a transformation at a higher level such as when you might need to separate the rotation and position transformations. This is common in camera follow scripts.</p>
<h3>Drawing Gizmos</h3>
<p>In the video, I also talk about helper scripts that use the <strong><a href="http://docs.unity3d.com/Documentation/ScriptReference/Gizmos.html">Gizmo</a></strong> class. I use the Gizmo class often in my project to create helper editor objects when building my games. Here is the code mentioned in the video to draw a simple wireframe sphere in the editor.</p><pre class="crayon-plain-tag">using UnityEngine;
using System.Collections;

public class Gizmo : MonoBehaviour {

	public float gizmoSize = .75f;
	public Color gizmoColor = Color.yellow;

	void OnDrawGizmos()
	{
		Gizmos.color = gizmoColor;
		Gizmos.DrawWireSphere(transform.position, gizmoSize);
	}
}</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2013/05/14/creating-custom-pivots-in-unity/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Animate a line draw using Line Renderer Component</title>
		<link>http://cgcookie.com/unity/2013/05/03/animate-a-line-draw-using-line-renderer-component/</link>
		<comments>http://cgcookie.com/unity/2013/05/03/animate-a-line-draw-using-line-renderer-component/#comments</comments>
		<pubDate>Fri, 03 May 2013 16:30:26 +0000</pubDate>
		<dc:creator>Wes McDermott</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[line renderer]]></category>
		<category><![CDATA[unity3d]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=6003</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/05/unity_scripting_lineDraw_feature-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="unity_scripting_lineDraw_feature" /></div>Learn to animate a line draw ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/05/unity_scripting_lineDraw_feature-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="unity_scripting_lineDraw_feature" /></div><h2>Learn to animate a line draw using the Line Renderer Component</h2>
<p>I recently worked on a project where I needed to draw a line from one <strong><a href="http://docs.unity3d.com/Documentation/Components/class-GameObject.html" target="_blank">game object</a></strong> to another. That is easy enough to do using the <strong><a href="http://docs.unity3d.com/Documentation/Components/class-LineRenderer.html" target="_blank">Line Renderer component</a></strong>, however, I wanted to animate the line renderer moving from point A to point B. In this video quick tip, I show you how I accomplished this effect with just a<a href="http://cgcookie.com/unity/2011/12/12/introduction-to-scripting-in-unity/" target="_blank"><strong> small bit of scripting</strong></a>.</p>
<p><a href="http://cgcookie.com/unity/files/2013/05/unity_scripting_lineDraw_ss01.jpg"><img class="alignnone  wp-image-6007" alt="unity_scripting_lineDraw_ss01" src="http://cgcookie.com/unity/files/2013/05/unity_scripting_lineDraw_ss01.jpg" width="666" height="389" /></a></p>
<p>&nbsp;</p>
<h3>Finding a position along a line</h3>
<p>The key to animating the line is to calculate a position along the known distance of the line and feed this updated position to the line renderer component&#8217;s &#8220;<a href="http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.SetPosition.html" target="_blank"><strong>SetPosition</strong></a>&#8221; method. You do this in the Update function.</p>
<h4>To calculate the postion along the line, I used the following steps.</h4>
<ol>
<li><strong>Get the unit vector in the desired direction</strong></li>
<li><strong>Multiply by the desired length</strong></li>
<li><strong>Add the starting point.</strong></li>
</ol>
<p>In order to actually animate the line, I increment a variable that is interpolated between 0 and the known distance of the line. The interpolation is handled by using <a href="http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html" target="_blank"><strong>Mathf.Lerp</strong></a>.</p>
<p><a href="http://cgcookie.com/unity/files/2013/05/unity_scripting_lineDraw_ss02.jpg"><img class="alignnone  wp-image-6008" alt="unity_scripting_lineDraw_ss02" src="http://cgcookie.com/unity/files/2013/05/unity_scripting_lineDraw_ss02.jpg" width="666" height="226" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2013/05/03/animate-a-line-draw-using-line-renderer-component/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Quick Tips: Combining multiple materials into a single draw call</title>
		<link>http://cgcookie.com/unity/2013/03/21/quick-tips-combining-multiple-materials-into-a-single-draw-call/</link>
		<comments>http://cgcookie.com/unity/2013/03/21/quick-tips-combining-multiple-materials-into-a-single-draw-call/#comments</comments>
		<pubDate>Thu, 21 Mar 2013 16:28:39 +0000</pubDate>
		<dc:creator>Alex Telford</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cgprogram]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[unity 3d]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=5591</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/03/lowPolyTrees-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="lowPolyTrees" /></div>Technical Quick Tip: Combining multiple materials into ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2013/03/lowPolyTrees-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="lowPolyTrees" /></div><h2>Technical Quick Tip: Combining multiple materials into a single draw call</h2>
<p>It&#8217;s time to get down and dirty with some scripting in <a title="Unity Game Engine Official Site" href="http://unity3d.com" target="_blank">Unity</a>.</p>
<p>In this <strong>Technical Unity Cookie Quick Tip</strong>, I take you through the process of using scripting to combine multiple materials and vertex colors into a single material with a single draw call. We&#8217;ll be starting with the <strong><a title="Mesh Color Script" href="http://docs.unity3d.com/Documentation/ScriptReference/Mesh-colors.html" target="_blank">mesh.color script available here</a></strong> from Unity.</p>
<p>This technique is based around changing the vertex colors with scripting, and then taking those colors into the shader, now this technique is not just for changing colors, we can also use it to change textures, and pass variables to the shader on a per object basis while all objects keep the same material and therefore within a single draw call.</p>
<p><a href="http://cgcookie.com/unity/files/2013/03/treesHeader.jpg"><img class="alignnone  wp-image-5592" alt="treesHeader" src="http://cgcookie.com/unity/files/2013/03/treesHeader.jpg" width="666" height="214" /></a></p>
<p>&nbsp;</p>
<h2>Referenced Tutorials in the Tip</h2>
<ul>
<li><a href="http://cgcookie.com/unity/series/introduction-to-surface-shader-scripting/" target="_blank"><span style="line-height: 12.997159004211426px;">Surface Shader Series</span></a></li>
<li><a href="http://cgcookie.com/unity/cgc-series/noob-to-pro-shader-writing-for-unity-4-beginner/" target="_blank">Intro to Shader Writing</a></li>
</ul>
<h2>Got questions?</h2>
<p>Feel free to leave them below or see you on the forums!</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2013/03/21/quick-tips-combining-multiple-materials-into-a-single-draw-call/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Unity Tower Defense Tutorial: Part 9 AI Tanks</title>
		<link>http://cgcookie.com/unity/2012/12/14/unity-tower-defense-tutorial-part-9-ai-tanks/</link>
		<comments>http://cgcookie.com/unity/2012/12/14/unity-tower-defense-tutorial-part-9-ai-tanks/#comments</comments>
		<pubDate>Fri, 14 Dec 2012 06:00:42 +0000</pubDate>
		<dc:creator>Gabriel williams</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Gabriel Williams]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[cgc-featured]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[flying]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=2187</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/12/TDT8_AI-Pathfinding_Banner-649x245.png" class="attachment-post-thumbnail wp-post-image" alt="TDT8_AI-Pathfinding_Banner" /></div>Tower Defense Tutorial: Part 9 AI ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/12/TDT8_AI-Pathfinding_Banner-649x245.png" class="attachment-post-thumbnail wp-post-image" alt="TDT8_AI-Pathfinding_Banner" /></div><h3>Tower Defense Tutorial: Part 9 AI Tanks</h3>
<p>In this video, we will take our new found knowledge of AI Pathfinding and apply it to real, in-game Tanks! This means AI units that move along the ground, and intelligently dodge obstacles, relentlessly marching toward your base.  Luckily, we will also cover upgrading your defenses- setting up projectiles and turrets for this new enemy.</p>
<h3>What is covered in this tutorial:</h3>
<ul>
<li>Applying AI to an in-game enemy</li>
<li>Upgrading projectile, enemy, and turret scripts to all be class-based</li>
<li>Creating spawns for the ground units</li>
<li>Efficiently rebuilding the AI Grid and Re-Pathing all units as needed</li>
</ul>
<p>&nbsp;</p>
<p><strong> This tutorial uses the &#8220;A&#8221; Pathfinding Project</strong></p>
<ul>
<li><a title="&quot;A&quot; Pathfinding Project" href="http://www.arongranberg.com/unity/a-pathfinding/" target="_blank">Download &#8220;A&#8221; Pathfinding Project</a></li>
<li><a href="http://www.arongranberg.com/astar/docs/getstarted.php" target="_blank">View Getting started tutorial on the &#8220;A&#8221; Pathfinding project.</a></li>
</ul>
<p><a href="http://cgcookie.com/unity/files/2012/12/TDT9_Tanks.png"><img class="alignleft  wp-image-2188" title="TDT9_Tanks" alt="" src="http://cgcookie.com/unity/files/2012/12/TDT9_Tanks.png" width="666" height="280" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2012/12/14/unity-tower-defense-tutorial-part-9-ai-tanks/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
	
		<series:name><![CDATA[Tower Defense]]></series:name>
	</item>
		<item>
		<title>Unity Tower Defense Tutorial: Part 8 AI Pathfinding</title>
		<link>http://cgcookie.com/unity/2012/12/11/unity-tower-defense-tutorial-part-8-path-finding/</link>
		<comments>http://cgcookie.com/unity/2012/12/11/unity-tower-defense-tutorial-part-8-path-finding/#comments</comments>
		<pubDate>Tue, 11 Dec 2012 18:28:54 +0000</pubDate>
		<dc:creator>Gabriel williams</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Gabriel Williams]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[cgc-featured]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[flying]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=2174</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/12/TDT8_AI-Pathfinding_Banner-649x245.png" class="attachment-post-thumbnail wp-post-image" alt="TDT8_AI-Pathfinding_Banner" /></div>Tower Defense Tutorial: Part 8 &#8211; ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/12/TDT8_AI-Pathfinding_Banner-649x245.png" class="attachment-post-thumbnail wp-post-image" alt="TDT8_AI-Pathfinding_Banner" /></div><h3>Tower Defense Tutorial: Part 8 &#8211; AI Pathfinding</h3>
<p>This tutorial will focus almost exclusively on setting up a fully-functional AI Pathfinding solution for our Tower Defense game! This paves the way for our ground-based attackers, allowing them to dynamically maneuver around turrets, even as you place them. Of course, AI Pathfinding can be useful for many other projects, so give this one your full attention!</p>
<h3>What is covered in this tutorial:</h3>
<ul>
<li>Downloading and importing the &#8220;A* Pathfinding Project&#8221;</li>
<li>Setting up a grid-based A* Graph</li>
<li>Creating a script to intelligently Path your Units</li>
<li>Using the Character Controller component to move units along the AI Path</li>
</ul>
<p><iframe width="666" height="375" src="http://www.youtube.com/embed/_KZT4lVVZN0?rel=0" frameborder="0" allowfullscreen=""></iframe></p>
<p><strong> This tutorial uses the &#8220;A&#8221; Pathfinding Project</strong></p>
<ul>
<li><a title="&quot;A&quot; Pathfinding Project" href="http://www.arongranberg.com/unity/a-pathfinding/" target="_blank">Download &#8220;A&#8221; Pathfinding Project</a></li>
<li><a href="http://www.arongranberg.com/astar/docs/getstarted.php" target="_blank">View Getting started tutorial on the &#8220;A&#8221; Pathfinding project.</a></li>
</ul>
<p><a href="http://cgcookie.com/unity/files/2012/12/TDT8_AI-Pathfinding_Banner.png"><img class="alignleft  wp-image-2175" title="TDT8_AI-Pathfinding_Banner" alt="" src="http://cgcookie.com/unity/files/2012/12/TDT8_AI-Pathfinding_Banner.png" width="666" height="280" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2012/12/11/unity-tower-defense-tutorial-part-8-path-finding/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<series:name><![CDATA[Tower Defense]]></series:name>
	</item>
		<item>
		<title>Introduction to Surface Shaders in Unity &#8211; Part 07</title>
		<link>http://cgcookie.com/unity/2012/11/26/introduction-to-surface-shaders-in-unity-part-0/</link>
		<comments>http://cgcookie.com/unity/2012/11/26/introduction-to-surface-shaders-in-unity-part-0/#comments</comments>
		<pubDate>Mon, 26 Nov 2012 16:00:10 +0000</pubDate>
		<dc:creator>Alex Telford</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[cgprogram]]></category>
		<category><![CDATA[hlsl]]></category>
		<category><![CDATA[materials]]></category>
		<category><![CDATA[rim lighting]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=1806</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/10/part71-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="Rim Lighting" /></div>Learn to write surface shaders in ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/10/part71-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="Rim Lighting" /></div><h3>Learn to write surface shaders in Unity in this complete tutorial series.</h3>
<p>In this <a href="http://unity3d.com" target="_blank">Unity</a> tutorial series we will be giving you an introduction <a href="http://cgcookie.com/unity/tag/shader" target="_blank">shader</a> <a href="http://cgcookie.com/unity/tag/scripting/" target="_blank">scripting</a>, showing you how to write your our own surface shaders for your Unity game.</p>
<p>This text tutorial is accompanied by a complete video. I recommend watching the video and referring to the text as reference.</p>
<p>Be sure to read the tutorial carefully, and post any questions you may have if you get stuck, as there is a lot to cover. I will be using some examples from the Unity shader reference found here:<br />
<a href="http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html">http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html</a></p>
<p>My goal is to help you understand how shaders work and how you can write your own shaders from scratch in Unity.</p>
<p><strong>NOTE: </strong>This tutorial requires very basic knowledge of scripting such as variables and functions. If you have no scripting experience I recommend you first watch our <a href="http://cgcookie.com/unity/2011/11/30/unity-3d-introduction-to-animation-2/">Introduction to Scripting tutorial</a> in the getting started section.</p>
<p>In part 6 we added cubemap reflections to our shader, now we will boost our silhouettes by adding rim lighting</p>
<h2>Part 7 &#8211; Adding rim lighting to our Unity surface shaders</h2>
<p>The last part of writing surface shaders that I want to teach you before we get into writing our own lighting models, is the Rim lighting.</p>
<ul>
<li>Rim lighting can be used without writing a lighting model and is really easy to use.</li>
<li>We are going to make use of the dot product in this shader, which allows us to get a value of 0 to 1 based on the angle.</li>
<li>We can use viewDir as the input angle to get some cool results.</li>
</ul>
<p>For this one we will be using the viewDir to have the value based on the angle we are looking from. So first let&#8217;s break down what we want to do.</p>
<ul>
<li>For rim lighting, we want to have some light glow around the edges of the model.</li>
<li>Now viewDir is 1 when we are looking straight at it, and 0 when it is perpendicular.<br />
- If we used this as the emission it would be bright at the center and diffuse at the rim. The opposite of what we want.<br />
- So we will need the inverse of that.</li>
</ul>
<p>Let&#8217;s write the shader.</p><pre class="crayon-plain-tag">Shader &quot;unityCookie/Rim Lighting&quot; {
	Properties {
		_MainTex (&quot;Diffuse Texture&quot;, 2D) = &quot;white&quot; {}
		_BumpTex (&quot;Normal Map&quot;, 2D) = &quot;bump&quot; {}
		_RimColor (&quot;Rim Color&quot;, Color) = (1,1,1,1) //The color tint of our rim light
		_RimPower (&quot;Rim Power&quot;, Range(0.1,10)) = 3.0 //The power of our rim, note that we start from 0.1 as we don't really want 0.
	}
	Subshader {
		Tags { &quot;RenderType&quot; = &quot;Opaque&quot;}
		CGPROGRAM
		#pragma surface surf Lambert
		struct Input {
			float2 uv_MainTex;
			float2 uv_BumpTex;
			float3 viewDir; //We want to get the viewing angle
		};
		sampler2D _MainTex;
		sampler2D _BumpTex;
		float4 _RimColor;
		float _RimPower;
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
			o.Normal = UnpackNormal (tex2D (_BumpTex, IN.uv_BumpTex));
			half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal)); // we take the view angle and place it in a 0-1 half value
			o.Emission = _RimColor.rgb * pow (rim, _RimPower); // we take our rim output (0-1) and take it to the power of our rim power.
		}
		ENDCG
	}
	Fallback &quot;Diffuse&quot;
}</pre><p>&nbsp;</p>
<p>Ok so there is an interesting line in there</p>
<blockquote><p>half rim = 1.0 &#8211; saturate(dot (normalize(IN.viewDir), o.Normal));</p></blockquote>
<p>So let&#8217;s break this up.</p>
<ul>
<li><strong>saturate()</strong> &#8211; This makes sure that the value stays between -1 and 1, as we don&#8217;t want it to go outside of the valid color ranges.</li>
<li><strong>dot()</strong> &#8211; This takes the dot product of our view angle with the normal angle and returns a value from 0-1</li>
<li><strong>normalize()</strong> &#8211; This removes any values less than 0, as when the normals are facing away from you they go into negatives which we don&#8217;t normally want.</li>
</ul>
<p>And finally we have the <strong>pow()</strong> function, which takes the first number to the power of the second number. (2 times three to the power of 4 = 2 * (3*3*3*3) )</p>
<p>So that&#8217;s our last shader for this series, in the next part we will take a look at debugging and optimizing our shaders.</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2012/11/26/introduction-to-surface-shaders-in-unity-part-0/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<series:name><![CDATA[Introduction to Surface Shader Scripting]]></series:name>
	</item>
		<item>
		<title>Introduction to Surface Shaders in Unity &#8211; Part 06</title>
		<link>http://cgcookie.com/unity/2012/11/23/introduction-to-surface-shaders-in-unity-part-06/</link>
		<comments>http://cgcookie.com/unity/2012/11/23/introduction-to-surface-shaders-in-unity-part-06/#comments</comments>
		<pubDate>Fri, 23 Nov 2012 16:00:50 +0000</pubDate>
		<dc:creator>Alex Telford</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[cgprogram]]></category>
		<category><![CDATA[cubemap]]></category>
		<category><![CDATA[hlsl]]></category>
		<category><![CDATA[normal]]></category>
		<category><![CDATA[shaderlab]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=1799</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/10/part61-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="Cubemap reflections" /></div>Learn to write surface shaders in ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/10/part61-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="Cubemap reflections" /></div><h3>Learn to write surface shaders in Unity in this complete tutorial series.</h3>
<p>In this <a href="http://unity3d.com" target="_blank">Unity</a> tutorial series we will be giving you an introduction <a href="http://cgcookie.com/unity/tag/shader" target="_blank">shader</a> <a href="http://cgcookie.com/unity/tag/scripting/" target="_blank">scripting</a>, showing you how to write your our own surface shaders for your Unity game.</p>
<p>This text tutorial is accompanied by a complete video. I recommend watching the video and referring to the text as reference.</p>
<p>Be sure to read the tutorial carefully, and post any questions you may have if you get stuck, as there is a lot to cover. I will be using some examples from the Unity shader reference found here:<br />
<a href="http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html">http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html</a></p>
<p>My goal is to help you understand how shaders work and how you can write your own shaders from scratch in Unity.</p>
<p><strong>NOTE: </strong>This tutorial requires very basic knowledge of scripting such as variables and functions. If you have no scripting experience I recommend you first watch our <a href="http://cgcookie.com/unity/2011/11/30/unity-3d-introduction-to-animation-2/">Introduction to Scripting tutorial</a> in the getting started section.</p>
<p>In part 5 we took a look at adding specularity to our shaders, now let&#8217;s take that a step further and add cubemap reflections.</p>
<h2>Part 6 &#8211; Adding cubemap reflections to our Unity surface shaders</h2>
<p>Instead of specularity, perhaps you want an object to be reflective such as a window, plastic or a teapot. This is where cubemaps come into play.</p>
<p>So let&#8217;s take our lambert diffuse shader and add some reflections:</p><pre class="crayon-plain-tag">Shader &quot;unityCookie/Cubemap Reflection&quot; {
	Properties {
		_MainTex (&quot;Diffuse Texture&quot;, 2D) = &quot;white&quot; {}
		_Cube (&quot;Cube Map&quot;, CUBE) = &quot;&quot; {} //A property so we can add the cube map in Unity
	}
	Subshader {
		Tags { &quot;RenderType&quot; = &quot;Opaque&quot;}
		CGPROGRAM
		#pragma surface surf Lambert
		struct Input {
			float2 uv_MainTex;
			float3 worldRefl; //This is a pre-defined variable in unity for world space reflections
		};
		sampler2D _MainTex;
		samplerCUBE _Cube; //We sample the cubemap so we can use it
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
			o.Emission = texCUBE (_Cube, IN.worldRefl).rgb;
		}
		ENDCG
	}
	Fallback &quot;Diffuse&quot;
}</pre><p>We introduce four new commands.</p>
<ul>
<li><strong>worldRefl;</strong> This is a built in variable which allows you to display an image in world space, making it appear as if the material is reflecting.</li>
<li><strong>samplerCUBE</strong>  This is like the sampler2D, but is specific to sampling cube maps, this tells Unity the map is a cubemap.</li>
<li><strong>o.Emission</strong>  This makes the surface emit this color, which is important for lighting and refelctions. It does not mean that the surface is actually emitting light.</li>
<li><strong>texCUBE</strong>  Like tex2D, but instead of a flat image we want to treat this like a box.</li>
</ul>
<p>So this is great, but what about the normal map? How do we get that to work? Well normal maps are a bit more tricky, we need to use the <em>INTERNAL_DATA</em> to get the <em>WorldReflectionVector</em> of our normal. Sounds confusing, but it isn&#8217;t much more to add to our code:</p><pre class="crayon-plain-tag">Shader &quot;unityCookie/Bumped Cubemap Reflection&quot; {
	Properties {
		_MainTex (&quot;Diffuse Texture&quot;, 2D) = &quot;white&quot; {}
		_BumpTex (&quot;Normal Map&quot;, 2D) = &quot;bump&quot; {}
		_Cube (&quot;Cube Map&quot;, CUBE) = &quot;&quot; {}
	}
	Subshader {
		Tags { &quot;RenderType&quot; = &quot;Opaque&quot;}
		CGPROGRAM
		#pragma surface surf Lambert
		struct Input {
			float2 uv_MainTex;
			float2 uv_BumpTex;
			float3 worldRefl; //We don't use this down below, but it is used in the INTERNAL_DATA module so we still need to specify we want it.
			INTERNAL_DATA // Notice how this is in all caps, this is telling Unity we want to use the internal data module, we won't get into what that is but just know that it's got some cool stuff in it.
		};
		sampler2D _MainTex;
		sampler2D _BumpTex;
		samplerCUBE _Cube;
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
			o.Normal = UnpackNormal (tex2D (_BumpTex, IN.uv_BumpTex));
			o.Emission = texCUBE (_Cube, WorldReflectionVector( IN, o.Normal)).rgb; //We take the world reflection vector from our normals, and we simply give it all our inputs
		}
		ENDCG
	}
	Fallback &quot;Diffuse&quot;
}</pre><p>Okay so hopefully that&#8217;s not too confusing. The WorldReflectionVector is a built in command that takes our normals and world space reflection, then uses the internal data to figure out what the reflection should be according to the normals.</p>
<p>Whew, this stuff get&#8217;s tricky doesn&#8217;t it? But don&#8217;t worry, once you understand it, this stuff will be a piece of cake.</p>
<p>In the next step we will move on to rim lighting.</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2012/11/23/introduction-to-surface-shaders-in-unity-part-06/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<series:name><![CDATA[Introduction to Surface Shader Scripting]]></series:name>
	</item>
		<item>
		<title>Introduction to Surface Shaders in Unity &#8211; Part 05</title>
		<link>http://cgcookie.com/unity/2012/11/21/introduction-to-surface-shaders-in-unity-part-05/</link>
		<comments>http://cgcookie.com/unity/2012/11/21/introduction-to-surface-shaders-in-unity-part-05/#comments</comments>
		<pubDate>Wed, 21 Nov 2012 21:21:13 +0000</pubDate>
		<dc:creator>Alex Telford</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[cgprogram]]></category>
		<category><![CDATA[hlsl]]></category>
		<category><![CDATA[shaderlab]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[specularity]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=1793</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/10/part51-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="Adding Specularity" /></div>Learn to write surface shaders in ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/10/part51-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="Adding Specularity" /></div><h3>Learn to write surface shaders in Unity in this complete tutorial series.</h3>
<p>In this <a href="http://unity3d.com" target="_blank">Unity</a> tutorial series we will be giving you an introduction <a href="http://cgcookie.com/unity/tag/shader" target="_blank">shader</a> <a href="http://cgcookie.com/unity/tag/scripting/" target="_blank">scripting</a>, showing you how to write your our own surface shaders for your Unity game.</p>
<p>This text tutorial is accompanied by a complete video. I recommend watching the video and referring to the text as reference.</p>
<p>Be sure to read the tutorial carefully, and post any questions you may have if you get stuck, as there is a lot to cover. I will be using some examples from the Unity shader reference found here:<br />
<a href="http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html">http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html</a></p>
<p>My goal is to help you understand how shaders work and how you can write your own shaders from scratch in Unity.</p>
<p><strong>NOTE: </strong>This tutorial requires very basic knowledge of scripting such as variables and functions. If you have no scripting experience I recommend you first watch our <a href="http://cgcookie.com/unity/2011/11/30/unity-3d-introduction-to-animation-2/">Introduction to Scripting tutorial</a> in the getting started section.</p>
<p>In part 4 we covered how to add normal maps to our shaders, now let&#8217;s take a look at adding specularity</p>
<h2>Part 5 &#8211; Adding specularity to our Unity surface shaders</h2>
<p>Our shader is looking great, but it is still using lambert shading. Let&#8217;s add some specular highlights to our shader and while we&#8217;re at it we will use some awesome properties to control the specularity.</p>
<p>First, before we can use specularity we need to change our lighting model. Currently we are using &#8216;Lambert&#8217;, to get specularity we need &#8216;BlinnPhong&#8217;, this is a predefined lighting model in the Lighting.cginc file in your Unity installation. (You can actually add lighting models to this cginc file, but that&#8217;s a topic for another day.)</p>
<p>So what properties does a specular shader add to the mix?</p>
<p>Well we have three new ones:</p>
<ul>
<li><strong>_SpecColor</strong> &#8211; This is used in the lighting model itself to control the tint of the specular highlights. As it&#8217;s already being called in the lighting model, we don&#8217;t need to do anything else with it.</li>
<li><strong>o.Specular</strong> &#8211; This is the specular power, or how soft the falloff is on the specularity.</li>
<li><strong>o.Gloss</strong> &#8211; If you want to use an image to control where the specular highlights will show, this is where it is output. You can think of this as a black and white image to cut out areas of specularity.</li>
</ul>
<p>So let&#8217;s apply specularity to our shader:</p><pre class="crayon-plain-tag">Shader &quot;unityCookie/Bumped Specular&quot; {
	Properties {
		_MainTex (&quot;Diffuse Texture&quot;, 2D) = &quot;white&quot; {}
		_BumpTex (&quot;Normal Map&quot;, 2D) = &quot;bump&quot; {}
		_SpecColor (&quot;Specular Color&quot;, Color) = (1,1,1,1) //This passes a color to the BlinnPhong lighting model
		_SpecPower (&quot;Specular Power&quot;, Range(0, 2)) = 0.5 //How strong our specular highlight it, notice how I have the range going to 2 to allow for some really sharp highlights
	}
	Subshader {
		Tags { &quot;RenderType&quot; = &quot;Opaque&quot;}
		CGPROGRAM
		#pragma surface surf BlinnPhong //We are now using the BlinnPhong lighting model
		struct Input {
			float2 uv_MainTex;
			float2 uv_BumpTex;
		};
		sampler2D _MainTex;
		sampler2D _BumpTex;
		float4 _SpecPower; //We define the _SpecPower as a float4 (4 floating variables for RGBA)
		void surf (Input IN, inout SurfaceOutput o) {
			fixed4 tex = tex2D (_MainTex, IN.uv_MainTex); //We define our texture as a fixed4 so we can extract the alpha to use as a gloss map
			o.Albedo = tex.rgb
			o.Normal = UnpackNormal (tex2D (_BumpTex, IN.uv_BumpTex));
			o.Specular = _SpecPower; //We use the spec power to control the intensity of our specularity
			o.Gloss = tex.a; //We use the alpha channel of our image to control where we want specularity.
		}
		ENDCG
	}
	Fallback &quot;Diffuse&quot;
}</pre><p>&nbsp;</p>
<p>Cool, so most of this should make sense, notice how we defined our _SpecPower with the sampler2D&#8217;s, This is where all properties go.</p>
<p>Next we have this one:</p>
<blockquote><p>o.Gloss = tex.a;</p></blockquote>
<p>We are using the alpha channel of our image to define the specular highlights. This means we don&#8217;t need a third image to control specularity.</p>
<p>You should be familiar with using spec maps by now, but if not, they are a black and white image, where the black cuts out the specularity to make it look more Lambert. this helps add an extra level of detail to the shader.</p>
<p>Next we will go in add take a look at cubemap reflections&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2012/11/21/introduction-to-surface-shaders-in-unity-part-05/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<series:name><![CDATA[Introduction to Surface Shader Scripting]]></series:name>
	</item>
		<item>
		<title>Unity Tower Defense Tutorial: Part 7- Upgrading Turrets</title>
		<link>http://cgcookie.com/unity/2012/11/13/tower-defense-upgrading-turrets/</link>
		<comments>http://cgcookie.com/unity/2012/11/13/tower-defense-upgrading-turrets/#comments</comments>
		<pubDate>Tue, 13 Nov 2012 17:05:02 +0000</pubDate>
		<dc:creator>Gabriel williams</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Gabriel Williams]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[cgc-featured]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[flying]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://cgcookie.com/unity/?p=2090</guid>
		<description><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/11/TDT7_Upgrade_Banner-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="TDT7_Upgrade_Banner" /></div>Tower Defense Tutorial: Part 7- Upgrading ]]></description>
				<content:encoded><![CDATA[<div><img width="649" height="245" src="http://cgcookie.com/unity/files/2012/11/TDT7_Upgrade_Banner-649x245.jpg" class="attachment-post-thumbnail wp-post-image" alt="TDT7_Upgrade_Banner" /></div><h3>Tower Defense Tutorial: Part 7- Upgrading Turrets</h3>
<p>No Tower Defense game is complete without up-gradable structures! This tutorial will setup your game for upgrades, and also lay some groundwork for purchasing of other items such as inventory, etc. Best of all, we will delve just a bit into &#8220;extending&#8221; from base classes. This is a technique you will find useful in many, many cases, and very simple to use.</p>
<h3>What is covered in this tutorial:</h3>
<ul>
<li>Creating a new &#8220;Upgrade Structure&#8221; GUI Panel</li>
<li>Creating multiple levels of each Structure</li>
<li>Using a Base Class, and Extending it with other scripts</li>
<li>Creating a generic function to handle cost-checking when purchasing</li>
</ul>
<p><img class="alignnone  wp-image-2091" title="TDT7_Upgrade_Banner" alt="" src="http://cgcookie.com/unity/files/2012/11/TDT7_Upgrade_Banner.jpg" width="666" height="280" /></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://cgcookie.com/unity/2012/11/13/tower-defense-upgrading-turrets/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
	
		<series:name><![CDATA[Tower Defense]]></series:name>
	</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Object Caching 29917/30192 objects using apc

 Served from: cgcookie.com @ 2013-05-25 06:34:34 by W3 Total Cache -->