<?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>Everyday 3D &#187; CSharp</title>
	<atom:link href="http://www.everyday3d.com/blog/index.php/category/unity3d/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.everyday3d.com/blog</link>
	<description>3D user experience on the web. Design, technology news. A blog by Bartek Drozdz.</description>
	<lastBuildDate>Fri, 01 Jul 2011 09:12:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Woodbots, an interactive installation</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/12/21/woodbots-an-interactive-installation/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/12/21/woodbots-an-interactive-installation/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 09:25:06 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[News & Events]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[3d camera]]></category>
		<category><![CDATA[ars thanea]]></category>
		<category><![CDATA[fotonic]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[interactive installation]]></category>
		<category><![CDATA[interactive institute]]></category>
		<category><![CDATA[North kingdom]]></category>
		<category><![CDATA[Radeon HD 5870]]></category>
		<category><![CDATA[Skellefteå]]></category>
		<category><![CDATA[Skellefteå Airport]]></category>
		<category><![CDATA[Umeå]]></category>
		<category><![CDATA[Woodbot]]></category>
		<category><![CDATA[Woodbot Pilots]]></category>

		<guid isPermaLink="false">http://www.everyday3d.com/blog/?p=1788</guid>
		<description><![CDATA[For the past 5 moths I have been working with North Kingdom on an interactive installation called Woodbot Pilots. I&#8217;m happy to announce that it had been launched last week! It&#8217;s located at the Skellefteå Airport in Northern Sweden. If you happen to be in the area, make sure to check it out. However, as [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.everyday3d.com/unity3d/woodbots/woodbots001-testing.jpg" width="520" height="364"></p>
<p>For the past 5 moths I have been working with <a href="http://northkingdom.com">North Kingdom</a> on an interactive installation called <strong>Woodbot Pilots</strong>. I&#8217;m happy to announce that it had been launched last week! </p>
<p>It&#8217;s located at the <a href="http://maps.google.pl/maps?q=skelleftea+airport&#038;oe=utf-8&#038;client=firefox-a&#038;ie=UTF8&#038;hq=&#038;hnear=Skellefte%C3%A5+Airport+(SFT),+Skellefte%C3%A5,+V%C3%A4sterbotten,+Sweden&#038;gl=pl&#038;ll=64.614459,20.917969&#038;spn=5.527356,23.269043&#038;z=6">Skellefteå Airport</a> in Northern Sweden. If you happen to be in the area, make sure to check it out. However, as I imagine, it isn&#8217;t a location easily accessible to most of my readers, so I will be posting more pictures &#038; videos as they appear.</p>
<p><iframe src="http://player.vimeo.com/video/18297796?portrait=0" width="520" height="293" frameborder="0"></iframe></p>
<p><em>UPDATE 28.01.2011 Here&#8217;s a video made by guys over at Interactive Institute in Umeå. The also published an article that you can <a href="http://interactiveinstituteumea.wordpress.com/2010/12/31/woodbot-pilots-3d-camera-project/">read here</a>.</em></p>
<p>Together with North Kingdom we are also working on a small website dedicated to the project. It will go live soon. In the meantime there&#8217;s a <a href="http://norran.se/nyheter/norrochvasterbotten/article1116110.ece">nice article from the opening ceremony</a> in the local newspaper, <a href="http://norran.se">Norran</a>, where you can see Mr. Robert Lindberg, the director of Skellefteå Airport doing the first run.</p>
<p><strong>Woodbot Pilots</strong> is a gestures controlled racing game. We display it on a huge <strong>82&#8243; HD touch screen (191 x 123 cm!)</strong> and a special <strong>3D depth camera</strong> is used to read the gestures. The camera was developed by a company called <a href="http://www.fotonic.com/content/Default.aspx">Fotonic</a> and is somehow similar to MS Kinect but has a bigger range (up to 7m) and is optimized for industrial environments.</p>
<p>The talented artists from <a href="http://www.arsthanea.com/">Ars Thanea</a> and North Kingdom created beautiful visuals and the sound engineers from <a href="http://www.dinahmoe.com/">Dinamhoe</a> added a great soundtrack. The game itself was developed in the <a href="http://www.unity3d.com">Unity3D</a> engine. We connected the 3D camera using a C/C++ plugin developed by programmers over at <a href="http://www.tii.se/groups/umea">Interactive Institute in Umeå</a> who did a fantastic job &#8211; it runs very smoothly. </p>
<p>The game logic is implemented in C# &#8211; quite some code in there, even though the game concept is relatively simple. Thanks to a powerful graphic card (XFX Radeon HD 5870 1GB GDDR5) the thing <strong>runs at 60 FPS in full HD resolution</strong> which is quite amazing!</p>
<p>I will talk about the technical details in a future article. For the moment enjoy some pictures of the race track, and <strong>Happy Christmas everyone</strong>!</p>
<p><img src="http://www.everyday3d.com/unity3d/woodbots/track01.jpg" width="520" height="303"></p>
<p><img src="http://www.everyday3d.com/unity3d/woodbots/track02.jpg" width="520" height="303"></p>
<p><img src="http://www.everyday3d.com/unity3d/woodbots/track03.jpg" width="520" height="303"></p>
<p><img src="http://www.everyday3d.com/unity3d/woodbots/track04.jpg" width="520" height="303"></p>
<p><img src="http://www.everyday3d.com/unity3d/woodbots/track05.jpg" width="520" height="303"></p>
<p><em>Also, make sure to check out <a href="http://www.northkingdom.com/blog/woodbot/">this article</a> on North Kingdom&#8217;s website &#8211; there are a lot of pictures of the installation itself.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/12/21/woodbots-an-interactive-installation/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>C# events and Unity3d</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/10/04/c-events-and-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/10/04/c-events-and-unity3d/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 20:29:12 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.everyday3d.com/blog/?p=1660</guid>
		<description><![CDATA[Did you know that C# has a built-in event system? And a very good one! It can be quite useful with Unity3D. Here's an example. In order to respond to events dispatched by a GameObject you typically would create a script that extends MonoBehaviour and implement the methods you need. So if you want to [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know that C# has a <strong>built-in event system</strong>? And a very good one! It can be quite useful with <a href="http://unity3d.com">Unity3D</a>. Here's an example.</p>
<p>In order to respond to <strong>events dispatched by a GameObject</strong> you typically would create a script that extends <a href="http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html">MonoBehaviour</a> and implement the methods you need. So if you want to react to the user hovering the object with the mouse you would create the <a href="http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseOver.html">OnMouseOver</a> method. It would typically look something like this: </p>
<div class="igBar"><span id="lcsharp-6"><a href="#" onclick="javascript:showPlainTxt('csharp-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-6">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> OnMouseOver <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">renderer.<span style="color: #0000FF;">material</span>.<span style="color: #0000FF;">color</span> = Color.<span style="color: #0000FF;">red</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>That works fine. But what if you want another object to be notified about that event? One way would be to keep a reference to that other object in your script and call it from within your method: </p>
<div class="igBar"><span id="lcsharp-7"><a href="#" onclick="javascript:showPlainTxt('csharp-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-7">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> MyScript myScript;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> OnMouseOver <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myScript.<span style="color: #0000FF;">NotifyMouseOver</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This also works fine, but you always need to keep a reference to the object you notify. Also, you can only notify this object, unless you keep more references to other object. This can quickly get messy. </p>
<p><strong>Messages</strong></p>
<p>Another solution would be to use <a href="http://unity3d.com/support/documentation/ScriptReference/Component.SendMessage.html">SendMessage</a> or <a href="http://unity3d.com/support/documentation/ScriptReference/Component.SendMessageUpwards.html">SendMessageUpwards</a> methods. While they seem to be the right tool for the job, these methods have several major flaws, and in my opinion you should completely avoid using them.</p>
<p>Their syntax is awkward to start with, and you need to pass method names as string literals - which is very error prone! Also, those methods work only within the object's transform hierarchy. You can invoke methods on scripts that are either attached to the same GameObject or to one of it ancestors.</p>
<p><strong>Events</strong></p>
<p>Fortunately there is a better way to solve this problem, and this is with the C# built in event system. I can't explain how the event system works in detail, but if you are interested on learning more on this topic, go and check <a href="http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx">the tutorial on MSDN</a>. </p>
<p>Now, let's see how to use events in Unity3D. Consider this script:</p>
<div class="igBar"><span id="lcsharp-8"><a href="#" onclick="javascript:showPlainTxt('csharp-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-8">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> UnityEngine;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EventDispatcher : MonoBehaviour <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> EventHandler<span style="color: #000000;">&#40;</span>GameObject e<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> EventHandler MouseOver;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> OnMouseOver <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>MouseOver != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; MouseOver <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">gameObject</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Don't worry if you don't know exactly what it does. What's important is that once you attach it to a GameObject you can go to another script (any script in your project!) and if you have a reference to that object you can write something like this:</p>
<div class="igBar"><span id="lcsharp-9"><a href="#" onclick="javascript:showPlainTxt('csharp-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-9">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> GameObject s;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>...<span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">s.<span style="color: #0000FF;">GetComponent</span>&lt;EventDispatcher&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">MouseOver</span> += Listener;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>...<span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> Listener<span style="color: #000000;">&#40;</span>GameObject g<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// g is being hovered, do something...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This approach is more flexible than using messages, because it works with any script not only with those that are in the same transform hierarchy. If you keep a Singleton object that manages the state of the application, you could listen to events coming from any GameObject in it. Of course not only a GameObject can send events - any object can. </p>
<p>One of the consequences of such setup is that the same listening function can be used to respond to events coming from different objects. By passing a reference as argument you always know which object dispatched the event - this is what I do in the example.</p>
<p><strong>References, controllers and MVC</strong></p>
<p>In the first scenario above you needed to keep a reference to the listener in the object that sends the event, and I said it's not a good idea. In the version which uses the built-in events, it needs to be the other way around - you need a reference to the object that sends the event in the object that listens to it. How is it better, you might ask?</p>
<p>First of all, here, the sender doesn't know who is listening to the events is sends - and even how many listeners there are. All it does is to send the event and forget. In the first scenario above, imagine how cumbersome it would be to tell the sender to stop notifying the listener!</p>
<p>With the event system, it's the listener that has the responsibility to decide what events it listens to, when to start, and when to stop listening to them. Such object usually manages the state of the application or executes some game logic. To borrow the term from the MVC design pattern - it's <strong>the controller</strong>. That is why it's perfectly logical to give him such responsibility. In that way, using events creates more solid code. </p>
<p>Finally, I love the overloaded += operator used to add a listener, it's so clean! As you might have guessed by now, when you are done listening to the event you can just say:</p>
<div class="igBar"><span id="lcsharp-10"><a href="#" onclick="javascript:showPlainTxt('csharp-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-10">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">s.<span style="color: #0000FF;">GetComponent</span>&lt;EventDispatcher&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">MouseOver</span> -= Listener; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Of course you can create a generic EventDispatcher class that implements all events that a GameObject can send - <a href="http://www.everyday3d.com/unity3d/events/EventDispatcher.cs.txt">here's a version</a> that implements a few of them already. By looking at the code, I'm sure you can figure out how to add others. However, beware of implementing OnGUI that way! (if you want to know why, <a href="http://www.mindthecube.com/blog/2010/09/avoiding-performance-cost-of-ongui">read this post</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/10/04/c-events-and-unity3d/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Unity3D training</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/10/04/unity3d-training/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/10/04/unity3d-training/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 11:18:45 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[News & Events]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[battersea power plant]]></category>
		<category><![CDATA[london]]></category>
		<category><![CDATA[october 2010]]></category>
		<category><![CDATA[unity 3]]></category>
		<category><![CDATA[unity3d training]]></category>

		<guid isPermaLink="false">http://www.everyday3d.com/blog/?p=1700</guid>
		<description><![CDATA[I'm proud to announce, that I will be running a 2 day training in London later this month. I will be teaching Unity3D, showing how to use the editor and how to work with various assets. I will be also giving an introduction to scripting in C# and hopefully we will be able to cover [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lfpug.com/hands-on-unity3d/"><img src="http://www.everyday3d.com/blog-assets/training/lfpug_oct_battersea.jpg" width="520" height="327" alt="Hands-on Unity3d, 21-22 Oct 2010" border="0"/></a></p>
<p>I'm proud to announce, that I will be running a <strong>2 day training</strong> in London later this month. I will be teaching <strong><a href="http://unity3d.com">Unity3D</a></strong>, showing how to use the editor and how to work with various assets. I will be also giving an introduction to scripting in C# and hopefully we will be able to cover some features of the latest release of the editor.</p>
<p><strong>Unity3D version 3</strong> has been released only a few days ago. The new version is packed a tremendous amount of new and cool features. Great games &#038; other projects are created with Unity3D every day, so there has never been a better moment to start learning it!</p>
<p>You can read all the details about the training on the <a href="http://www.lfpug.com/hands-on-unity3d/">LFPUG page</a>, who is also the organizer of the session. Unfortunately, the tickets have been <strong>sold out</strong>. However, if you would like to learn some Unity3D, please contact me (bartek [at] everyday3d.com) or Tink (training [at] lfpug.com). We are pretty excited about the interest people are showing for this topic, so currently we are planning to run <strong>another session in London in November</strong> and there are still <strong>a few places</strong> left on the list.</p>
<p>I got a couple of requests to run such a training in other places, so hopefully I will be bringing some Unity3D goodness to <strong>other cities in Europe</strong> as well. I'm working out the details, and I'll keep you posted. If you would be interested in <strong>hosting a Unity3D training session</strong> in your city, please let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/10/04/unity3d-training/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Loading 3d models at runtime in Unity3d</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/05/24/loading-3d-models-runtime-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/05/24/loading-3d-models-runtime-unity3d/#comments</comments>
		<pubDate>Mon, 24 May 2010 13:59:32 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[collada]]></category>
		<category><![CDATA[obj]]></category>
		<category><![CDATA[wavefront obj]]></category>

		<guid isPermaLink="false">http://www.everyday3d.com/blog/?p=1363</guid>
		<description><![CDATA[IMPORTANT UPDATE! With Unity 3 there were some changes in the API and code described below doesn't work. Here's a new version, that works with Unity 3: obj (v1.2). Now, instead of creating a new object, attach the OBJ script to a game object and add the path to the .obj file in the inspector. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>IMPORTANT UPDATE!</strong> With Unity 3 there were some changes in the API and code described below <strong>doesn't work</strong>. Here's a new version, that <strong>works with Unity 3</strong>: <a href="http://www.everyday3d.com/unity3d/obj/obj.1.2.unitypackage">obj (v1.2)</a>. Now, instead of creating a new object, attach the OBJ script to a game object and add the path to the .obj file in the inspector.</p>
<p>This post is an addition to the last series I wrote about <a href="http://www.everyday3d.com/blog/index.php/2010/03/08/unity3d-beyond-games/">dynamic content loading</a> in Unity3D. </p>
<p>When building a dynamic application, every now and then you might need to load some 3d models at runtime. Either because there's a database with models you want to use or just because you want someone to update the models without rebuilding the project. I actually had this situation a few weeks ago. Surprisingly, <strong>this option does not come with Unity out of the box</strong>. </p>
<p><strong>Asset bundles and resource folders</strong></p>
<p>But first thing first. I said there's no ready-to-use way to load 3d models at runtime, but it's not 100% true. In fact, there are two options: <strong>asset bundles</strong> and <strong>resource folders</strong>. These methods are discussed <a href="http://unity3d.com/support/documentation/Manual/Loading%20Resources%20at%20Runtime.html">in depth here</a>. </p>
<p>They can both be useful in some cases, but they both have one fundamental drawback: in order to create them, someone needs to open the project in Unity, import the 3d models, create the bundles or resource folders and then export the whole thing again. To create an <strong>AssetBundle</strong> you need to run a script in the editor, while the <strong>Resource Folders</strong>... honestly, I failed to even make them work! </p>
<p>In brief, it's all too complicated for what I was looking for. </p>
<p><strong>A straightforward solution</strong></p>
<p>What I needed and wanted was easy and simple: to <strong>load a 3d object (geometry + some materials and textures) at runtime, using just a URL as parameter</strong>. The only solution seemed to write my own importer. </p>
<p>Basically, I had the choice between <a href="https://collada.org/mediawiki/index.php/COLLADA_-_Digital_Asset_and_FX_Exchange_Schema">Collada</a> and <a href="http://en.wikipedia.org/wiki/Obj">Wavefornt OBJ</a>. I would choose OBJ any time of the day because it's a simple, concise plain-text format, while Collada is bloated and is XML-based. </p>
<p>It's not that I hate XML (although I'm not a big fan either) but in order to parse XML you need to include a pretty weighty DLL in your *.unity3d file, around 850Kb, which in this case (and in many others) defeats the purpose. Still, it's good to know that it possible, and there are situations when it's ok to use it. If you want to learn <strong>more about Unity3D and XML</strong> there's a <a href="http://www.paultondeur.com/2010/03/23/tutorial-loading-and-parsing-external-xml-and-json-files-with-unity-part-1-xml/">awesome article</a> on this topic by <a href="http://www.paultondeur.com"/>Paul Tondeur</a>. </p>
<p>It turned out that while it's not rocket science to write an OBJ importer, it's not exactly banal either. I spent a few days coding it so I thought I'll share this with everyone - maybe someone will make good use of it. </p>
<p>Here's <a href="http://www.everyday3d.com/unity3d/obj/obj.1.1.unitypackage">a package with the source code (v1.1)</a> (or a version that works in Unity 3 <a href="http://www.everyday3d.com/unity3d/obj/obj.1.2.unitypackage">(v 1.2)</a>), along with a simple scene demonstrating how it works. In fact it couldn't be simpler. All you have to do is to create an instance of the OBJ class and start a coroutine to load the contents, like this: </p>
<div class="igBar"><span id="lcsharp-12"><a href="#" onclick="javascript:showPlainTxt('csharp-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-12">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">string</span> path = <span style="color: #808080;">"http://www.everyday3d/unity3d/obj/monkey.obj"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">OBJ obj = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OBJ<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">StartCoroutine<span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span>path<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Supported features</strong></p>
<ul>
<li>Vertex, normals and UV data</li>
<li>Multiple objects per file</li>
<li>Vertex groups and multiple materials per object</li>
<li><a href="http://en.wikipedia.org/wiki/Material_Template_Library">MTL</a> files, diffuse and specular materials</li>
<li>Basic textures</li>
</ul>
<p>And it's all at a cost of ~12Kb extra added to your final file!</p>
<p><strong>Testing and the universality of OBJ format</strong></p>
<p>In the 3D world the <strong>OBJ format is nearly universal</strong> - I think that every 3D editor in existence is able to export to this format. This also means that Wavefront files come in many different flavors.</p>
<p>I tested the code against models created with <a href="http://blender.org">Blender</a> and against common sense assumptions on how an OBJ file can be constructed. If it doesn't work with files exported from your editor send me the OBJ file, and if possible I'll update the code. It's <strong>released under the MIT license</strong> so feel free to use it in your projects, commercial or not.</p>
<p>That's it! I hope you'll find it useful! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/05/24/loading-3d-models-runtime-unity3d/feed/</wfw:commentRss>
		<slash:comments>70</slash:comments>
		</item>
		<item>
		<title>Runtime objects in Unity3D</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/04/14/runtime-objects-with-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/04/14/runtime-objects-with-unity3d/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 11:30:04 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[city]]></category>
		<category><![CDATA[generating 3d geometry]]></category>
		<category><![CDATA[material]]></category>
		<category><![CDATA[procedural city]]></category>
		<category><![CDATA[procedural mesh]]></category>
		<category><![CDATA[self-lit shader]]></category>
		<category><![CDATA[texture]]></category>

		<guid isPermaLink="false">http://www.everydayflash.com/blog/?p=1272</guid>
		<description><![CDATA[Unity3D can work with meshes created in external 3D editors, but geometry can also be created with code. And not only geometry, but materials and textures as well. To test all these options I created a simple cityscape. I got inspired by this set of awesome pictures and also by a project called Pixel City [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.everyday3d.com/unity3d/city/04/"><img src="http://www.everyday3d.com/unity3d/city/city.jpg" width="520" height="239" border="0"></a></p>
<p><strong><a href="http://unity3d.com">Unity3D</a></strong> can work with meshes created in external 3D editors, but <strong>geometry can also be created with code</strong>. And not only geometry, but materials and textures as well. To test all these options I created a simple cityscape. I got inspired by this set of <a href="http://www.boston.com/bigpicture/2010/03/nyc_and_las_vegas_from_above_a.html">awesome pictures</a> and also by a project called <a href="http://www.youtube.com/watch?v=-d2-PtK4F6Y">Pixel City</a> by <a href="http://www.shamusyoung.com/">Shamus Young</a>. </p>
<p><strong>Creating meshes</strong></p>
<p>All information about the geometry of an object is stored in the <code><a href="http://unity3d.com/support/documentation/ScriptReference/Mesh.html">Mesh</a></code> object inside the <code><a href="http://unity3d.com/support/documentation/ScriptReference/MeshFilter.html">MeshFilter</a></code> component. A mesh is composed of an array of vertices grouped into triangles and with UV information added to it. It's a pretty common way to store information on 3d geometry, similar to what you can find in Actionscript - <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/Graphics.html#drawTriangles%28%29">Graphics.drawTriangles</a> expects arguments in the same data format. Many file formats out there store meshes that way too, ex. <a href="http://en.wikipedia.org/wiki/Obj">Wavefront OBJ</a> (which makes writing importers/exporters relatively easy by the way).</p>
<p>Creating a mesh with code means populating the arrays of vertices, triangles and UVs. While it is pretty straightforward, even building a simple cube can be tricky and the code quickly becomes a unmanageable list of numbers. In the <a href="http://www.everyday3d.com/unity3d/city/city.unityPackage">source code</a>, the buildings are generated in the <strong>BuildingBuilder.cs</strong> file, so you can see it for yourselves.</p>
<p><strong>Creating textures</strong></p>
<p>We've <a href="http://www.everyday3d.com/blog/index.php/2010/03/09/loading-and-manipulating-images-in-unity3d/">already seen</a> that any bitmap you import into a Unity3D project is represented by the class <code><a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.html">Texture2D</a></code>. <strong>Of course, bitmaps don't have to be imported - they can be created from scratch</strong>. The constructor of the Texture2D class take a few arguments, including the width and height of the image. </p>
<p>Once a new texture is created all it's pixels are empty (i.e. black and transparent). Their values can be changed either on a pixel-per-pixel basis using the <code><a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.SetPixel.html">SetPixel</a></code> method or by copying portions of other bitmaps using <code><a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.SetPixels.html">SetPixels</a></code>. </p>
<p>In the above example I have 6 predefined textures with different types of windows, so I don't create those from scratch. However I use a <strong>procedural texture for the illumination maps</strong>. I generate a 64x64 bitmap where the value of each pixel determines the brightness of a single window (i.e. how much light it emits). It's pretty basic: after the Texture2D is created I just run through all it's pixels and assign a random value to it's alpha component.</p>
<p><strong>Creating materials</strong></p>
<p>There are 6 different textures representing a different type of window each and one illumination map. <strong>How to put these together? That's what materials are used for.</strong> As with any other element, materials are represented by the <code><a href="http://unity3d.com/support/documentation/ScriptReference/Material.html">Material</a></code> object in scripts, and they can be created with code. The constructor takes only one argument: a shader. </p>
<p><strong>Shaders are one of the most crucial elements of Unity3D</strong> (and any other 3D rendering engine for that matter). They describe how a 3D geometry will be rendered, what colors and/or textures will be used and how it will interact with light. For the buildings I use a shader called <a href="http://unity3d.com/support/documentation/Components/shader-SelfIllumDiffuse.html">Self-Illuminated Diffuse</a>. It's one of Unity3D built-in shaders. </p>
<p>Shaders usually need some parameters to work with - like colors or textures. <strong>A material is essentially a shader and a number of objects that define it's parameters.</strong> Self-Illuminated shaders expect one texture for the color and the other that determines the amount of light emitted by this texture. It's great for creating objects that emit light like bulbs, neon signs or... windows in a night scenery (it's important to understand however that the object emits light but this light doesn't illuminate any other objects around it). </p>
<p>I use the window image as the color texture, and the procedural image with random alpha values as the illumination map texture. The trick is that I set the scale of the color texture to 64. This way the window is repeated 64x64 times on the material and each pixel of the procedural illumination map (scaled 1:1) corresponds to one single window. I also set the filtering mode of the illumination map texture to <a href="http://unity3d.com/support/documentation/ScriptReference/FilterMode.Point.html">Point</a>, so that it doesn't get blurred when seen form a bigger distance. </p>
<p><strong>A word on optimization</strong></p>
<p>The generated buildings have different width, height and depth. A natural solution to apply textures on all of them was to create a material that repeats the windows the appropriate amount of times for each building. Ex. a 4 story building with 3 windows in a row would have a material where the x-scale of the texture is set to 3 and y-scale to 4. The emission map scale needs to be divided by 64 in this case, giving values of 3/64 and 4/64 respectively. </p>
<p>While this seems quite ok, it poses an optimization problem. Unity3D is much more effective when there's a <strong>lower amount of meshes composed of large amount of vertices</strong>, rather that the other way around - lots of meshes with few vertices. One building has 20 vertices and I create 2500 separate meshes! It's far from optimal, and you can really feel the framerate going down because of that. Fortunately there is a <strong>built-in script called CombineChildren</strong> that automatically combines all the buildings into one huge mesh making everything work much faster and smoother. But (of course) there is a trick!</p>
<p>Unity3D will combine objects only <strong>if they share the same, exact material</strong>. The scale of a texture on a material is unique, so if there are two materials that are identical and the only difference is the scale of the texture, we still need 2 different materials. In the above scenario almost each building uses a different material, unless it has exactly the same number of stories and the same number of windows in a row. I had to come up with a way to use only one material, and to deal with texture coordinates in a different way. </p>
<p>The answer is to <strong>use UVs instead of the texture scaling</strong>. So <strong>I started by creating only one material</strong>, which contains 64x64 windows. If a building is ex. 20 stories high and has 6 windows in a row, this means that the wall of the building needs to be mapped to the texture with this coordinates [0, 0, 6/64, 20/64]. Remember that <strong>UV values are normalized</strong>, i.e have values between 0 and 1, 0 pointing to the left/top pixel of the texture, and 1 to the right/bottom. </p>
<p>This way I can reuse the same material on every building regardless of it's size. It works even on buildings that are higher that 64 stories, because UV values are repeated - ex. an UV value of 1.2 is the same as 0.2. Perfect!</p>
<p>One little problem still persist though. Since each building uses the same material it also has the same pattern of lit and unlit windows. It doesn't look nice. Fortunately there was an easy solution for this: to offset the UVs of each building by a random value multiplied by 1/64. That way each building is mapped to a different area of the of the material and things look more natural.</p>
<p><strong>Effects</strong></p>
<p>To make the cityscape more interesting I added a Glow image effect to the camera. It adds a subtle glow around lit areas of the scene - in this case the windows, giving it a slightly blurry look.</p>
<p><strong>Source</strong></p>
<p>You can grab the package with the <a href="http://www.everyday3d.com/unity3d/city/city.unityPackage">source code here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/04/14/runtime-objects-with-unity3d/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>3 ways to draw 3D lines in Unity3D</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 11:53:03 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[3d lines]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[GL]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[scribbler]]></category>
		<category><![CDATA[unity3d pro]]></category>

		<guid isPermaLink="false">http://www.everydayflash.com/blog/?p=1044</guid>
		<description><![CDATA[Just as I was thinking about an interesting demo to play with drawing functions in Unity3D, Mrdoob published his Harmony drawing tool made with HTML5/Canvas. It looks really cool, so I though how about doing this in 3D? I only had to figure out how to draw lines. I did some research and below I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.everyday3d.com/unity3d/drawing/"><img src="http://www.everyday3d.com/unity3d/drawing/drawing.jpg" width="520" height="280" border="0"></a></p>
<p>Just as I was thinking about an interesting demo to play with drawing functions in <a href="http://www.unity3d.com">Unity3D</a>, <a href="http://www.mrdoob.com">Mrdoob</a> published his <a href="http://mrdoob.com/projects/harmony/">Harmony</a> drawing tool made with HTML5/Canvas. It looks really cool, so I though how about doing this in 3D? I only had to figure out how to draw lines.</p>
<p>I did some research and below I present <strong>3 different solutions</strong>. You can grab the source of the examples discussed below <a href="http://www.everydayflash.com/unity3d/drawing/lines3d.unitypackage">here</a>. </p>
<p><strong>Drawing lines with Line Renderer</strong> [<a href="http://www.everyday3d.com/unity3d/drawing/lr.html">demo</a>]</p>
<p>When it comes to lines, the first thing you'll bump into in the Unity3D API is the <a href="http://unity3d.com/support/documentation/ScriptReference/LineRenderer.html">Line Renderer</a> component. As the name suggests, it is used to draw lines so it seems the right tool for the job. Lines in this case are defined by 2 or more points (segments), a material and a width. </p>
<p>It has an important limitation: <strong>the line must be continuous</strong>. So if you need two lines, you need two renderers. The other problem is that the Line Renderer acts very strangely when new points are added dynamically. The width of the line does not seem to render correctly. It's either <strong>buggy or just wasn't designed for such use</strong>. Because of these limitations I had to create a separate Line Renderer for each tiny bit of line I'm drawing. </p>
<p>It was easy to implement, but not very fast since I end up spawning lots of GameObjects each with a LineRenderer attached. It seems to be the <strong>only option if you don't have Unity3D Pro</strong> though.</p>
<p><strong>Drawing lines as a mesh using Graphics</strong> [<a href="http://www.everyday3d.com/unity3d/drawing/gr.html">demo</a>]</p>
<p>The <a href="http://unity3d.com/support/documentation/ScriptReference/Graphics.html">Graphics</a> class allows to draw a mesh directly without the overhead of creating game objects and components to hold it. It <strong>runs much faster than Line Renderer</strong>, but you need to create the lines yourself. This is a bit more difficult but also gives you total control of the lines - their color, material, width and orientation. </p>
<p>Since meshes are composed of surfaces rather than lines or points, in 3D space a line is best rendered as a very thin quad. A quad is described with 4 vertices, and usually you'll only have the start and end points and a width. Based on this data you can compute a line like this:</p>
<div class="igBar"><span id="lcsharp-14"><a href="#" onclick="javascript:showPlainTxt('csharp-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-14">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 normal = Vector3.<span style="color: #0000FF;">Cross</span><span style="color: #000000;">&#40;</span>start, end<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 side = Vector3.<span style="color: #0000FF;">Cross</span><span style="color: #000000;">&#40;</span>normal, end-start<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">side.<span style="color: #0000FF;">Normalize</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 a = start + side * <span style="color: #000000;">&#40;</span>lineWidth / <span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 b = start + side * <span style="color: #000000;">&#40;</span>lineWidth / -<span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 c = end + side * <span style="color: #000000;">&#40;</span>lineWidth / <span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 d = end + side * <span style="color: #000000;">&#40;</span>lineWidth / -<span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>First, you get the normal of the plane on which both start and end vectors lie. This will be the plane on which the line-quad will located. The cross product of the normal and of the difference between end and start vectors gives you the side vector (the "thin" side of the quad). You need to normalize it to make it a unit vector. Finally calculate all 4 points of the rectangle by adding the side vector multiplied by half width to both start and end points in both directions. In the <a href="http://www.everydayflash.com/unity3d/drawing/lines3d.unitypackage">source code</a> all this happens in <code>MakeQuad</code> and <code>AddLine</code> methods, so take a look in there.</p>
<p>It wasn't easy to implement, but once I was there it runs pretty fast.</p>
<p><strong>Direct drawing with GL</strong> [<a href="http://www.everyday3d.com/unity3d/drawing/">demo</a>]</p>
<p><strong>No fast is fast enough!</strong> Instead of leaving this topic and live happily with the Graphics solution, I kept searching for something even better. And I found the <a href="http://unity3d.com/support/documentation/ScriptReference/GL.html">GL class</a>. GL is used to "issue rendering commands similar to OpenGL's immediate mode". This sounds like fast, doesn't it? It is! </p>
<p>Being much easier to implement that the Graphics solution it is a <strong>clear winner for me</strong>, the only drawback being that <strong>you don't have much control over the appearance of the lines</strong>. You can't set a width and perspective does not apply (i.e. lines that are far behind look exactly the same as those that are close to the camera). </p>
<p><strong>Conclusion</strong></p>
<p>For massive &#038; dynamic line drawing LineRenderer is not the best solution, but it is the only one available in Unity free version. It can surely be useful to draw limited amounts of static lines and this is probably what it was made for. If you do have Unity3D Pro, the solution with Graphics is reasonable and very flexible but if it is performance you're after <strong>choose GL.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Loading and manipulating images in Unity3D</title>
		<link>http://www.everyday3d.com/blog/index.php/2010/03/09/loading-and-manipulating-images-in-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2010/03/09/loading-and-manipulating-images-in-unity3d/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 06:52:39 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dynamic content]]></category>
		<category><![CDATA[loading images]]></category>
		<category><![CDATA[unity3d tutorial]]></category>
		<category><![CDATA[web applications]]></category>

		<guid isPermaLink="false">http://www.everydayflash.com/blog/?p=935</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="/unity3d/cubepict/?image=medusa.jpg"><img src="/unity3d/cubepict/imagedecomposer.jpg" width="520" height="280" border=0"></a></p>
<p>As <a href="http://www.everydayflash.com/blog/index.php/2010/03/08/unity3d-beyond-games/">I promised</a> here's the first article exploring <a href="http://www.unity3d.com">Unity</a> as a 3D web application tool. </p>
<p>Every web application nowadays is data driven, therefore the ability to dynamically load assets is crucial. Unity3D can load <strong>plain text, images, video and sound.</strong> Since loading plain text is not so interesting, <strong>I thought I'd see how it works with an image</strong>. </p>
<p>Here's a little experiment I called "<a href="/unity3d/cubepict/?image=medusa.jpg"><strong>Image Decomposer</strong></a>". It loads an image passed as parameter in the URL and creates a wall of little cubes which colors are based on the pixels of the image. When you rollover the cubes they fall down slowly decomposing it, hence the name.</p>
<p>The code is pretty simple, you can <a href="http://www.everyday3d.com/unity3d/cubepict/imageDecomposer.unityPackage">get the source here</a>. Below are a few things I found out while making it.</p>
<p><strong>URL parameters</strong></p>
<p>A very useful way to pass parameters to your swf files is to attach them as a <strong>query string</strong> in the embedding HTML, like this: <code>flash.swf?paramA=valueA&#038;paramB=valueB</code>. It's a Flash classic! </p>
<p>The good news is that is also works in Unity. Take a look a the <a href="view-source:http://www.everyday3d.com/unity3d/cubepict/?image=medusa.jpg">HTML source</a> of the demo to see how the image URL is passed to the unity3d file. Inside Unity3D it's very easy to get access to it through a static property called <code><a href="http://unity3d.com/support/documentation/ScriptReference/Application-srcValue.html">Application.srcValue</a></code>. It returns the whole path, including the file name and you need to parse the query string yourself. Here's a sample code in C#:</p>
<div class="igBar"><span id="lcsharp-19"><a href="#" onclick="javascript:showPlainTxt('csharp-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-19">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">string</span> su = Application.<span style="color: #0000FF;">srcValue</span>; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">string</span> qs = su.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>su.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">"?"</span><span style="color: #000000;">&#41;</span> + <span style="color: #FF0000;color:#800000;">1</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> deli = <span style="color: #808080;">"="</span>.<span style="color: #0000FF;">ToCharArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ps = qs.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span>deli<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The 'ps' array now contains pairs of key/values from the query string.</p>
<p><strong>Loading freedom</strong></p>
<p>Unity3D supports <strong>JPG and PNG format</strong>. Sadly GIFs are not supported, even animated... ;) The really nice thing is that <strong>there aren't any security restrictions</strong> when loading files from other domains. This is something that drove me crazy so many times with Flash! In Unity3D <strong>pass a valid URL and it will load the asset</strong>, it's so simple. If you don't believe me - just paste any link pointing to an image in the demo ("image" parameter in the URL) and see it for yourself.</p>
<p>I wouldn't expect things to be like that forever though, so use it while you can people!</p>
<p><strong>Coroutines and C#</strong></p>
<p>To deal with asynchronous events, like loading an external asset, Unity3D uses <strong>coroutines</strong>. They are a bit different from events in AS3, but not very much. If you are a fan of C# and want to use coroutines you may find the official documentation a bit unclear. So, first, read the official documentation <a href="http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html">here</a> and <a href="http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html">here</a>, and continue reading this post afterward. </p>
<p>What caught my attention is that to use the <strong>yield statement</strong>, the enclosing method must return <strong>IEnumerator</strong>. But all of the inherited methods in MonoBehavior, like Start(), Update() and the event handling ones return void. </p>
<p>Surprisingly, it turns out that you can just <strong>change the signature of any of those methods</strong> so that they return IEnumerator! If this isn't breaking OOP rules, I don't know what is. But, since it works, it's ok I guess. It's certainly not obvious. Otherwise it is possible to create a custom function that returns IEnumerator and use the StartCoroutine() method of MonoBehavior to run it. </p>
<p>Another thing is that with C# you need to write <code>yield return...</code> not just <code>yield...</code> The official docs being written in Javascript they tend to omit the <code>new</code> operator as well, and C# does not like that. So, if the docs say: </p>
<div class="igBar"><span id="ljavascript-20"><a href="#" onclick="javascript:showPlainTxt('javascript-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-20">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">yield WaitForSeconds<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>and you copy/paste this to a C# script, you need to edit this line and make it look like this: </p>
<div class="igBar"><span id="lcsharp-21"><a href="#" onclick="javascript:showPlainTxt('csharp-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-21">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">yield <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WaitForSeconds<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If you keep this in mind you will spare yourself some annoying compile errors.</p>
<p><strong>Scaling images (or why you don't need to do this)</strong></p>
<p>Once the image is loaded, it can be assigned to a <a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.html">Texture2D</a> object, which is the type to store bitmaps in Unity3D. From now on it can be used as a texture for any material. For example, you can create a plane and assign it as the main texture of the plane's material or use <a href="http://unity3d.com/support/documentation/ScriptReference/GUI.Label.html"><code>GUI.Label</code></a> to display the picture in 2D. But let's do something more interesting.</p>
<p>Texture2D has a few methods to extract information from the image, the most basic being <a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.GetPixel.html"><code>GetPixel()</code></a> and <a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.SetPixel.html"><code>SetPixel()</code></a>. They work exactly the same way as their equivalents in AS3 BitmapData class. The first allows to get the color of a pixel at a given coordinate, the other does the opposite.</p>
<p>When you start to play with these and you use images embedded in the project, make sure to <strong>select the "Is Readable" checkbox</strong> in the inspector. Images loaded dynamically are <strong>readable by default</strong>. </p>
<p>In the demo, no matter how big the loaded image is, the amount of cubes generated remains the same (it will adapt for different aspect ratios though). My first idea was to scale the image to something like <strong>50x50 pixels</strong>, and then using <code>getPixel()</code> to <strong>fetch the color for each cube</strong>. But I found out that <strong>there isn't any function to resize an image</strong> in the API. Sure, I could write my own function, but this can get quite complex as you can see <a href="http://forum.unity3d.com/viewtopic.php?t=25191&#038;highlight=resizing+texture2d">here</a>. Fortunately there is a better way.</p>
<p>Texture2D has a method called <a href="http://unity3d.com/support/documentation/ScriptReference/Texture2D.GetPixelBilinear.html"><code>GetPixelBilinear()</code></a>. It uses <a href="http://en.wikipedia.org/wiki/Bilinear_filtering">bilinear filtering</a> to evaluate the color of the image at a specific point defined by <strong>normalized coordinates</strong> (i.e. in 0-1 range). If the word "bilinear" sounds familiar to you it's because <strong>Photoshop also uses this algorithm</strong> to scale images. With this method the same code can be used to get pixel colors from images regardless of their size and aspect ratio: </p>
<div class="igBar"><span id="lcsharp-22"><a href="#" onclick="javascript:showPlainTxt('csharp-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-22">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Color c1 = image.<span style="color: #0000FF;">GetPixelBilinear</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">0</span>,<span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</span>; <span style="color: #008080; font-style: italic;">// always top-left color</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Color c2 = image.<span style="color: #0000FF;">GetPixelBilinear</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">1</span>,<span style="color: #FF0000;color:#800000;">1</span><span style="color: #000000;">&#41;</span>; <span style="color: #008080; font-style: italic;">// always bottom-right </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It's much more elegant (and faster) than scaling images! </p>
<p>As a side note: it would be also very easy to implement an actual resizing function using this method, in case you really need that. I'm sure that by now you can figure out how to do this :)</p>
<p><strong>What's next?</strong></p>
<p>The above example is simple but I hope it shows you how to get started with dynamic content. As far as images are concerned, much more is possible. By using <a href="http://unity3d.com/support/documentation/Manual/Shaders.html">custom shaders</a> and <a href="http://unity3d.com/support/documentation/ScriptReference/RenderTexture.html">render textures</a> you can apply <strong>filter effects to images (and video)</strong>. However, these features are available <strong>only in Unity3D Pro</strong> and besides, explaining it would make this post way too long. I'll do that in a separate article instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2010/03/09/loading-and-manipulating-images-in-unity3d/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Modifiers in Unity3D</title>
		<link>http://www.everyday3d.com/blog/index.php/2009/11/10/porting-actionscript-to-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2009/11/10/porting-actionscript-to-unity3d/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 13:04:03 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[as3dmod]]></category>

		<guid isPermaLink="false">http://www.everydayflash.com/blog/?p=438</guid>
		<description><![CDATA[Ringo from FlashBookmarks asked me if there were modifiers similar to AS3Dmod in Unity. I searched for something similar some time ago, but didn't find anything interesting. However, when I was starting C# scripting, I ported two of the modifiers from AS3Dmod - Bend and Twist. So I thought to share them with everyone. Don't [...]]]></description>
			<content:encoded><![CDATA[<p><img width="520" height="279" src="http://www.everydayflash.com/blog-assets/unityModifier.jpg"/></p>
<p>Ringo from <a href="http://www.flashbookmarks.com/">FlashBookmarks</a> asked me if there were modifiers similar to <strong>AS3Dmod</strong> in <a href="http://unity3d.com/">Unity</a>. I searched for something similar some time ago, but didn't find anything interesting. </p>
<p>However, when I was starting C# scripting, I ported two of the modifiers from AS3Dmod - <strong>Bend and Twist</strong>. So I thought to share them with everyone. Don't expect much, it's not the full library, just two classes. If I have some free time, I'll look into how to implement stacking, since for the moment you cannot apply two modifiers to the same object (the second one won't have any effect). </p>
<p>To play around with them grab <a href="http://www.everydayflash.com/unity3d/UnityModifiers.unitypackage">this package</a> and import it into Unity. Keep in mind that the modifiers work at runtime, so you won't see any effects in the editor until you run the game. If you want to animate them, just add another script that will have a reference to the modifier instance and change it's properties at each Update() call.</p>
<p>If you are interested in this kind of "bricolage" with 3D geometry, I recommend to take a look at the <strong><a href="http://unity3d.com/support/resources/example-projects/procedural-examples">Procedural Examples</a></strong> project provided by Unity. You will find there some highly interesting samples like <strong>dynamic extrudes or perlin noise</strong>. It's a great starting point to explore <strong>runtime geometry transformation</strong>. </p>
<p>Of course, take a look into the sources of the modifiers too! You'll see how simple it is to access the geometry at runtime and do some modifications. Basically it goes like this: </p>
<div class="igBar"><span id="lcsharp-24"><a href="#" onclick="javascript:showPlainTxt('csharp-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-24">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MeshFilter mfilter&nbsp; = GetComponent<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>MeshFilter<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> MeshFilter;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mesh = mfilter.<span style="color: #0000FF;">mesh</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> vs = mesh.<span style="color: #0000FF;">vertices</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> vc = vs.<span style="color: #0000FF;">Length</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i = <span style="color: #FF0000;color:#800000;">0</span>; i &lt;vc; i++<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #008080; font-style: italic;">// Modify your vertices here</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mesh.<span style="color: #0000FF;">vertices</span> = vs;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mesh.<span style="color: #0000FF;">RecalculateNormals</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Things worth notice: In Unity a <strong>vertex is just an instance of Vector3</strong> - there isn't any special class for this, as in Flash-based 3D engines. </p>
<p>You can see a bit of the casting-drama I need to do (lines 1-2) to get to the object holding the actual geometry information (Mesh). That's a bit annoying in C#, with JS that code would be more readable. Anyway, it's better to <strong>make that only once</strong>, in Start() and not at every Update(). </p>
<p>To get <strong>correct lightning effects</strong> on the materials, do not forget to call <strong>mesh.RecalculateNormals()</strong> after you've modified the positions of the vertices.</p>
<p>If you want to modify your mesh continuously (i.e. to animate it) it's necessary to keep the <strong>original array of vertices</strong>, because the code above overwrites the original positions. Take a look at the <a href="http://www.everydayflash.com/unity3d/UnityModifiers.unitypackage">source code in the package</a> to see how I did it. </p>
<p>That's all for now, hope you enjoy it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2009/11/10/porting-actionscript-to-unity3d/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Book review &#8220;Unity Game Development Essentials&#8221;</title>
		<link>http://www.everyday3d.com/blog/index.php/2009/10/16/unity-game-development-essentials/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2009/10/16/unity-game-development-essentials/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 19:39:19 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[News & Events]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[packt]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://www.everydayflash.com/blog/?p=631</guid>
		<description><![CDATA[I like books. A book is always a good thing, no matter how abundant online resources are. It's always at hand, with all the information brought together in one place, not scattered across different sites or even worst, across different forum threads. When I learned a couple of months ago that Will Goldstone was writing [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.packtpub.com/unity-game-development-essentials/book"><img width="520" height="243" src="http://www.everydayflash.com/unity3d/unityBook.jpg" alt="Unity Game Development Essentials" border="0"/></a></p>
<p><strong>I like books.</strong> A book is always a good thing, no matter how abundant online resources are. It's always at hand, with all the information brought together in one place, not scattered across different sites or even worst, across different forum threads.</p>
<p>When I learned a couple of months ago that <a href="http://willgoldstone.com/blog/?p=468">Will Goldstone</a> was writing a <a href="http://www.packtpub.com/unity-game-development-essentials/book">book on Unity3D</a>, I was pleasantly surprised. A bit later Packt, the publisher of the book, contacted me asking for a review. </p>
<p>In the meantime the book <a href="http://www.amazon.com/Unity-Game-Development-Essentials-Goldstone/dp/184719818X">hit the shelves</a>, and there was some buzz on Twitter, so there's a good chance you've already heard about it. If you are still wondering whether you should buy it, keep reading.</p>
<p><strong>Great for beginners</strong></p>
<p>The book is written in the form of one big tutorial. The author leads us through different parts of the Unity3D IDE in the course of creating a simple game. The word "simple" is key here. Before I had the chance to read the book I went through the <a href="http://unity3d.com/support/resources/tutorials/3d-platform-game">Unity3D official tutorial</a> which has a similar structure. It presents us with a very cool 3D artwork and a pretty complex game to build. Compared to that, what we will create with the book is pretty basic. <strong>However, in this case: simpler means better.</strong></p>
<p>After completing the official tutorial I felt I merely scratched the surface, and many parts of it were too complex to follow. The book doesn't leave you with this feeling. You'll be guided in creating a game but you will do it from scratch and all the steps are explained in depth. </p>
<p><strong>Scripting</strong></p>
<p>I assume most readers of this blog are familiar with programming. In this case you may find the code presented in the book somewhat rudimentary. One way of making it more fun is to <strong>translate the code from JavaScript into C#</strong> on the way. I did that, and I think it's much more beneficial than just copy-pasting. At least you will read it that way.</p>
<p>Furthermore, Actionscript 3 programmers can find that some examples are <strong>bending the rules of strict OOP</strong>. Again, for a seasoned AS3 developer, restructuring the code in those places can be another good exercise. </p>
<p>The book covers obviously more than just scripting. Among other topics, I particularly enjoyed the chapters on <strong>particle systems</strong> and on <strong>2D GUI</strong> (as you will find out, 2D GUI is the weird part of Unity) </p>
<p><strong>Other ressources</strong></p>
<p>An indispensable companion of the book is the Unity <a href="http://unity3d.com/support/documentation/ScriptReference/index.html">scripting reference</a>. The docs are solid and in most cases you'll find what you're looking for. Not always however, and if you feel you need more info I recommend to <a href="http://forum.unity3d.com/search.php">search the forums</a>. I am not a fan of forums overall, but I must admit that the Unity3D forum is a pretty good resource. </p>
<p><strong>A remark on the Unity3D documentation</strong></p>
<p>A bit off-topic, but I will take the occasion to rant about the Unity3D scripting documentation. Not about the content, but about the form.</p>
<p>In the Unity3D docs the list of classes is sometimes placed on the left column, sometimes in the middle. Sometimes they are listed in alphabetical order, sometimes presented in an inheritance structure. Every property and method is presented on a separate page, which is a big waste of space given the fact that most of them have max 2-3 lines of description. It leads to constant back/forward clicking when exploring the API. And it might get even worse when Unity will introduce namespace support.</p>
<p>It would be cool if the docs followed the good old <a href="http://java.sun.com/j2se/1.5.0/docs/api/">Java standard</a>, where the list of classes is always in the same place, always accessible and in alphabetical order. The properties and methods are listed in one big table, which is also much easier to browse. </p>
<p>OK, enough complaining, now back to the book...</p>
<p><strong>Conclusion</strong></p>
<p>If you want to get into Unity, it's probably a <strong>good idea to buy this book</strong>. You'll get yourself a decent introduction to the main aspects of the software. Remember that you won't learn any advanced stuff like stucturing code in large projects, writing custom shaders or making advanced physics simulations. <strong>The important thing is that the book explains all the basics leaving you well prepared to explore the rest</strong>. I enjoyed reading it and I learned quite a lot!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2009/10/16/unity-game-development-essentials/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Getting started with C# for Unity3D</title>
		<link>http://www.everyday3d.com/blog/index.php/2009/07/21/c-sharp-for-unity3d/</link>
		<comments>http://www.everyday3d.com/blog/index.php/2009/07/21/c-sharp-for-unity3d/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 19:34:56 +0000</pubDate>
		<dc:creator>bartek drozdz</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Unity3d]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.everydayflash.com/blog/?p=434</guid>
		<description><![CDATA[Unity3D offers a choice between 3 different programming languages: JavaScript, C# and Boo. Each of them has its pros and cons, but for me C# almost immediately came out as a clear winner. That is because it is fully object oriented and it's syntax is similar to Java and Actionscript 3, both of which I [...]]]></description>
			<content:encoded><![CDATA[<p><img width="520" height="240" src="http://www.everydayflash.com/unity3d/csharp.jpg" alt="C#" /></p>
<p><a href="http://unity3d.com/">Unity3D</a> offers a choice between 3 different programming languages:<strong> JavaScript, C# and <a href="http://boo.codehaus.org/">Boo</a></strong>. Each of them has its pros and cons, but for me C# almost immediately came out as a clear winner. That is because it is <strong>fully object oriented</strong> and it's syntax is similar to <strong>Java and Actionscript 3</strong>, both of which I am experienced with. However, before I started to play with Unity3D, I never wrote a single line in C#, so I had to learn it from scratch.</p>
<p>As you probably know C# was originally developed by <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx">Microsoft</a> and is widely used in the <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx">.NET framework</a> as well as in <a href="http://silverlight.net/">Silverlight</a> development. It is important to understand however that learning <strong>C# for Unity3D</strong> is not the same thing as learning the .NET platform. In fact you don't have to know anything about .NET to use C# with Unity3D. </p>
<p>While I use all kinds of online documentation, a book is often the best companion, so I thought buying a <strong>good C# book</strong> would be in order. It turns out that the small <a href="http://oreilly.com/catalog/9780596519223/">O'Reilly C# Pocket Guide</a> is exactly what one needs. It is absolutely unnecessary to buy a 500 page C# bible, as most of the stuff in those books is related to .NET and is not applicable to Unity3D at all. </p>
<p>Of course the book alone is not enough. You also need the <a href="http://unity3d.com/support/documentation/ScriptReference/">Unity3D scripting reference</a>. All the examples in the docs are given <strong>in Javascript</strong> only. But don't worry, they are easy to translate once you get used to.</p>
<p>C# is a very <strong>elegant and powerful</strong> language. It's dot-syntax is based on Java, so if you come from a Java background you'll quickly feel comfortable with it. There are a few differences though. The most striking difference is the convention to start property and method names with an uppercase letter. I guess it comes from Visual Basic. I am not a fan of this convention, but it's better to use it and have a consistent code rather than fight it. </p>
<p>The list of all C# features is so long, that it makes AS3 look poor in comparison. C# is really like <strong>Actionscript on steroids</strong>. I suppose the next version of Actionscript will have at least some of those implemented, so it's good to know about them. Here's a list of my favorites.</p>
<p><strong>Operator overloading</strong></p>
<p>This is the coolest one by far! It allows to define custom actions for common operators like +, -, * or /. The best illustration of this feature comes with vector addition. In Actionscript, to get a sum of 2 vectors you need to write something like this:</p>
<div class="igBar"><span id="lactionscript-31"><a href="#" onclick="javascript:showPlainTxt('actionscript-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-31">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> c:Vector3D = a.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is not very nice, and becomes almost unreadable if more than two vectors are added. In C#, thanks to the <strong>overloaded + operator</strong> it looks like this:</p>
<div class="igBar"><span id="lcsharp-32"><a href="#" onclick="javascript:showPlainTxt('csharp-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-32">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Vector3 c = a + b; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is much more readable and elegant, isn't it? It goes without saying that the 3D vector implementation in Unity3D has all the operators overloaded. In case you want to do it yourself however, the implementation of operator overloading is very simple and in the above case could look something like this:</p>
<div class="igBar"><span id="lcsharp-33"><a href="#" onclick="javascript:showPlainTxt('csharp-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-33">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Vector3 <span style="color: #0600FF;">operator</span> + <span style="color: #000000;">&#40;</span>a:Vector3, b:Vector3<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Vector3<span style="color: #000000;">&#40;</span>a.<span style="color: #0000FF;">x</span> + b.<span style="color: #0000FF;">x</span>, a.<span style="color: #0000FF;">y</span> + b.<span style="color: #0000FF;">y</span>, a.<span style="color: #0000FF;">z</span> + b.<span style="color: #0000FF;">z</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>There are some simple rules: the operator function must always be static and must return an object of type it is defined for. However it can take any type of parameters. This allows to define operator overloading for adding or multiplying vectors but also for multiplying vector by a scalar or multiplying vectors by matrices. In fact this can be achieved thanks to another C# feature - method overloading.</p>
<p><strong>Method overloading</strong></p>
<p>Method overloading allows to define multiple methods with the same name provided that they take different parameters. It works with regular methods and with constructors as well. If there a multiple ways to create an object you can define multiple constructors to satisfy each case. As mentioned above it works with operators too.</p>
<p>A good example of method overloading in Unity3D API is the 'Transform.Rotate()' method. It has has been overloaded 3 times with different ways to specify a rotation:</p>
<div class="igBar"><span id="lcsharp-34"><a href="#" onclick="javascript:showPlainTxt('csharp-34'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-34">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> Rotate <span style="color: #000000;">&#40;</span>Vector3 eulerAngles, Space relativeTo<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> Rotate <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">float</span> xAngle, <span style="color: #FF0000;">float</span> yAngle, <span style="color: #FF0000;">float</span> zAngle, Space relativeTo<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> Rotate <span style="color: #000000;">&#40;</span>Vector3 axis, <span style="color: #FF0000;">float</span> angle, Space relativeTo<span style="color: #000000;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You can either pass a vector of angle values, three floats - one for each axis, or a single angle combined with a rotation axis. This is only logical to call it 'Rotate' every time because this is what the method does at the end. </p>
<p>Actionscript has default parameter values, which in some cases can simulate method overloading but they are not quite as flexible as that.</p>
<p><strong>Getter/setter syntax</strong></p>
<p>C# has a very concise way of declaring parameters. While in Actionscript it takes two functions - one for 'get' and one for 'set', in C# it can look like this:</p>
<div class="igBar"><span id="lcsharp-35"><a href="#" onclick="javascript:showPlainTxt('csharp-35'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-35">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">float</span> Size <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>In this case the compiler automatically adds a private member for this property and there isn't anything else you need to do. Of course such a parameter is not very practical, so here's a more complex code, involving a public getter and private setter:</p>
<div class="igBar"><span id="lcsharp-36"><a href="#" onclick="javascript:showPlainTxt('csharp-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-36">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">float</span> Area <span style="color: #000000;">&#123;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> size * size; <span style="color: #000000;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> set <span style="color: #000000;">&#123;</span> size = Mathf.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You will also notice that neither the 'function' nor the 'var' keywords do not exist in C#. In that C# closely follows Java. This is good because both these keywords are completely redundant and do not add any information to the code.</p>
<p>Among other interesting features I recommend to explore <strong>indexers, generic types, structures and enumerators</strong> all of which are covered in depth in the pocket guide mentioned above but also in the many online C# tutorials.</p>
<p><strong>Editing C#</strong></p>
<p>There are multiple editors that support C#. Microsoft offers a great and free editor called <a href="http://www.microsoft.com/express/vcsharp/">Visual C# Express Edition</a> which features code completion, but is Windows only (obviously). For a Mac there's <a href="http://monodevelop.com/">Mono Develop</a> which looks very powerful, but is not very stable yet. Unfortunately the <a href="http://www.improve-technologies.com/alpha/esharp/">C# plugin</a> for Eclipse is completely unstable for the moment, and I wouldn't recommend it. On the other hand, it is still in alpha so let's give it some time. Of course there's always <a href="http://macromates.com/">TextMate</a>. </p>
<p>Last but not least there's <strong>Unitron</strong> - the Unity3D default editor. It is not very popular among developers, and I saw people complaining about it being too basic. I don't share this opinion. While it ain't no <a href="http://fdt.powerflasher.com/">FDT</a>, it's pretty solid, very stable and has <strong>basic auto-completion</strong>. The trick is auto-completion is turned off by default (why?). To turn it on go to 'Preferences' and you'll find it at the bottom of the 'General' section. With that, I'm sure that Unitron is enough to get started.</p>
<p>Most of us know JavaScript from the <strong>browser environment</strong>. JavaScript is often the first programming language people ever used. If this is the case, you may be tempted to just use JS with Unity3D right away and not bother learning anything new. After all, as far as Unity3D is concerned, anything that can be done with C# <strong>can also be done with JavaScript</strong>. </p>
<p>However in the long term, C# offers better control over the code, full OOP support, strong typing and a lot satisfaction of mastering a robust programming language, so the extra effort is worth it. Plus, for a seasoned AS3 developer, mastering C# is a matter of a couple of weeks anyway, so go ahead and try it out!</p>
<p><strong>UPDATE March 2010. </strong>Here's a new post about C# and Unity3D:<br />
<a href="http://www.everydayflash.com/blog/index.php/2010/03/09/loading-and-manipulating-images-in-unity3d/">Loading and manipulating images in Unity3D</a><br />
<em>(more coming)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.everyday3d.com/blog/index.php/2009/07/21/c-sharp-for-unity3d/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>

