<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace V5 Site Server v5.13.159 (http://www.squarespace.com) on Sun, 26 May 2013 01:24:59 GMT--><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Friction Point Blog</title><link>http://www.frictionpointstudios.com/blog/</link><description></description><lastBuildDate>Wed, 05 Dec 2012 12:14:18 +0000</lastBuildDate><copyright></copyright><language>en-AU</language><generator>Squarespace V5 Site Server v5.13.159 (http://www.squarespace.com)</generator><item><title>A genuine passion for C# .Net development</title><category>C#</category><dc:creator>Sam Cox</dc:creator><pubDate>Wed, 05 Dec 2012 12:03:16 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2012/12/5/a-genuine-passion-for-c-net-development.html</link><guid isPermaLink="false">512318:5867401:31684020</guid><description><![CDATA[<p>My latest contract dev contract is coming to an end so I've been looking around for something for next year. The recruitment company that does my payroll stuff mentioned that there was some potential work going at a company and sent me their 'Developer Questionnaire' to fill out.</p>
<p>It's amazing how long these end up taking, but I thought I'd share part of my answer to the following question.</p>
<p>Q: [Give details/examples etc of] A genuine passion for C#.Net development.</p>
<p>A: <span style="color: black;" lang="EN">My wife has been using an iPhone app called Baby Tracker to keep track of my newborn son&rsquo;s activities. Unfortunately the app has limited display options and no export functionality. I worked out that the app uses an unencrypted SQLite database to store its data. I am currently writing a .Net 4 WPF app using MVVM to display the domain data extracted using Fluent NHibernate in Visual Studio 2012. </span></p>
<p>Google tells me that lots of other people are trying to get data out of this app. If it turns out to be any good I'll OSS it and see if anyone wants to use it.</p>
<p><span style="color: black;" lang="EN"><br /></span></p>
<p>&nbsp;</p><p><br/><br/></p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-31684020.xml</wfw:commentRss></item><item><title>MonoBehaviour Magic – using C# Namespaces and Inheritance in Unity</title><category>C#</category><category>Unity</category><dc:creator>Sam Cox</dc:creator><pubDate>Sat, 18 Jun 2011 05:25:46 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/6/18/monobehaviour-magic-using-c-namespaces-and-inheritance-in-un.html</link><guid isPermaLink="false">512318:5867401:11829980</guid><description><![CDATA[<p class="western" style="margin-bottom: 0cm;">A discussion on Unity's workhorse class, MonoBehaviour and ways to use it in C#.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<h3 class="western" style="margin-bottom: 0cm;">What is MonoBehaviour?</h3>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">MonoBehaviour is the class from which all scripts attached to objects that exist in a scene derive from. What seems strange to many seasoned (non-Unity) programmers when they first encounter this beast is that all its methods seem to be missing. MonoBehaviour inherits from Behaviour which inherits from Component then from Object. None of these have the methods we're used to seeing, namely Awake(), Start() and OnGUI().</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">These are magic methods, summoned directly from the dark Unity overlord. Or, more likely, via reflection. Unity checks if your class contains any of these methods and will call them when appropriate. In the interests of clarity, from now on I'll call these methods 'Overridable  Methods' as the Unity documentation does.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">So back to the thing that irks purist programmers; Unity is using reflection here, so it skips the usual  processes used to allow or exclude other classes from accessing methods. As you can see on the default class created by Unity.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">
    void Awake()
    {
    }
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">This methods is private, so nothing outside the class should be able to call it. But obviously stuff can otherwise nothing would work.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">One way to think of it is the following.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">public class MonoBehaviour
{
    public virtual void Awake()
    {
    }
}

public class MyMonoBehaviour : MonoBehaviour
{
    public override void Awake()
    {
    }
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Then imagine the Unity Engine doing something like this</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">foreach (MonoBehaviour baseClass in listOfAllMonobahaviours)
{
     baseClass.Awake();
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">If you start of just using the default Unity created classes, it may take you a while to realise how many MonoBehaviour overrideable methods there actually are. At latest count there are 49 'Overridable Functions' that Unity uses. A full list of them is <a href="http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html">here</a>.&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Now, don't let the fact that Unity magics up these methods for you discourage you from programming in a traditional manner. That is, you can still utilise inheritance in the manner that you might like to. The thing to remember is that the Unity engine is the only thing that will call these methods in an improper way, everything we do manually will work as expected.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">For example, the classic 'new' vs 'override' issue.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">public class BaseClass
{
    public void DoSomething()
    {
    }
}

public class DerivedClass : BaseClass
{
    public new void DoSomething()
    {
    }
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Which method gets called depends on what the type is when we call it. I.e.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">        DerivedClass newClass = new DerivedClass();

        newClass.DoSomething();
        (newClass as BaseClass).DoSomething(); 
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">...will call different methods. But in Unity, it doesn't matter what modifiers you add to your MonoBehaviour overridable methods, Unity will treat them as if you used</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">    public override void Awake()
    {
    }
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">...and call the Awake() method of the highest derived class.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Manually though, it all works as expected. You can hide and override to your heart's content for these overridable methods, but just accept that whatever you do, Unity doesn't care and will still treat method as having been declared as an override (even if it's private). And note that you can manually call all these methods, and if you do they will be treated exactly as you would expect in non-Unity programming. But it's probably best not to for the sake of clarity.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">An exception to that rule however might be if you have a derived class and you want to call an overridable method in the base class. I.e. In the Awake() method of the derived class you manually call base.Awake(). Since the base class's implementation of Awake will never be call by Unity (since it will always be overridden) this seems ok.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<h3 class="western" style="margin-bottom: 0cm;">Execution Order</h3>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">However, it's worth noting that the execution order of the overridable methods is random. You cannot guarantee the order in which they are called. If this is a problem for you, I'd recommend just creating your own update method, MyUpdate() and then have a controlling class call them manually.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">public class MyMonoBehaviour : MonoBehaviour
{
    public void MyUpdate()
    {
    }
}


public class MyController : MonoBehaviour
{
    void Update()
    {
        foreach (MyMonoBehaviour myMonoBehaviour in this.listOfAllMyMonobehaviour)
        {
            myMonoBehaviour.MyUpdate();
        }
    }
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<h3 class="western" style="margin-bottom: 0cm;">Namespaces and Inheritance</h3>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">One of the other things that traditional programmers might notice is the lack of namespaces. Well, that's because Unity likes to pretend that it doesn't use namespaces, but it can.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">The basic rule is that you can't put any MonoBehaviour derived class inside an explicit namespace, but that shouldn't stop you from using them completely.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Now if you do put a MonoBehaviour script inside a namespace you'll get this compiler warning. 
&ldquo;The class defined in script file named 'MyScript' does not match the file name!&rdquo; And none of the MonoBehaviour overridable methods (Start, Update) will be called because Unity will treat it as a normal class.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">So what, you still shouldn't use namespaces right? Well, the documentation say no, but they are just so useful that I reckon you should anyway.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Having to give everything a unique class name for a project of any size is annoying to say the least. Now, for the MonoBehaviour classes we're screwed, but for everything else, go nuts.  Now I tend to section off as much common code as I can into static libraries or helper classes, on top of using inheritance to concentrate common code in base classes.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">For a simple example.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Create separate Constant classes for each level. Note: You can stick all these into the one file if you want. The requirements for Class name to match File name is only for MonoBehaviour classes.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">namespace Level01 
{
	public class Constants
	{
		public const float Speed = 0.1f;
	}
}

namespace Level02
{
	public class Constants
	{
		public const float Speed = 0.3f;
	}
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Create a Player base class and an abstract member (so it can be used within the class).</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">public abstract class PlayerBase : MonoBehaviour
{
	// Common code for Player

	public abstract float Speed {get;}

	void Update()
    	{
       		this.transform.position += new Vector3(0.0f, 0.0f, this.Speed * Time.deltaTime);
    	}
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Then we create separate Player classes specific to each level. Each will import the required namespace and thus get access to the Constants for that level..</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<pre class="brush: csharp">using Level01;

public class Level01Player : PlayerBase
{
	// Level 01 specific code for Player

	public override float Speed
	{
		get {return Constants.Speed;}
	}
}


using Level02;

public class Level02Player : PlayerBase
{
    // Level 01 specific code for Player

    public override float Speed
    {
        get { return Constants.Speed; }
    }
}
</pre>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Note: Each of these is in a separate file.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">Maybe a bit of a contrived example, but I'm just trying to show that you shouldn't throw away the traditional programming tools when you start working with Unity.</p>
<p class="western" style="margin-bottom: 0cm;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm; font-style: normal;">Note that I didn't have to make the base class abstract. Another option would have been to make the Speed property virtual and have the derived classes override it. But since we have no intention of actually using PlayerBase on a Unity object directly it makes sense to ensure it can never be used (Unity will spit it if you attach an abstract class to an object).</p>
<p class="western" style="margin-bottom: 0cm; font-style: normal;">&nbsp;</p>
<h3 class="western" style="margin-bottom: 0cm; font-style: normal;">Performance</h3>
<p class="western" style="margin-bottom: 0cm; font-style: normal;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm; font-style: normal;">Finally a little word on performance. After I realised that I didn't have to use MonoBehaviours for everything, I started wondering about their performance overhead. Perhaps I should be avoiding them at all cost? Well it turns out that whatever overhead there is is almost negligible, at least according to my quick and dirty testing.</p>
<p class="western" style="margin-bottom: 0cm; font-style: normal;">&nbsp;</p>
<p class="western" style="margin-bottom: 0cm;">I created a scene with 1000 objects with a MonoBehaviour script on them and a task to be performed in their Update() methods, then another scene with only one MonoBehaviour class and an Update() method that did the same task 1000 time. I ran them both and measure how many times Update was called. The result? Pretty much the same. Now I'm not conclusively saying that there is no performance gain to be had by minimising MonoBehaviours, but in my opinion there are hundreds of performance tweaks you should look at first before considering it.</p><p><br/></p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-11829980.xml</wfw:commentRss></item><item><title>Maze Rover Prototype</title><category>Game-Dev</category><category>Games</category><category>MazeRover</category><category>Unity</category><dc:creator>Admin</dc:creator><pubDate>Mon, 30 May 2011 04:40:27 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/5/30/maze-rover-prototype.html</link><guid isPermaLink="false">512318:5867401:11618133</guid><description><![CDATA[<p>After working on a number of titles for various clients, I've decided to launch into releasing my own game. The current working title is <a href="http://www.frictionpointstudios.com/maze-rover-test/">Maze Rover</a>, and I've just set up a page <a href="http://www.frictionpointstudios.com/maze-rover-test/">here</a> to show how the prototype is developing and get some feedback on the gameplay.</p>
<p>There is more info about what I'm trying to achieve on the other page, but I thought I'd talk a little here about what new and cool things I've learnt in the process.</p>
<h2>Super fast serialization/deserialization</h2>
<p>&nbsp;</p>
<p>Maze rover is, as the title suggests, a maze game. Each level is a different maze. Alongside my Unity project, I've also had to make a level editor in order to draw the mazes. For this, I used pure C#, WPF, nothing to do with Unity.&nbsp;</p>
<p>Here is a screenshot of the, incredibly ugly, level edit in action.</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frictionpointstudios.com/storage/maze-rover-files/LevelEditorScreenshot.jpg?__SQUARESPACE_CACHEVERSION=1306730967837" alt="" /></span></span></p>
<p>&nbsp;I created a C# library, that both the level editor and the game would use, that contained my maze classes. Square, Bonus, Dimensions etc. The next question is how to get the info out of the editor and into the game.</p>
<p>What I'm talking about here is serialization/deserialization. I've already done a <a href="http://www.frictionpointstudios.com/blog/2011/3/31/using-protobuf-net-serialization-in-unity-iphone.html">post</a> on my final solution, protobuf-net, but it's interesting to document what extra technology I had to learn to get this to work properly.</p>
<p>The max size of my levels at the moment is 48 x 32 squares, or 1536 square. The way I've set it up, there is a maze container with a 2 dimensional array containing all the squares. My initial use of XML serialization wasn't very promising, with it taking hundreds of ms to deserialize in the game. With protobuf I think I'm averaging 10-15ms. Great improvement.</p>
<h2>Dynamic Mesh Creation</h2>
<p>&nbsp;</p>
<p>After reading in the maze for a particular level, the next step is to create the maze. I initially started with creating pre-fabs of each type of 'square' and instantiating them one after another. But, as I suspected and is pretty obvious, this is not very performance friendly. Even if they are just simple squares, having 1000+ objects in a scene is just scary.</p>
<p>So, meshes. I didn't know much are creating meshes before I started this. But I looked at a few examples and realised that with a bit of maths, they are not too hard to create.</p>
<p>Here is a screenshot of the maze mesh.</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frictionpointstudios.com/storage/maze-rover-files/MeshView.jpg?__SQUARESPACE_CACHEVERSION=1306731877685" alt="" /></span></span></p>
<p>Pretty simple, but dramatically more efficient that previously. Although I create the meshes using squares, they only contain the faces I need and each face is UV mapped as appropriate. In the pic above that is not shadows making the sides seem darker than the tops, it's just a darker texture. In fact, I'm not using any lights at all at the moment.</p>
<h2>UIToolkit</h2>
<p>&nbsp;</p>
<p>For an in-game HUD solution I was initially going to use GUISpriteUI, which was created some time ago by Prime31, but when I went to check on progress he had released a whole new solution, <a href="http://forum.unity3d.com/threads/87917-Prime31-UIToolkit-Multi-Resolution-GUI-Solution-Ready-for-Use...and-it-s-free">UIToolkit</a>.</p>
<p>Users of GUISpriteUI will understand how much better the new system is. No more mucking around with UvRects and ruining everything when your atlas is changed. UIToolkit uses TexturePackerGUI to do the work for you and, most importantly, now supports fonts.</p>
<p>So, long story short, 1 draw call now does all my HUD. In the pic below there are 13 HUD elements (not counting the FPS counter) that are all done in the one call. FYI - Crappy graphics are placeholder, don't freak out ;-)</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frictionpointstudios.com/storage/maze-rover-files/HUDtest.jpg?__SQUARESPACE_CACHEVERSION=1306732540594" alt="" /></span></span></p>
<p>If you've previously used GUISpriteUI and got frustrated by certain aspect, I highly recommend checking out UIToolkit. Things are so much easier and bug fixes and improvements are being added regularly. It's (kinda) open source, with Prime31 giving out GIT access to certain posters on Unity forums.</p>
<h2>Stay Tuned</h2>
<p>&nbsp;</p>
<p>I will hopefully get some good feedback from releasing this prototype and it will all go in to making the game even better. Once I get the theme nailed down, I'll be looking around for artists to create the assets I need.</p>
<p>Hopefully we're looking at a release in the next few months.</p>
<p>&nbsp;</p>
<p>&nbsp;</p><p><br/></p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-11618133.xml</wfw:commentRss></item><item><title>iOS versioning: iTunes made me an offer I couldn't refuse</title><category>Apple</category><category>Xcode</category><category>iOS</category><dc:creator>Sam Cox</dc:creator><pubDate>Tue, 19 Apr 2011 00:01:50 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/4/19/ios-versioning-itunes-made-me-an-offer-i-couldnt-refuse.html</link><guid isPermaLink="false">512318:5867401:11196802</guid><description><![CDATA[<p>I know that getting angry at Apple's iOS/Xcode/iTunes dance is nothing new for an iPhone developer, but I just ran into a problem that I thought I would share the 'solution' to.</p>
<p>NOTE: If you Googled this and are looking for a way to downgrade your iPhone <em>while </em>keeping all your data then this is not it.</p>
<p>Now, iOS is Apple's baby, and they are very protective of it. You will upgrade it as soon as possible, and if you think about downgrading it then you are a terrorist who has less right to live on God's green earth than a weasel.</p>
<p>This works fine for 99.9% of people with an iOS device. You plug your phone into iTunes and it installs the new iOS version for you and all is good. But for a developer it's not that simple because if you upgrade your iOS version then you also have to upgrade Xcode. This is a little bit of a problem for me because...</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frictionpointstudios.com/storage/images/xcode%20download.png?__SQUARESPACE_CACHEVERSION=1303171871102" alt="" /></span></span></p>
<p>It's a 4.6 GB download! Because I live about 3km from my nearest exchange (and 6km from the CBD of a city with 1.2 million people!?!) my internet speed is crap. So when I sit down to start work for the day I can't really just quickly download Xcode and get to work. I have to put it on overnight, and then I forget to, then I'm back in the same position.</p>
<p>So, simple solution is to not upgrade until you have everything downloaded. Fine.</p>
<p>But, today I hit a problem where I want to restore my iPhone. Restore it to its current iOS version (not the latest). iTunes wont let you do that because it will, by default, restore the phone to the latest version.</p>
<p>But but, there is a trick. If you hold shift and press the Restore button you can choose which firmware version to install.</p>
<p>Magic, thank you apple. There is just one problem with this; iTunes <strong>automatically deletes </strong>old versions of the firmware, presumably to stop you doing what I'm trying to do.</p>
<p>So Google will helpfully show you the location where iTunes stores the files. On windows 7 it's in</p>
<blockquote>
<p>C:\Users\USERNAME\AppData\Roaming\Apple Computer\iTunes\iPhone Software Updates</p>
</blockquote>
<p>But you can look in there all you want because if there is a newer version of iOS out there, it will be empty.</p>
<p>Windows 7 is not helping me much here either, because a quick search of my whole system using Windows 7 new super duper 'I'll search but not everywhere and hide the options' method showed no IPSW files. That's the extension by the way, iOS firmware files look like this.</p>
<blockquote>
<p>iPhone2,1_4.3.1_8G4_Restore.ipsw</p>
</blockquote>
<p>So <strong>before </strong>I had downloaded the new iOS version, iTunes had helpfully deleted the old firmware, effectively forcing me to upgrate, or preventing me from using the device I own depending on your viewpoint.</p>
<p>But, before I gave up I fired up a DOS prompt and did a proper search of my system.</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frictionpointstudios.com/storage/images/cmd.png?__SQUARESPACE_CACHEVERSION=1303173037501" alt="" /></span></span></p>
<p>Lo and behold, there were my IPSW files. Lucky I am slack and never empty my recycle bin. So I went and looked in the recycle bin and there were my files (they actually show up with proper names when you look through the recycle bin). So I restored the one I wanted (iOS 4.3.1) and fired up iTunes...</p>
<p>Which then deleted it again of course. (I think it only deletes it when you hit Restore actually, but whatever...)</p>
<p>So I restored it again, <strong>copied it somewhere else</strong> then fired up iTunes again.</p>
<p>Then I was finally able to restore my iPhone to its current version. Of course, this wipes everything on the phone, which was fine for me because I'm just using it as a test device and don't care. A while ago I actually tried to downgrade my phone <em>while</em> keeping it's contents and I gave up. I wasn't sure if I would be able to restore my backup of all my contents after restoring the firmware since the backup would be a later version of iOS. I never tried it but I highly suspect it wouldn't work.</p>
<p>Old iOS versions are purged from the earth according to Apple. Wiped  from history. You can't even find a link to old iOS firmware files on  their website. The files are there of course, there is just no link to  them. <a href="http://www.felixbruns.de/iPod/firmware/">This </a>site allows your to download old versions, with direct links to the Apple website and yet there is no information on the iOS <strong>Developer </strong>Center about where to get them.</p>
<p>I understand Apple's motivation for pushing their users to the latest version, but why does it have to be so impossible to do things slightly differently? Will the world come to an end if you downgrade the iOS version?</p>
<p>And we're developers for Pete's sake, sometimes there is a valid reason for having 6 devices all with different versions. I could understand iTunes doing what it's doing if there was a 'Firmware Control Panel' or something in Xcode that allowed us developers to do what we wanted. But there's not.</p>
<p>Crazy stuff Apple.</p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-11196802.xml</wfw:commentRss></item><item><title>Major update to Spartan Athletics approved</title><category>Game-Dev</category><category>Unity</category><category>Xcode</category><category>iOS</category><dc:creator>Sam Cox</dc:creator><pubDate>Wed, 06 Apr 2011 23:05:10 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/4/7/major-update-to-spartan-athletics-approved.html</link><guid isPermaLink="false">512318:5867401:11075465</guid><description><![CDATA[<p>After a bit of a marathon effort, version 1.1 of <a href="http://itunes.apple.com/au/app/spartan-athletics/id360074313">Spartan Athletics</a> is now available in the app store.</p>
<p>Now, I'm just the coder for this project, but if I was the owner I'd have called it version 2.0 at least, because a lot has changed since the original version.</p>
<p>It now has all ten events available, as well as bluetooth multiplayer support and a whole heap of improvements and bug fixes.</p>
<p>But getting it approved was not easy. A while ago I updated my 3GS to iOS 4.3 without thinking, and of course that meant that I had to update XCode to 4.0 to be able to build to device. Only then did I realise that perhaps this wasn't the smartest move after looking at the growing number of threads about the problems people were having.</p>
<p>One major problem was that the plug-in I used for bluetooth was definitely not compatible with Xcode 4.0. I used <a href="http://www.prime31.com/unity/">Prime31</a>'s 'GameKit Bluetooth/WiFi and Voice Chat' plugin. That runs some magic little scripts when you build in Unity to set up the Xcode project and they were broken. But, due to the awesomeness of Mr Prime31's service, after a quick message on twitter I was sent a beta of a 4.0 compatible version of the plugin.</p>
<p>So all seemed good, and the plethora of ad-hoc builds I'd done using Xcode 4.0 all worked fine, on iOS from 4.2.1 to 4.3.1 and iPod touch, iPad and iPhone. So we submit it to Apple and pretty soon get a rejection letter.</p>
<blockquote>
<p><span class="Apple-style-span" style="border-collapse: collapse; color: #262626; font-family: 'Lucida Grande',Geneva,Verdana,Arial; font-size: 12px; line-height: 16px;"><strong><em>We found that your app failed to launch on iPhone 4 running iOS 4.3.1.<br /><br />We encountered the issue when selecting the application on the Home screen - the app displayed a launch image then quit unexpectedly. This may be because iOS 4 uses a watchdog timer for applications; if an application takes too long to complete its initial startup, the operating system terminates the application.&nbsp;<br /><br />For information about the watchdog timer, please see&nbsp;</em></strong><a style="color: #4673cb; text-decoration: none;" href="http://developer.apple.com/library/ios/qa/qa2009/qa1592.html" target="_blank"><strong><em>Technical Q&amp;A: Application does not crash when launched from debugger but crashes when launched by user.</em></strong></a><strong><em>.<br /><br />Another possibility could be a missing entitlement. For more information, please see the&nbsp;</em></strong><a style="color: #4673cb; text-decoration: none;" href="http://developer.apple.com/library/ios/technotes/tn2009/tn2242.html" target="_blank"><strong><em>Technical Note: Resolving "0x800003A", applications not launching and "missing entitlement"</em></strong></a><strong><em>.<br /><br />For discrete code-level questions, you may wish to consult with&nbsp;</em></strong><a style="color: #4673cb; text-decoration: none;" href="http://developer.apple.com/support/resources/technicalsupport/" target="_blank"><strong><em>Apple Developer Technical Support</em></strong></a><strong><em>. Depending on your questions, be sure to include any symbolicated crash logs, screenshots, or steps to reproduce the issues you&rsquo;ve encountered.<br /><br />To appeal this review, please submit a request to the&nbsp;</em></strong><a style="color: #4673cb; text-decoration: none;" href="http://developer.apple.com/appstore/resources/approval/contact.html" target="_blank"><strong><em>App Review Board</em></strong></a></span></p>
</blockquote>
<p><span class="Apple-style-span" style="border-collapse: collapse; color: #262626; font-family: 'Lucida Grande',Geneva,Verdana,Arial; font-size: 12px; line-height: 16px;">I had no idea what was wrong, and the documents that Apple linked to were not very helpful. But, I figured that maybe I had messed up in the signing process and used the wrong distribution profile or something. So I re-did it carefully and tried again. Nup, still rejected.</span></p>
<p><span class="Apple-style-span" style="border-collapse: collapse; color: #262626; font-family: 'Lucida Grande',Geneva,Verdana,Arial; font-size: 12px; line-height: 16px;">Then we finally found this <a href="http://forum.unity3d.com/threads/83076-iOS-4.3-App-Rejected">thread</a> on the Unity forums. Luckily I still had the Xcode 3.2 installer. So I installed that and re-build in Unity with the target SDK set to 4.2 and re-submitted. </span></p>
<p><span class="Apple-style-span" style="border-collapse: collapse; color: #262626; font-family: 'Lucida Grande',Geneva,Verdana,Arial; font-size: 12px; line-height: 16px;">Success. </span></p>
<p>Bloody hell Apple, you sure don't make it easy for us developers. Between the provisioning profile clusterf*ck, the insanely diffucult iOS downgrade process and the almost five gig Xcode downloads for even miniscule version updates, the world of Android development is looking better and better.&nbsp;</p>
<p>But you're still where the money is... and that's why we love you.</p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-11075465.xml</wfw:commentRss></item><item><title>Using protobuf-net serialization in Unity iPhone</title><category>C#</category><category>Unity</category><category>protobuf-net</category><category>serialization</category><dc:creator>Sam Cox</dc:creator><pubDate>Wed, 30 Mar 2011 22:46:35 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/3/31/using-protobuf-net-serialization-in-unity-iphone.html</link><guid isPermaLink="false">512318:5867401:11000658</guid><description><![CDATA[<p>If performance is an issue for your serialization in a Unity iPhone project, this post shows you how to use the protobuf-net library.</p>
<h3>Some Background</h3>
<p>As part of a new game project I'm working on, I had to store level information in a file and read it in for each new level. I had created a simple level editor as a windows WPF C# project and then I would serialize my level model class onto the file system then include it in my Unity project to be loaded in during runtime.</p>
<p>I initially used simple XML serialization, but as the levels increased in complexity it was taking longer and longer (well, 100s of miliseconds, but every bit counts ;-) to deserialize. So, off to the web to find the fastest and easiest serialization. The best candidate was Marc Gravell's <a href="http://code.google.com/p/protobuf-net/">protobuf-net</a>, which provides 'Fast, portable, binary serialization for .Net' using Google's protocol buffers technology. And he's not kidding on the 'Fast' part either, see the performance stats <a href="http://code.google.com/p/protobuf-net/wiki/Performance">here</a>.</p>
<p>So I grabbed the library, chuck it into Unity and start testing. It worked in Unity Windows, worked in Unity Mac but when I deployed it to my iPhone I hit this:</p>
<blockquote>
<p>ExecutionEngineException: Attempting to JIT compile method</p>
</blockquote>
<p>Now, I'm no expert on the inner working of .Net and it's relationship with iOS running Mono, but according to <a href="http://forum.unity3d.com/threads/79418-json-parsing-w-JsonFx">this</a> thread on the Unity forums, the culprit is the JIT (Just in Time) compilation of classes that have not been seen by the system before. Mono on iOS is an AOT (Ahead of Time) only system, so that's why it craps out. I'm sure smarter people than me could provide a better explanation, but that will do for now.</p>
<p>So, I emailed Marc Gravell for help because he mentioned in <a href="http://stackoverflow.com/questions/5098459/c-mono-aot-with-protobuf-net-getting-executionengineexception">this</a> thread that version 2 of protobuf-net would have a 'pre-compile to dll' option, meaning that the serializer/deserializer classes can be pre-made in a dll instead of on the fly (and JIT'ed). I think that's how it works.</p>
<p>Anyway, he sent me an alpha version of a Unity iPhone friendly protobuf-net and that's the one that works. Woohoo.</p>
<p>I also have to say that this is all info that Marc sent me so all credit goes to him here. I wouldn't know a protocol buffer if it came up and bit me in the arse to be honest. </p>
<h3>The Solution</h3>
<p>First up, download the alpha version of the protobuf-net libraries. The link Marc sent me is <a href="http://code.google.com/p/protobuf-net/downloads/detail?name=iDevice%20alpha%201.zip">this one </a>but go check out the site to see if there is a later version.</p>
<p>This contains two libraries, the '<strong>Light Framework</strong>' and '<strong>Full Framework</strong>' versions of protobuf-net. The basic procedure is this:</p>
<ul>
<li>Create a library dll (assembly) of the model classes you want to serialize/deserialize. That is, just create a new 'Class Library' Visual Studio project and have it contain only your model. (I don't use MonoDevelop but I'm sure it's a similar process). You will have to reference the 'Light Framework' dll in this project in order to use the&nbsp; [ProtoContract]/[ProtoMember] attributes, described in the <a href="http://code.google.com/p/protobuf-net/wiki/GettingStarted">Getting Started</a> guide. Build this project to produce your MyModel.dll. </li>
</ul>
<pre class="brush: csharp">
namespace ProtoTest
{
    // Simple model classes, with some inheritence and generics thrown in. 
    [ProtoContract]
    public class MyModel
    {
        [ProtoMember(1)]
        public int int1 { get; set; }
        [ProtoMember(2)]
        public TestEnum enum1 { get; set; }

        public List<int> intList { get; set; }
        [ProtoMember(3)]
        public List<float> floatList { get; set; }
        [ProtoMember(4)]
        public List<string> stringList { get; set; }

        [ProtoMember(5)]
        public List<AnotherClass> anotherClassList { get; set; }
    }

    [ProtoContract, ProtoInclude(10, typeof(DerivedClass))]
    public class AnotherClass
    {
        [ProtoMember(1)]
        public string string1 { get; set; }
    }

    [ProtoContract]
    public class DerivedClass : AnotherClass
    {
        [ProtoMember(1)]
        public float float1 { get; set; }
    }

    public enum TestEnum
    {
        run,
        walk,
        skip
    }
}
</pre>
<ul>
<p><strong>Note: </strong>Make sure your model project is set to .Net 2.0 in the project properties, otherwise Unity will throw up the following error:</p>
<blockquote>Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'MyModel' </blockquote>
<li>Next you need to create the serilization/deserialization classes. So create a new 'Console Application' Visual Studio project. Now, for this one you need to reference the <strong>Full Framework</strong> protobuf-net library as well as obviously your newly created MyModel.dll assembly.&nbsp;</li>
<li>Now you need the code to create the libraries. </li>
</ul>
<pre class="brush: csharp">
var model = TypeModel.Create();

model.Add(typeof(AnotherClass), true);
// Note: you don't need to add DerivedClass here, in fact it craps out if you do. 
model.Add(typeof(TestEnum), true); 
model.Add(typeof(MyModel), true);
model.Compile("MySerializer", "MySerializer.dll");
</pre>
<ul>
<li>This will output 'MySerializer.dll'&nbsp;</li>
</ul>
<ul>
<li>Now we have our serialization library that we can use in our Unity project. So now you have to add three assemblies to your Unity iPhone project:  
<ul>
<li>MyModel.dll </li>
<li>MySerializer.dll</li>
<li>Protobuf-net.dll (<strong>Light Framework</strong>)</li>
</ul>
</li>
</ul>
<p>And we're good to go.</p>
<p>To serialize the files in my external application, I used the following code. I haven't played around with writing files to the iOS file system so I won't post that code, but I'm sure it's similar once you get the paths correct.</p>
<pre class="brush: csharp">
MyModel myNewModel = new MyModel();

MySerializer mySerializer = new MySerializer();

using(var file = File.Create("TestFile001.bytes"))
{
    mySerializer.Serialize(file, myNewModel);
}
</pre>
<p>In my case I was creating my game level files in an external application, so having the libraries external was actually more convenient. Once I had run my level editor app and created the binary serialized output files, I figured the easiest way to load them in Unity was via <strong>TextAsset</strong> class. TextAsset can be used to load files from the <strong>Resources</strong> folder just like any other resource, and despite the name, it is also fine for binary files.&nbsp;</p>
<p><strong>Note: </strong>From the Unity docs on <a href="http://unity3d.com/support/documentation/ScriptReference/TextAsset-bytes.html">TextAsset</a></p>
<blockquote>
<p>If you're using the text asset to contain binary data, you should <strong>make  sure the file has the .bytes extension</strong>. For any other of the extensions the TextImporter will try to strip nonascii characters if it  is unable to parse the file as an utf8 string.</p>
</blockquote>
<p>So inside our Unity project scripts, to read in the binary file we just use this.</p>
<pre class="brush: csharp">
TextAsset textFile = Resources.Load("TestFile001") as TextAsset;

MySerializer mySerializer = new MySerializer();

MyModel readInMyModel;

using (System.IO.Stream s = new System.IO.MemoryStream(textFile.bytes))
{
    readInMyModel = mySerializer.Deserialize(s, null, typeof(MyModel)) as MyModel;
}
</pre>
<p> I had a look via Reflector and the second parameter to Deserialize() there is used in case your type variable is null, so I assume you can use either one. </p>
<p>And there you have it. A bit more work than just using a library directly, but if performance is an issue then it is well worth the effort. I haven't done proper metrics yet, but from a quick look it seems at least an order of magnitude faster than the XmlSerializer I was using before.</p>
<p>&nbsp;</p>
<p>
<p>
<p>
<p> <b>Edit:</b> In response to the comment below about not being able to use Vector3.  </p>
<pre class="brush: csharp">    [ProtoContract]
    public class MyVector3
    {
        [ProtoMember(1)]
        public float x { get; set; }

        [ProtoMember(2)]
        public float y { get; set; }

        [ProtoMember(3)]
        public float z { get; set; }

        public MyVector3()
        {
            this.x = 0.0f;
            this.y = 0.0f;
            this.z = 0.0f;
        }

        public MyVector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public static implicit operator Vector3(MyVector3 v) 
        {
            return new Vector3(v.x, v.y, v.z);
        }

        public static implicit operator MyVector3(Vector3 v)
        {
            return new MyVector3(v.x, v.y, v.z);
        }
    }
</pre><p><br/><br/></p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-11000658.xml</wfw:commentRss></item><item><title>Unity and accessing common MonoBehaviour manager classes</title><category>C#</category><category>Interfaces</category><category>Singletons</category><category>Unity</category><dc:creator>Sam Cox</dc:creator><pubDate>Tue, 22 Mar 2011 08:01:53 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/3/22/unity-and-accessing-common-monobehaviour-manager-classes.html</link><guid isPermaLink="false">512318:5867401:10869932</guid><description><![CDATA[<p>I've started a few Unity codebases over the last couple of years and one of the first things you want to do is work out how to access your MonoBehaviour 'Manager' classes attached to gameobjects from other scripts.</p>
<p>Now, the manager-type scripts that are attached to our Unity GameObjects are essentially Singletons. I usually call them 'MonoBehaviour Singletons' because they usually exist just once and need to be accessed from all over the place. I'm talking here about things like a class that manages all the playing of sound for a particular scene. You create a script called, for example, SoundManager and attach it to a GameObject in the Unity Editor and it is the single point of sound management throughout your scene. What I'm talking about here is how to access that script from wherever you need it.</p>
<p>So here is our little SoundManager class that we want to access. It just has a single method, PlayClickSound().</p>
<pre class="brush: csharp">public class SoundManager : MonoBehaviour
{
    public void PlayClickSound()
    {
    }
}
</pre>
<h3><p>Direct Access</p></h3>
<p>Now of course, the 'easiest' way to access that script is via a call to GameObject.FindObjectOfType or, if you have several classes of the same type, GameObject.FindWithTag where you have 'tagged' each gameobject differently.</p>
<pre class="brush: csharp">(GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager).PlayClickSound();
// or
(GameObject.FindWithTag("Sounds").GetComponent(typeof(SoundManager)) as SoundManager).PlayClickSound();
</pre>
<p>But, calls to these methods are expensive and you absolutely do not want to be calling them often.</p>
<h3><p>Keep a Reference</p></h3>
<p>The first solution that is in the documentation is to call those expensive methods once in the Awake or Start method of each script that needs them, then store the reference for all further calls.</p>
<pre class="brush: csharp">public class TestClass : MonoBehaviour
{
    SoundManager mySoundManager;
    bool needToPlaySound;

    void Awake()
    {
        this.mySoundManager = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
    }

    void Update()
    {
        if (this.needToPlaySound)
        {
            this.mySoundManager.PlayClickSound();
        }
    }
}
</pre>
<p>From a performance perspective, that's pretty much all you'd need to do (unless for example you are doing this from a 'bullet' script and creating many bullets a second) and I'm sure you all know that, but I'm looking at it from a maintainability and ease-of-use perspective. With even a small project, the code above is going to be copied many times.</p>
<h3><p>Singleton Manager</p></h3>
<p>In my opinion, the lesser the code the better. So, what's next? Create a static class that holds this code that we can access from anywhere. Since this class is controlling our access to the MonoBehaviour 'Singleton' manager classes, I've called it the SingletonManager</p>
<pre class="brush: csharp">public class SingletonManager
{
    public static SoundManager GetSoundManager()
    {
        return GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
    }
}
</pre>
<p>Then in our classes we can access it like this:</p>
<pre class="brush: csharp">public class TestClass : MonoBehaviour
{
    SoundManager mySoundManager;
    bool needToPlaySound;

    void Awake()
    {
        this.mySoundManager = SingletonManager.GetSoundManager();
    }

    void Update()
    {
        if (this.needToPlaySound)
        {
            this.mySoundManager.PlayClickSound();
        }
    }
}
</pre>
<p>This is better in my option, but still not perfect. There is still a lot of code repeated in each of our accessing classes. The less code in our classes, the easier it is to see what is happening and the easier the maintain the code.</p>
<h3><p>Singleton Singleton Manager</p></h3>
<p>So, another option is to make our SingletonManager a classic singleton and have it store the references to all the MonoBehaviour singletons.</p>
<pre class="brush: csharp">public class SingletonManager
{
    private static SingletonManager instance;
    public static SingletonManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new SingletonManager();
            }
            return instance;
        }
    }

    private SoundManager soundManager;
    public SoundManager SoundManager
    {
        get
        {
            if (this.soundManager == null)
            {
                this.soundManager = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
            }
            return this.soundManager;
        }
    }
}
</pre>
<p>Then we can access it like this:</p>
<pre class="brush: csharp">public class TestClass : MonoBehaviour
{
    bool needToPlaySound;
    
    void Update()
    {
        if (this.needToPlaySound)
        {
            SingletonManager.Instance.SoundManager.PlayClickSound();
        }
    }
}
</pre>
<p>Note: I'm not touching on thread-safety here for simplicity but it shouldn't matter to most people. If you're doing anything with threads you'd better Google "threadsafe singleton"<p>
<p>This way we have concentrated the code accessing the MonoBehaviour singletons in one place. The Awake method can be used for more essential code needed there, and in many cases can be removed all together.</p>
<h3><p>Interfaces - Use 'em</p></h3>
<p>Now one final problem is that these MonoBehaviour singletons we are exposing with the SingletonManager are (obviously) extending the  MonoBehaviour class, which is a huge class with many methods on it. This  means that you have way too much access to that class and its attached  GameObject that you need.</p>
<p>For example, you can type SingletonManager.Instance.SoundManager.transform.rotation = ... and suddenly you're able to change the rotation of the SoundManager's GameObject from anywhere in the code. From a good coding viewpoint this is not a good thing. The SoundManager should only let external classes have access to a very limited number of methods. That's where interfaces can save you.</p>
<p>So, instead of having the SingletonManager expose the SoundManager class directly, we create an interface ISoundManager and expose that.</p>
<pre class="brush: csharp">public interface ISoundManager
{
    void PlayClickSound();
}
</pre>
<p>Then our SoundManager will use the interface like this:</p>
<pre class="brush: csharp">public class SoundManager : MonoBehaviour, ISoundManager
{
    public void PlayClickSound()
    {
    }
}
</pre>
<p>And the SingletonManager will look like this:</p>
<pre class="brush: csharp">public class SingletonManager
{
    private static SingletonManager instance;
    public static SingletonManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new SingletonManager();
            }
            return instance;
        }
    }

    private SoundManager soundManager;
    public ISoundManager SoundManager
    {
        get
        {
            if (this.soundManager == null)
            {
                this.soundManager = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
            }
            return this.soundManager;
        }
    }
}
</pre>
<p>Much better. Now, when something screwy is happening to our SoundManager, we know exactly what the external classes are capable of doing to it. Plus it essentially forces the coder to think about what relationship our MonoBehaviour singleton classes have with each other. If you need to do something more you have to add it to the interface, rather than hack away at whatever properties you want.</p>
<p>I highly recommend using interfaces in this way. You will thank yourself as your codebase grows.</p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-10869932.xml</wfw:commentRss></item><item><title>Spartan Athletics</title><category>Game-Dev</category><category>Games</category><category>Unity</category><category>iPhone</category><dc:creator>Sam Cox</dc:creator><pubDate>Wed, 09 Mar 2011 00:53:31 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2011/3/8/spartan-athletics.html</link><guid isPermaLink="false">512318:5867401:10715620</guid><description><![CDATA[Little post about the game I recently developed, Spartan Athletics.]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-10715620.xml</wfw:commentRss></item><item><title>Updated Unity Bézier curve - Now in easy to use Prefab form</title><category>C#</category><category>Unity</category><dc:creator>Sam Cox</dc:creator><pubDate>Fri, 21 May 2010 02:14:54 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2010/5/20/updated-unity-bezier-curve-now-in-easy-to-use-prefab-form.html</link><guid isPermaLink="false">512318:5867401:7739929</guid><description><![CDATA[<p>So a while ago I implemented a B&eacute;zier curve for use in fly-throughs and anywhere where you need something to follow a nice curve. It worked kinda well enough for a simple fly-through but in the project I'm currently working on I needed much more control over the curve, so I've redone it and now it's much more&nbsp;usable.&nbsp;</p>
<p>The previous one used a kind of quazi B&eacute;zier&nbsp;curve with the control points being the waypoints around the point you're calculating. This one uses proper control points, and they are all nicely editable.&nbsp;</p>
<p>&nbsp;Here is the prefab once you drag it into the scene. It comes with two points. To create more just duplicate one of the points. The green points represent the waypoints that make up the curve, and the purple points are the left and right control points for each waypoint.&nbsp;</p>
<p><span class="full-image-block ssNonEditable"><span><img style="width: 600px;" src="http://www.frictionpointstudios.com/storage/bezier-curve-post/prefab01.jpg?__SQUARESPACE_CACHEVERSION=1274409076649" alt="" /></span></span></p>
<p>With the added control, you can make any curve you want now.&nbsp;</p>
<p><span class="full-image-block ssNonEditable"><span><img style="width: 600px;" src="http://www.frictionpointstudios.com/storage/bezier-curve-post/curve01.jpg?__SQUARESPACE_CACHEVERSION=1274410217922" alt="" /></span></span></p>
<p>&nbsp;To use this prefab, the only Interface you should have to worry about is <strong>IBezierCurveManager</strong>. This is the interface to the BezierCurveManager (duh) and it only has two methods.&nbsp;</p>
<ul>
<li>Vector3 GetPositionAtTime(float time);</li>
<li>Vector3 GetPositionAtDistance(float distance, float time);</li>
</ul>
<p>Plus the options on the BezierCurveManager script&nbsp;editable&nbsp;from the Editor.&nbsp;</p>
<ul>
<li>DrawGizmos</li>
<li>DrawControlPoints</li>
<li>IsFullLoop</li>
<li>SecondsForFullLoop</li>
<li>EnableDistanceCalculations</li>
</ul>
<p><strong>GetPositionAtTime</strong> <strong>-</strong> Gets a position along the curve at a time, with 0 being the start of the curve and SecondsForFullLoop being the end (or start again if IsFullLoop is true).&nbsp;</p>
<p><strong>GetPositionAtDistance -</strong> The same as GetPositionAtTime but with a distance offset. This is useful for having one object follow another. Because the speed along the curve is not constant usually (unless your waypoints are evenly spaced. In order to use this method you must have EnableDistanceCalculations set to true before starting.&nbsp;</p>
<p>Now, it's a little hacky how I get the Distance calculations for use in EnableDistanceCalculations. I basically do a run around the loop first and manually calculate the distance. I'm sure there is a nicer mathematical way to do it, but this will do for now. But what it means is that there is a little pause on startup where it calculates it all. If you don't want this and don't want to call EnableDistanceCalculations then just set EnableDistanceCalculations to false.&nbsp;</p>
<p>So, here is the <a href="http://www.frictionpointstudios.com/storage/bezier-curve-post/BezierCurvePrefabPackage.unitypackage">Prefab Package</a>. Hope it all works.&nbsp;</p>
<p>And here is an <a href="http://www.frictionpointstudios.com/storage/bezier-curve-post/BezierCurveExamplePackage.unitypackage">Example Project</a> using it. Here we have a red ball going really fast along the curve, a bigger red ball going more slowly along it, and two purple balls a fixed distance ahead and behind it.&nbsp;</p>
<p><span class="full-image-block ssNonEditable"><span><img style="width: 600px;" src="http://www.frictionpointstudios.com/storage/bezier-curve-post/curve02.jpg?__SQUARESPACE_CACHEVERSION=1274411047034" alt="" /></span></span></p>
<p>The example project has a source file Sphere.cs which shows simply how to find the IBezierCurveManager object and call GetPositionAtTime on it.&nbsp;</p>
<p><strong>A note on speed:</strong>&nbsp;</p>
<p>This is crazy fast, so if you only have a few objects using it you should not have a problem with speed. The GetPositionAtDistance call does do more than the normal one, but it's just running through arrays which seem to be stupidly quick. I'm sure there are optimisations to be found, but I didn't see the point at the moment. I've tested it on my 3GS iPhone with negligible effect.&nbsp;</p>
<p>Enjoy!</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-7739929.xml</wfw:commentRss></item><item><title>Unity webplayer - load on demand javascript</title><dc:creator>Sam Cox</dc:creator><pubDate>Thu, 08 Apr 2010 02:04:01 +0000</pubDate><link>http://www.frictionpointstudios.com/blog/2010/4/7/unity-webplayer-load-on-demand-javascript.html</link><guid isPermaLink="false">512318:5867401:7262942</guid><description><![CDATA[<p>The Unity Webplayer is a pretty awesome thing, and it's what first got me interested in Unity in the first place. The ability to write a full 3D game and have it playable in any browser is pretty amazing.</p>
<p>When you deploy to the Webplayer, Unity spits out a default html file that you can stick on a webserver and have your game playable instantly, which is also awesome, but it's a little limited.</p>
<p>First off, it's a little hard to work out how to embed it in a page other than the default one. Secondly it loads automatically, which if you have several Unity files on the one webpage (like a blog) then it will be pretty annoying for the user to have them all load at once. Not to mention what the performance will be like.</p>
<p>So, I've tried to simplify things a little. The goals are;</p>
<ul>
<li>To show an image first, then if the user clicks the image the Webplayer loads</li>
<li>To make it easier to embed in a blog. </li>
</ul>
<p>I started with the default Webplayer html, and pulled out all the script components. I've saved them all into a file called '<strong>unityscripts.js</strong>'. Now, the chunk of javascript in the 'body' of the default file is the entry point to load the Webplayer, so I made this a new function called '<strong>LoadUnityAfterClick</strong>' and added four arguments to it. The path to the unity file, a unique&nbsp;id, &nbsp;the width and the height. This makes all the javascript generic so it can be reused all over the site.</p>
<p>Next it gets a little dodge. For detecting the Webplay in Internet Explorer, there is a vbscript method in the defaut file. This caused a bit of a problem, because I could not for the life of me work out how to call this method '<strong>DetectUnityWebPlayerActiveX</strong>' from the now external javascript. If you know of a way to do that I'm all ears.</p>
<p>So I decided to rewrite the vbscript component in javascript. According to the Unity 3D <a href="http://unity3d.com/support/documentation/Manual/Detecting%20the%20Unity%20Web%20Player%20using%20browser%20scripting.html">documentation</a> this is not the best because...</p>
<blockquote>
<p>VBScript is used instead of JavaScript because after detection it releases the plugin resource immediately (whereas in JavaScript it would only happen the next time garbage collection is performed).</p>
</blockquote>
<p>That didn't sound like too much of a deal breaker to me, so I've decided to ignore it.</p>
<p>So now that the whole thing is contained in one javascript file, embedding the Webplayer in something like Squarespace becomes much easier.</p>
<p>Now I'm no javascript or html expert, so for the Load on image part I was helped by some code from <a href="http://www.webdevforums.com/showpost.php?p=100186&amp;postcount=5">here</a>.</p>
<p>First you just save the '<strong>unityscripts.j</strong>s' file somewhere. Then all the code you need to add is the image href that calls the javascript in its OnClick method, plus the noscript part for the tin-foil-hat brigade that think everyone is out to steal their porn stash.</p>
<p>I won't mess up this page with a whole bunch of ugly code so I'll just link to the files. I made them rtf so they have colours and are a little easier to read but there is also a text version which is what you should copy if you want to use this.</p>
<p>The Embedded Code is <a href="http://www.frictionpointstudios.com/storage/embed-code-for-blog/Embeded%20Code.rtf">here</a>&nbsp; or as text <a href="http://www.frictionpointstudios.com/storage/embed-code-for-blog/Embeded%20Code.txt">here</a>.</p>
<p>The UnityScripts.js file is <a href="http://www.frictionpointstudios.com/storage/embed-code-for-blog/UnityScripts.rtf">here</a> or as text <a href="http://www.frictionpointstudios.com/storage/embed-code-for-blog/UnityScripts.txt">here</a>.</p>
<p>The UnityScripts.js file should work right off the bat, but the Embeddded Code part has placeholders that need to be searched for and replaced. They are fairly self explanitory.</p>
<ul>
<li>FULL_PATH_TO_UNITYSCRIPT.JS</li>
<li>FULL_IMAGE_PATH</li>
<li>FULL_UNITYFILE_PATH</li>
<li>UNIQUE_ID </li>
<li>WIDTH</li>
<li>HEIGHT</li>
</ul>
<p>So for an example, if you're wanting to add your unity file to a squarespace blog;</p>
<ol>
<li>Go to 'File Storage' and add the UnityScripts.js file. Then hit 'url' to copy the full path.&nbsp;</li>
<li>Do the same with the Image file and the Unity file. </li>
<li>Open up the 'Embedded Code' file in notepad or something and replace the variables above with the full paths you got when importing them, plus a uniqeId and a width and height that you'd like. Note: The UNIQUE_ID in the href needs the # in front of it so don't accidentally delete it. </li>
<li>Start a new post and hit 'Insert Code Block' and add the 'Embedded Code' to your post.&nbsp;</li>
<li>?</li>
<li>Profit. </li>
</ol>
<p>&nbsp;Here is a little demo...</p>
<p><!--Add a reference to the Javascript file with all the goodies--><script language="javascript1.1" 
		            type="text/javascript" 
		            src="http://www.frictionpointstudios.com/storage/scripts/unityscripts.js"></script> <!--Insert the image that will be replaced with the webplayer when clicked--></p>
<div id="Example01"><a href="#Example"><img onclick="LoadUnityAfterClick('http://www.frictionpointstudios.com/storage/embed-code-for-blog/LoadOnDemandExample.unity3d', '300', '200', 'Example01')" src="http://www.frictionpointstudios.com/storage/embed-code-for-blog/OnDemand.jpeg" border="0" alt="No images? Hello 1993" /> </a></div>
<!--Add the no script part for the paranoid-->
<p><noscript><object id="Object1" 
				        classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" 
				        width="300" 
				        height="200" 
				        codebase="http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0">
					<param name="src" value="http://www.frictionpointstudios.com/storage/embed-code-for-blog/LoadOnDemandExample.unity3d" />
					<embed id="UnityEmbed" 
					       src="http://www.frictionpointstudios.com/storage/embed-code-for-blog/LoadOnDemandExample.unity3d" 
					       width="300" 
					       height="200" 
					       type="application/vnd.unity" 
					       pluginspage="http://www.unity3d.com/unity-web-player-2.x" />
					<noembed>
						<div align="center">
							This content requires the Unity Web Player<br /><br />
							<a href="http://www.unity3d.com/unity-web-player-2.x">Install the Unity Web Player today!</a>
						</div>
					</noembed>
				</object> </noscript></p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://www.frictionpointstudios.com/blog/rss-comments-entry-7262942.xml</wfw:commentRss></item></channel></rss>