Blog

Unity to Flash Exporter with iTween

December 22, 2011 by Devin Reimer

If you didn’t already know Unity 3.5 Developer Preview is now available to the public.
http://unity3d.com/unity/preview/

To me the most exciting thing in the developer preview is the Unity to Flash exporter. The ability to easily create and distribute 3D content on the web without needing the user to have a different plugin.

So the first thing I did was start to port over some older projects. As this is an early build there are a lot of limitations, so moving an existing complex project over isn’t an easy task. One of the major stumbling blocks I had (and from the sounds of things a lot of people are having) is that iTween simply doesn’t work.

iTween is great framework for doing tweening in Unity. So I decided that with my deep knowledge of Unity, iTween and AS3 I would attempt to ‘fix’ the iTween libary to work with the Flash exporter.

Turns out iTween uses a lot of unsupported features and that coupled with the exporter being very new, turned this into a very challenging problem.
45+ hacks and a lot of knowledge later, I won.

Without further ado here is Flash export ready version of iTween: iTween.cs

Here is a quick demo proving it works: SimpleCrateDemo

This version of iTween is a little rough around the edges, but it works in my initial tests.

Known Issues

-ColorTo & ColorFrom are unsupported as they cause an infinite loop (reason unknown)
-FadeTo & FadeFrom are unsupported as they cause an infinite loop (reason unknown)
-Flash player throws a silent error on Destroy of iTween component (should not effect anything but will show up in Flash debug)

There are probably more issues so let me if you come across any.

I do know that a new version of iTween is being worked on and the Flash exporter is in an early state so this is a temporary version to hold everyone over.

PS: This took me an entire day, so if someone uses this to win the Unity ‘Flash in a Flash’ contest you official owe me a drink. :)

iTween Parameter Code Hinting

September 13, 2010 by Devin Reimer

For those who don’t already know, iTween is a great tweening library for Unity. If you haven’t yet had a chance to use it, go download it. I guarantee that you will find it useful.

Now that introductions are out of the way, on to the point at hand. Many code libraries (especially tweening libraries) have the problem that there is no access to code hinting for properties being passed into it’s functions. The reason being since there are so many possible parameters that could be past into each function a list of optional parameters are requested instead of a sequentially list of required parameters. This makes using these functions a lot easier and more customizable. The downside is there is no coding hinting for what  parameters could be possible passed in.

In the case of using iTween in C# you pass arguments into a Hashtable and that Hashtable is then in turn passed into the these functions.
Example:

Hashtable tweenHash = new Hashtable();
tweenHash.Add("x", -5);
tweenHash.Add("y", 5);
tweenHash.Add("time", 10);
iTween.MoveAdd(gameObject, tweenHash);

Or as a single line (if using Unity 3.0 or greater):

iTween.MoveAdd(gameObject,new Hashtable(){{"x",-5},{"y",5},{"time",10}});

Or an even shorter single line (does have additional creation overhead)

iTween.MoveAdd(gameObject,iTween.Hash("x",-5,"y",5,"time",10));

The only way to know what the list of possible parameters are for a particular function is to either a) open iTween’s source and look at the comments or b) look through the iTween documentation.

Since this slows down learning and speed of using a new library I set off to try to find a better way. After exploring some different options I think I have found a better way. I wrote an app that goes through the iTween library and creates a Helper class called iT (contained within iTweenHinting.cs). Using this class you will have the ability to access code hinting/discovery when using iTween.

For example:

Hashtable tweenHash = new Hashtable();
tweenHash.Add(iT.MoveAdd.x, -5);
tweenHash.Add(iT.MoveAdd.y, 5);
tweenHash.Add(iT.MoveAdd.time, 10);
iTween.MoveAdd(gameObject, tweenHash);

Or as a single line (if using Unity 3.0 or greater):

iTween.MoveAdd(gameObject,new Hashtable(){{iT.MoveAdd.x,-5},{iT.MoveAdd.y,5},{iT.MoveAdd.time,10}});

Or even shorter single line (does have additional creation overhead)

iTween.MoveAdd(gameObject,iTween.Hash(iT.MoveAdd.x,-5,iT.MoveAdd.y,5,iT.MoveAdd.time,10));

When you type ‘iT.’ within a code editor it will give you a list of all tweening functions within iTween. Once you select the function you wish to use (ex: MoveAdd), hit ‘.’ and your code editor will then give you a list of all possible parameters for that function (see image above).

The great thing about this method, is as you get more comfortable with the iTween you can write out the strings of the simple and easy to remember parameters, while still having the ability to fallback on the helper class.

To download iTweenHinting.cs click here (right-click save as).

Note: While I don’t expect the list of functions and parameters within iTween to change that often. I will do my best to keep iTweenHinting.cs up-to-date.

If you have any suggestions on ways I could improve/change iTweenHinting.cs please leave them in the comments below.

Interesting side note: For this project I wrote code that reads code to write code that allows you to write code faster. Plus it was AS3 that was reading and writing C#. Isn’t programming fun. :)