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. :)

45 Responses to "Unity to Flash Exporter with iTween"

  1. Nice work Devin, but it took me way more than a day to build iTween so do I get a beer too?

  2. Devin Reimer says:

    @Pixelplacement I think you should get one too. :)

  3. You rock. Can’t wait to get you version 3. The API will be a warm welcome. 😉

  4. As sorely tempted as I am to integrate this into my current entry, I’d hate for some unforeseen error to cause fatal crashes during the judgement period of the contest.

  5. On second thought, if it’s got Bob’s official endorsement then I guess it wouldn’t hurt. One iTween here.. another there…

  6. Devin Reimer says:

    Hi @Dark Acre Jack

    If it makes you feel better, I finished porting over one of my games Office Zombie Simulator. It uses iTween a lot and it works great. http://blog.almostlogical.com/2011/12/23/office-zombie-simulator-in-flash-via-unity/

  7. Mathieu says:

    Thanks for the port!
    Note that you can’t use inline hashtables in JavaScript. it throw a “Definition Boo.Lang:Hash could not be found” error.

  8. GFX47 says:

    Hey, thank you guys for this version of iTween.
    I’m getting this strange error in my flash build:

    Fatal Error : TypeError: Error #1006: value n’est pas une fonction.
    at global::iTween/iTween_ConflictCheck()[…\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween.as:4233]
    at global::iTween/iTween_TweenStart()[…\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween.as:2213]
    at global::iTween__Start_c__Iterator3/IEnumerator_MoveNext()[…\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween__Start_c__Iterator3.as:48]
    at com.unity::UnityNative$/Ext_Scripting_InvokeMoveNext()
    at com.unity::UnityNative$/_ZN9Coroutine3RunEv()
    at com.unity::UnityNative$/_ZN13MonoBehaviour15CreateCoroutineEP15ScriptingObjectP15ScriptingMethod()
    at com.unity::UnityNative$/_ZN13MonoBehaviour30InvokeMethodOrCoroutineCheckedEP15ScriptingMethodPFP15ScriptingObjectPvPP18ScriptingExceptionES3_()
    at com.unity::UnityNative$/_ZN13MonoBehaviour16DelayedStartCallEP6ObjectPv()
    at com.unity::UnityNative$/_ZN18DelayedCallManager6UpdateEi()
    at com.unity::UnityNative$/_Z10PlayerLoopbb()
    at com.unity::UnityNative$/NativeExt_PlayerLoop()
    at com.unity::UnityContentInternal/playerLoop()
    at com.unity::UnityContent/onEnterFrame()

    Does it ring any bell to anyone?
    Thanks again.
    GFX47

  9. Devin Reimer says:

    @GFX47

    I’m guessing, it seem to be erroring out when it discovered another identical iTween exists. Send over an example of what causes this error (devin@almostlogical.com) and I will see if I can fix that for you.

  10. Devin Reimer says:

    @Mathieu I didn’t end up testing in UnityScript as that is something I don’t normally worry about. If you discover what causes that send it my way and I will happily update the script.

  11. Sebastian says:

    It works really for me nice thanks!

  12. Mathieu says:

    @GFX47
    I have the exact same error on a :
    iTween.RotateBy(MonDiametre,iTween.Hash(“amount”, Vector3(0, 0.02, 0), “time”, 0.2, “easetype”, iTween.EaseType.linear));

  13. GFX47 says:

    @Mathieu
    It seems to occur when you launch an iTween effect on a game object already running one.
    Here is a repro project: http://dl.dropbox.com/u/24322340/Unity3.5%2BiTween.zip
    It works under editor but not once build in flash.

  14. Tof42 says:

    @GFX47 @Mathieu

    Hi,

    I have the same problem, just replace line 7511 of itween.cs with this line :

    if((item.tweenArguments[currentProp.Key]!=tweenArguments[currentProp.Key]) && (string)currentProp.Key != “id”){

    This should do the trick, at least it’s working for me.
    Also, thanks a lot Devin for the first working version, it’s really great :)

  15. yue says:

    It seems there is a mistake at line 6950

    should be
    destinationArray[n+destinationIndex] = sourceArray[n+sourceIndex];

  16. Devin Reimer says:

    Thanks @GFX47 and @Mathieu for reporting that bug and a big thanks to @Tof42 for actually finding a fix for it. Also thanks @yue for finding an squashing another bug.
    I’ve have now added both these bug fixes to iTween (it is now V1.1).

  17. GFX47 says:

    @Tof42 Okaaaaay that’s must be linked to this bugged comparator thing I guess.
    @* Thanks for the fix! 😉

  18. Tim Miller says:

    Thanks @Tof42, @yue and especially Devin for the quick fix, the changes in v1.1 cleared up those issues for me.

    I’m still getting the following in my flashlog.txt when calling MoveTo and PunchScale (haven’t tried others yet). Not sure if this is a problem or not but thought I’d mention it.

    Can’t remove iTween (Script) because Mono’s disabled (MonoBehaviour), Mono’s disabled (MonoBehaviour), Mono’s disabled (MonoBehaviour) depends on it
    LogCallStackError
    at com.unity::UnityNative$/Ext_Flash_LogCallstack()
    at com.unity::UnityNative$/_Z17DebugStringToFilePKciS0_iiii()
    at com.unity::UnityNative$/_Z22DestroyObjectHighLevelP6Objectb()
    at com.unity::UnityNative$/_Z22DelayedDestroyCallbackP6ObjectPv()
    at com.unity::UnityNative$/_ZN18DelayedCallManager6UpdateEi()
    at com.unity::UnityNative$/_Z10PlayerLoopbb()
    at com.unity::UnityNative$/NativeExt_PlayerLoop()
    at com.unity::UnityContentInternal/playerLoop()
    at com.unity::UnityContent/onEnterFrame()
    Can’t remove iTween (Script) because Mono’s disabled (MonoBehaviour), Mono’s disabled (MonoBehaviour), Mono’s disabled (MonoBehaviour) depends on it

    (Filename: C:/BuildAgent/work/b0bcff80449a48aa/Runtime/Misc/GameObjectUtility.cpp Line: 982)

  19. Devin Reimer says:

    @Tim Miller There is an issue with the Flash exporter that is throws an error when trying to remove a MonoBehaviour using Destroy. The error doesn’t break anything and just stops execute of that current execution of the script. My modified iTween is built to survive this silent error.
    Long story short, this should cause you no problem.

  20. Mathieu says:

    For some reason
    if((item.tweenArguments[currentProp.Key]!=tweenArguments[currentProp.Key]) && (string)currentProp.Key != “id”){
    Don’t seems to act as the same as
    if(!item.tweenArguments[currentProp.Key].Equals(tweenArguments[currentProp.Key]) && (string)currentProp.Key != “id”){

    Not easy to describe but it’s like my RotateBy method is launched *after* the launching event not *during it*. Am I the only one seeing this?

  21. Tof42 says:

    @Mathieu

    It’s indeed possible that it doesn’t act the same, but you should only see a difference when launching a new tween on an object on which another tween was already running. Is it your case ?
    I am not an expert in iTween but as far as I understand the code, if there was a problem with the fix, you could have 2 kind of problem:
    – 2 tweens conflicting and running at the same time
    – 1 tween not happening at all.

    I don’t think it could delay a tween, but I may be wrong…

  22. Devin Reimer says:

    @Mathieu I agree with @Tof42 that is possible Equals and == do not yield the same result. I can’t seem to come up with a good test to determine the problem. I agree with @Tof42 analysis. If you have an example case that is causing a problem, send it over to me: devin@almostlogical.com or toss a link to it here. I can then take a look.

  23. GFX47 says:

    And you can add Dictionary.Remove to the unsupported features list :/
    Here is the proof project: http://goo.gl/E7MbJ
    Logs talk about a null object reference exception…

  24. GFX47 says:

    Tip from Ralph Hauwert (@UnitZeroOne): for buggy audio sources (distorted sound, ugly noise, etc), set rolloff mode to custom!

  25. Nattawit says:

    Hi, Devin. Thank you for iTween flash build. I got this error. I only use iTween.MoveTo funcition with random path. And eastype is only linear or easeInOutCubic. I’m not sure what caused the problem.

    Fatal Error : TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at UnityEngine::Vector3$/op_Subtraction_Vector3_Vector3()
    at UnityEngine::Vector3$/op_Inequality_Vector3_Vector3()
    at global::iTween/iTween_GenerateMoveToPathTargets()[C:\Documents and Settings\Joybook S41 Series\Desktop\AntiMatter\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween.as:1355]
    at global::iTween/iTween_GenerateTargets()[C:\Documents and Settings\Joybook S41 Series\Desktop\AntiMatter\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween.as:978]
    at global::iTween/iTween_TweenStart()[C:\Documents and Settings\Joybook S41 Series\Desktop\AntiMatter\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween.as:2214]
    at global::iTween__Start_c__Iterator3/IEnumerator_MoveNext()[C:\Documents and Settings\Joybook S41 Series\Desktop\AntiMatter\Temp\StagingArea\Data\ConvertedDotNetCode\global\iTween__Start_c__Iterator3.as:48]
    at com.unity::UnityNative$/Ext_Scripting_InvokeMoveNext()
    at com.unity::UnityNative$/_ZN9Coroutine3RunEv()
    at com.unity::UnityNative$/_ZN13MonoBehaviour15CreateCoroutineEP15ScriptingObjectP15ScriptingMethod()
    at com.unity::UnityNative$/_ZN13MonoBehaviour30InvokeMethodOrCoroutineCheckedEP15ScriptingMethodPFP15ScriptingObjectPvPP18ScriptingExceptionES3_()
    at com.unity::UnityNative$/_ZN13MonoBehaviour16DelayedStartCallEP6ObjectPv()
    at com.unity::UnityNative$/_ZN18DelayedCallManager6UpdateEi()
    at com.unity::UnityNative$/_Z10PlayerLoopbb()
    at com.unity::UnityNative$/NativeExt_PlayerLoop()
    at com.unity::UnityContentInternal/playerLoop()
    at com.unity::UnityContent/onEnterFrame()

  26. Nattawit says:

    I also want to mention that I also use iTween.Stop(gameObject); to interrupt and change the movement midway. I don’t know if this is possible to cause iTween path to lost its object reference.

  27. Nattawit says:

    I figure out the problem. The input path in flash build has to be array of Vector3. It doesn’t work for array of Transform (but it still works in editor).

  28. Lance Snider says:

    I was having an issue where everything worked fine in testing, but in Flash player the tweens wouldn’t run. They would just instantly move over as if the time were set to 0. Here was the old code:

    iTween.MoveTo(myGO, {“x”,100, “time”,1});

    Turns out the problem was that it doesn’t like “x”. You need to use a Vector3 position instead. Ex:

    iTween.MoveTo(myGO,iTween.Hash(“position”,Vector3(100,0,0), “time”,1));

    Also, you need to use iTween.Hash(), not just the normal {}.

    I hope that helps someone. :)

  29. 3.5 Publisher says:

    Hi! Thanks for this quick fix. I get two error messages when I try to export to Flash.

    global/iTweenEvent.as(99): col: 37 Error: Access of undefined property Enumerable.

    global/iTweenEvent.as(15): col: 20 Error: Definition System.Linq:Enumerable could not be found.

    Otherwise it is working.

  30. Devin Reimer says:

    @Publisher

    Unity->Flash exporter does not support Linq and it appears the the iTweenEvent system is using it somewhere. If I get a chance I will take a look.

  31. mr12fingers says:

    Great work guys.
    I am having an issue exporting to flash though.
    I keep receiving this error:

    C:\Users\Administrator\Documents\Unity_TechDemo\Temp\StagingArea\Data\ConvertedDotNetCode\global\ViewCamera.as(56): col: 19 Error: Type was not found or was not a compile-time constant: Hash.

    C:\Users\Administrator\Documents\Unity_TechDemo\Temp\StagingArea\Data\ConvertedDotNetCode\global\ViewCamera.as(56): col: 30 Error: Call to a possibly undefined method Hash.

    They comes with several other errors but these two seem like they would be the best help in identifying the issue.

    Regards,

    Mr.12
    And idea what it could be?

  32. Mel says:

    I’m getting the same problem as mr12fingers ..

    UNITY ERROR:
    Error: Type was not found or was not a compile-time constant: Hash.

    ORIGINAL LINE IN JS ( causing the problem):
    iTween.LookUpdate(gameObject,{“looktarget”:target,”time”:rotationSpeed,”axis”:”y”});

    AS ERROR LINK LINE :
    var $expr_1B4: Hash = new Hash().Hash_Constructor();

    Any help would be great!

  33. Ryan says:

    Thanks very much for this mate! Works like a peach.

  34. Devin Reimer says:

    Hi @mr12fingers & @Mel, sorry for the very delayed response. Kept thinking I found find time to take a look at this issue but it never happen.

    This is looks like a Hashtable issue. I’ve noticed they are not completely supported.

    I did not test this using UnityScript, if @mr12fingers is also using UnityScript maybe that is the cause of the problem.

  35. Kirill says:

    Functions like LookAt didn’t works in Flash SWF

  36. Devin Reimer says:

    Hi @Kirill, There is a couple of things that don’t work in the Flash port yet. Haven’t had time to look into them, if you discover a fix let me know so I can add it in.

  37. keraj37 says:

    I really want want to thank the author. You helped me a lot! God bless you!

    JS

  38. mtoivo says:

    Just for the record, in case someone is wondering it, using path does not work, at least when trying to supply array of Vector3 as path. Type checking in the AS-converted code seems to be broken, and the the array is taken as an array of Transform. Using such array might work, didn’t bother testing it (since Transforms cannot be created on the fly it seems).

  39. mtoivo says:

    Just for the record, I managed to hack together a fix for the path-issue, also enables ithe TweenPath -plugin in flash. Corresponding forum post here: http://forum.unity3d.com/threads/143459-Compiling-to-Flash-Erorrs?p=1008977&viewfull=1#post1008977

  40. Devin Reimer says:

    Hi @mtoivo,

    Thanks for posting the fix!

  41. mtoivo says:

    Any ideas to get things rolling with Unity 4? Tried the stock-version too, still no go without some modifications.

  42. Dented Pixel says:

    LeanTween (a super efficient tweening engine for Unity) now supports Flash Publishing as well (Unity 3.5.x and 4.x). This is not a port of the original engine but the whole engine itself, every feature should work as expected.

    You can see a demo over at: http://dentedpixel.com/developer-diary/leantween-now-supports-flash-publishing/

    The engine itself can be found on the Asset Store at: http://u3d.as/content/dented-pixel/lean-tween/31i

Leave a response