Five Common AS3 to UnityScript Translations

i’ve been using Unity3D on and off for a week now. i’d burned through about a dozen beginner tutorials, and am a little overwhelmed by how much i don’t know about the engine, which enables you to create 3D games to deploy to a number of platforms, including the iPhone and web browsers.

i’d look like a fool if i tried to write a tutorial at this point. So instead, i’ll share this list of translations of common Actionscript 3 coding tasks to the Unity3D engine’s JavaScript-like UnityScript language.

Flash to Unity

1. Render an Object Visible or Invisible

Actionscript 3:

thing.visible = true; // or false

UnityScript:

thing.renderer.enabled = true; // or false

2. Tint an Object

Actionscript 3:

var colorTransform:ColorTransform = new ColorTransform();
colorTransform.color = 0xFF0000;
thing.transform.colorTransform=colorTransform;

UnityScript:

thing.renderer.material.color = Color.red;

3. Get a Random Number

Actionscript 3:

var someRandomNumberBetween0And5:int = Math.floor(Math.random()*5);

UnityScript:

var someRandomNumberBetween0And5:int = Random.Range(0, 5);

4. Respond to a Key Press

Actionscript 3:

 
stage.addEventListener(KeyboardEvent.KEY_DOWN, doKeyDown);
 
private function doKeyDown(e:KeyboardEvent):void
{
	switch(e.keyCode)
	{
		case 32:
			// Space bar! Do stuff.
			break;
		default:
			break;
	}
}

UnityScript:

if(Input.GetKeyDown("space"))
{
	// Do stuff.
}

5. Open a Web Page

Actionscript 3:

navigateToURL(new URL("http://www.untoldentertainment.com"), "_self");

UnityScript:

Application.OpenURL("http://www.untoldentertainment.com");

25 thoughts on “Five Common AS3 to UnityScript Translations

    1. Ryan

      Thanks, samBrown. The only snags i’ve hit have been a benign error message at the bottom of the screen whenever i exit Play mode, and a very important lesson about the different between “F” and CTRL+F (“F” centers the selected object in the view. i don’t know what CTRL+F does, but it messes up my object’s transform)

      Please let us all know if that book is worth the cashola! Next week, i’ll list the tutorials i’ve been following, and i’ll star the ones i think were worth the time.

      – Ryan

      Reply
  1. Joseph Burchett

    The funny thing about this type of comparison is that if you were to do it with AS2 instead of AS3 both scripting languages would look very similar! While with AS3 seems that Adobe has greatly complicated all these very simple things, odd… More reason for people who are intimated by AS3 and the new flash to just head on over to Unity I guess.

    Reply
    1. Ryan

      Joe – that’s one thing that really jumped out at me while i was putting this together. The AS3 examples involve quite a bit more typing. And i’ve even ommitted the necessary import statements, and didn’t include bits that required method declarations, like the keyboard example.

      i’m really enjoying the simplicity of UnityScript, and i get a little guilty shiver every time i drag n’ drop a piece of code onto an object … i keep worrying that an egghead will jump out from under my desk and throttle me for bad coding practices.

      In many ways, Unity3D makes me feel like i did when i was just starting out with Flash at version 4, back in 2000. There’s all kinds of stuff i want to do … i don’t know how to do most of it … but here i am, willing to keep straining at it until i figure everything out.

      – Ryan

      Reply
  2. Joseph Pasek

    Not bad, it’s always a good thing to record your thoughts when you’re learning something new. Posting them up allows other newcomers to the platform is definitely beneficial to others.

    You might want to note that when checking for input you want to do that within some sort of loop, in Unity being a MonoBehaviour’s Update() or FixedUpdate() methods, or (if you want precise control that isn’t checking *all* of the time) inside of a Coroutine.

    Cheers!

    Reply
    1. Ryan

      Thanks, Joseph P. As with the AS3 examples, i ommitted some of the surrounding code structure. You’re right – that input example should be inside one of the looping methods you mentioned. i have heard the term “Coroutine” in the IRC channel, but i don’t know what it means yet.

      Reply
  3. Joseph Burchett

    Agreed Ryan. One thing I have a fear of is that with all these new amazing tools coming out that we will start getting game developers who are so used to “scripting” and don’t really know how to do anything else when it comes to developing software. They will be so used to dragging and dropping code, that the whole idea of OOP, design patterns, all that good stuff will be forgotten. They will also have an excuse to because they have this excellent tool doing all the work for them… We will begin to be flooded with “dumb” game developers. I don’t know perhaps I am a bit to cynical about it all, but just a fear I have. Either way I am getting more and more excited for the day when I can really sit down and start digging into Unity, the comparison makes me even more happy to do it!

    Reply
    1. Ryan

      Joe B – i think we just have to stay a step ahead of the “dumb” devs *cough* FLASH *cough*. Once everyone jumps ship to Unity and messes the place up a few years from now, we’ll have our sites set on the next big thing.

      Reply
  4. Joseph Pasek

    A Coroutine is a type of loop where you can explicitly break up its functionality into multiple frames using yield commands. It is invaluable when you’re scripting something sequential like a transition, as you won’t need a cubic sh*t ton of booleans inside an Update() loop for early outs if it is something that only happens once. It keeps everything modular and logical.

    In fact, I think I just found my first topic for my first InfiniteUnity3D blog post. Thanks for that!

    Reply
    1. Ryan

      Joseph P. – good idea! But i’m still having trouble picturing it without a ferinstance. Can you give me a ferinstance in this case? (Of course, i can also sit tight and wait for your blog post :)

      Reply
  5. Pingback: Untold Entertainment on Infinite Unity3D » Blog Archive » Five Common AS3 to UnityScript Translations

  6. TJ

    First off, I think it’s great that you’re posting this kind of stuff. I’m hoping to eventually move on to Unity, and I could see resources like this being very useful in making the transition.

    Here’s a little more info on coroutines in Unity3D I found through google real quick:

    http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

    This is something I really wish AS3 had. There are two big uses I can think of immediately for this:

    1. When processing a large amount of data in Flash, the function will hold up everything else and essentially freeze your application. As it is now, it takes a bit of trickery to get any sort of function to work asynchronously. With coroutines, you could just add in a yield as part of a loop to fix it.

    2. You could use coroutines to hold up a function while waiting for some event. For example, you could make a WaitForKeyPress function which will infinitely loop until a key is pressed, then return, instead of dealing with states and event listeners which can get a little messy sometimes.

    I’m sure there are many more uses, but those are the two that immediately popped into my head that I think would be extremely useful.

    Reply
  7. Pingback: Posts about Actionscript 3 as of September 25, 2009 - Perry Multimedia Blog

  8. Brennon Williams

    Great post. The main reasons why I’m not in Unity are cost and, more importantly, I can’t draw in 3D, and already have a hard time with 2 dimensions. How are you guys getting your 3D models done?

    Reply
    1. Ryan

      Thanks, Brennon. As we mentioned in a recent post, our wonderful readers (maybe you?) helped us win a free copy of Unity core with Unity iPhone. Cost is definitely a concern when we go to buy a second copy, and then when we move to upgrade both suites to pro versions.

      We haven’t had to draw anything yet, but i’ve poked around a little in Blender. Once my hair had turned completely white from the horrendous UI, i started to see how i could get some use out of it. You can also buy rights-free content packs of 3D models to suit your needs, which we may explore. Or we’ll hire a 3D artist – i haven’t decided yet.

      – Ryan

      Reply
  9. Pingback: Five Common AS3 to UnityScript Translations

  10. Pingback: Five Common AS3 to UnityScript Translations

  11. Pingback: Scripting Unity 3D Game Engine Infinite List | cutienews

  12. Pingback: Weekly Digest for May 2nd — Hello. My name is Václav Van?ura.

  13. Pingback: Scripting Unity 3D Game Engine Infinite List

  14. Pingback: Scripting Unity 3D Game Engine Infinite List | Infinite Indie

  15. Pingback: Scripting Unity 3D Game Engine Infinite List | Marveloper Media

Leave a Reply

Your email address will not be published. Required fields are marked *