DEV STUFF

Damien Pegoraro

Scaleform, Unity3D & FlashDevelop

scaleform

If you’ve ever tried building GUI with Unity3D, you may know how painful it can be.
Made by Autodesk, Scaleform gives you the ability to add a Flash layer to your 3D projet to make things easier.

Here’s a little walthrough to start creating GUI in a Unity + Flash Develop context.

This tutorial is meant to be as simple as possible and is focused on the workflow. No big code involved!

First of all, download an evaluation copy of Scaleform and unzip it somewhere. (give a valid mail to grab your licence key).

http://gameware.autodesk.com/scaleform/public/downloads

Scaleform Public SDK Downloads  Autodesk Gameware - Google Chrome

Create a new Unity3D project without using any default package.

Import Scaleform unitypackage to your project.

Assets -> Import package -> custom package -> your Scaleform_Unity_Integration-Evaluation.unitypackage

This operation may take a while. Grab a coffee.

If this error shows up, just forget it, we don’t care about 3DS max for now.

3ds Max could not be found.
Make sure that 3ds Max is installed and the max file has 3ds Max as its ‘Open with’ application!

Let’s try the sample scene:

Open Assets/scenes/main_level , don’t save your empty useless opened scene, and hit run.

Play with the demo to get a quick overview of the features :

– Sweet fonts, render textures, 3D-> flash -> 3D interactions …

Unity - main_level.unity - TutoScaleform - PC, Mac & Linux Standalone

My Unity was crashing at this point because of some DX11 issue. I just had to convert my project to DX9 :

Edit -> Project Settings -> Player -> untick use Direct3D 11

That’s cool, but what we’re really trying to achieve is building our first GUI from scratch.

Now let’s create our own scene named myfirstgui in Assets/scenes.

What we’re trying to achieve is a SWF gui with a button calling a Unity method.
This method will invoke some callback on our SWF.

First of all, create a C# script that will be the proxy for our SWF:

Assets/scripts/ScaleFormGUI.cs

using System;
using System.Collections;
using UnityEngine;
using Scaleform;

public class ScaleFormGUI : Movie {

protected Value theMovie = null;
private SFCamera parent = null;

public ScaleFormGUI(SFCamera parent, SFManager sfmgr, SFMovieCreationParams cp) :
base(sfmgr, cp)
{
this.parent = parent;
SFMgr = sfmgr;
this.SetFocus(true);
}

public void OnRegisterSWFCallback(Value movieRef)
{
Debug.Log("SWF REGISTERED") ;
theMovie = movieRef;
}

public void TestCallback()
{
Debug.Log("CALLED ") ;
theMovie.Invoke("fromUnity");
}

}

We put two callbacks : OnRegisterSWFCallback will be called when swf is ready, TestCallback will be called on mouse click.

We then have to write a Scaleform camera script :

using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.IO;
using System.Collections;
using Scaleform;

public class ScaleformCamera : SFCamera {

public ScaleFormGUI myGUI = null ;

new public IEnumerator Start()
{
// The eval key must be set before any Scaleform related classes are loaded, other Scaleform Initialization will not
// take place.
#if (UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR) && !UNITY_WP8
SF_SetKey("XDXSTZCND3W*************9TETWFON4J434KHRLA6DB96KV");
#elif UNITY_IPHONE
SF_SetKey("");
#elif UNITY_ANDROID
SF_SetKey("");
#elif UNITY_WP8
sf_setKey("");
#endif

InitParams.TheToleranceParams.Epsilon = 1e-5f;
InitParams.TheToleranceParams.CurveTolerance = 1.0f;
InitParams.TheToleranceParams.CollinearityTolerance = 10.0f;
InitParams.TheToleranceParams.IntersectionEpsilon = 1e-3f;
InitParams.TheToleranceParams.FillLowerScale = 0.0707f;
InitParams.TheToleranceParams.FillUpperScale = 100.414f;
InitParams.TheToleranceParams.FillAliasedLowerScale = 10.5f;
InitParams.TheToleranceParams.FillAliasedUpperScale = 200.0f;
InitParams.TheToleranceParams.StrokeLowerScale = 10.99f;
InitParams.TheToleranceParams.StrokeUpperScale = 100.01f;
InitParams.TheToleranceParams.HintedStrokeLowerScale = 0.09f;
InitParams.TheToleranceParams.HintedStrokeUpperScale = 100.001f;
InitParams.TheToleranceParams.Scale9LowerScale = 10.995f;
InitParams.TheToleranceParams.Scale9UpperScale = 100.005f;
InitParams.TheToleranceParams.EdgeAAScale = 0.95f;
InitParams.TheToleranceParams.MorphTolerance = 0.001f;
InitParams.TheToleranceParams.MinDet3D = 10.001f;
InitParams.TheToleranceParams.MinScale3D = 10.05f;

InitParams.UseSystemFontProvider = false;
return base.Start();
}

new public void Update()
{
CreateGameHud();

}

private void CreateGameHud()
{
if (myGUI == null)
{
myGUI = Util.CreateSwf("mygui.swf", true, new Color32(255, 255, 255, 0));
}
else
{
myGUI.SFMgr.ProcessCommands() ;
}

}
}

Replace your licence key and attach this script to your camera.

We now have to make our mygui.swf with a single button. So let’s create a new AS3 FlashDevelop project.

Here’s my Main.as :

package
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;

public class Main extends Sprite
{

private var s:Sprite;

public function Main():void
{
stage.scaleMode = StageScaleMode.NO_SCALE ;
stage.align = "TL" ;
init() ;
}

private function init():void
{
s = new Sprite() ;
addChild( s) ;

s.graphics.beginFill( 0x00FF00 ) ;
s.graphics.drawCircle( 200, 200, 50 ) ;
s.graphics.endFill() ;
s.addEventListener(MouseEvent.CLICK, callUnity ) ;

if ( ExternalInterface.available ){
ExternalInterface.call ("OnRegisterSWFCallback", this);
}
}

private function callUnity(e:MouseEvent):void
{
trace("click") ;
if ( ExternalInterface.available ) ExternalInterface.call("TestCallback") ;
}

public function fromUnity( ) : void
{
s.graphics.clear() ;
s.graphics.beginFill( Math.random() * 0xFFFFFF ) ;
s.graphics.drawCircle( 200, 200, 50 ) ;
s.graphics.endFill() ;
}
}

}

Just notice that calling a Unity method is the same thing as calling a Javascript method with ExternalInterface. Methods that will be called from Unity must be declared as public.

We made a code with a round button that calls a Unity method. This method calls flash again to change the button’s color to random.

Don’t forget to set the output in your Unity project StreamingAssets folder and build your swf.

Now we just have to run our Unity projet and if everything’s okay, clicking the green button should change it’s color.

You should see something like that:

Unity - myfirstgui.unity - TutoScaleform - PC, Mac & Linux Standalone_2

I added a cube on my scene just to show that there’s 3D running in the background…

Note that flash trace calls will be redirected to Unity debug console.

That was pretty easy! Tell me if you read something unclear or wrong!

1,450 Comments on “Scaleform, Unity3D & FlashDevelop

  1. [url=http://1statenolol.com/]atenolol 25mg[/url] [url=http://1stlevitra.com/]mail order levitra[/url] [url=http://acyclovirbest.us.org/]acyclovir[/url] [url=http://tadalafilbest.us.com/]tadalafil prices[/url] [url=http://1stlasix.com/]buy lasix online cheap[/url] [url=http://xenicaloffers.us.org/]xenical[/url] [url=http://1stlexapro.com/]lexapro[/url] [url=http://mobicbest.us.com/]mobic 15mg[/url] [url=http://1stlisinopril.com/]lisinopril price[/url] [url=http://azithromycinbest.us.com/]more info[/url]

  2. [url=http://1stsynthroid.com/]buy levothyroxine online[/url] [url=http://1sttetracycline.com/]Tetracycline[/url] [url=http://1stlevitra.com/]buy cheap levitra online[/url] [url=http://tadalafilbest.us.com/]tadalafil best price[/url] [url=http://cafergotbest.us.org/]cafergot online[/url] [url=http://levitrabest.us.org/]levitra 5mg[/url] [url=http://azithromycinbest.us.org/]azithromycin[/url] [url=http://cheapestsildenafilonline.com/]sildenafil 100[/url] [url=http://nolvadexbest.us.org/]Nolvadex[/url] [url=http://cialisoffers.us.com/]cialis professional 20 mg[/url]

  3. [url=http://acyclovirbest.us.org/]aciclovir cream[/url] [url=http://xenicalbest.us.org/]more info[/url] [url=http://cialisoffers.us.com/]CIALIS USA[/url] [url=http://1sttetracycline.com/]Tetracycline Cream[/url] [url=http://1stsynthroid.com/]synthroid[/url] [url=http://inderalbest.us.com/]buy inderal no prescription canada[/url] [url=http://1stdoxycycline.com/]doxycycline[/url] [url=http://1stprozac.com/]PROZAC[/url] [url=http://tadalafilbest.us.com/]tadalafil tablets 20mg[/url] [url=http://1stlisinopril.com/]lisinopril[/url]

  4. payday loans illinois [url=http://myloanusa.com/]website link[/url] In our present economic climate, many people require cash money quickly. There is so much that goes into a financial scenario, as well as lots of people seek to cash advance for assistance. Maintain reading this post for handy tips concerning this subject if you’re consider this kind of option. original site

  5. [url=http://cafergotbest.us.org/]cafergot[/url] [url=http://paxilbest.us.org/]Paxil Breast Cancer[/url] [url=http://1stsynthroid.com/]Synthroid[/url] [url=http://1stprednisone.com/]prednisone[/url] [url=http://levitrabest.us.org/]discount levitra[/url] [url=http://1stprednisolone.com/]prednisolone[/url] [url=http://inderalbest.us.com/]inderal[/url] [url=http://retinabest.us.com/]online retin-a[/url] [url=http://1stlevitra.com/]order levitra online[/url] [url=http://1stdoxycycline.com/]doxycycline[/url]

  6. Woman of Alien Fantastic perform you might have accomplished, this page is really amazing with amazing facts. Time is God as strategy for holding almost everything from occurring at once.

  7. There is obviously a bunch to identify about this. I believe you made some good points in features also.

  8. Normally I don at read article on blogs, however I would like to say that this write-up very compelled me to check out and do so! Your writing style has been amazed me. Thanks, quite great article.

  9. [url=http://1stlisinopril.com/]continue reading[/url] [url=http://1stzithromax.com/]zithromax pfizer[/url] [url=http://nexiumbest.us.org/]nexium[/url] [url=http://1stprednisone.com/]prednisone pill[/url] [url=http://acyclovirbest.us.org/]ACYCLOVIR[/url] [url=http://synthroidbest.us.org/]synthroid[/url] [url=http://phenerganbest.us.com/]PHENERGAN 50 MG[/url] [url=http://1stdoxycycline.com/]doxycycline 100mg online[/url] [url=http://1statenolol.com/]atenolol[/url] [url=http://levitrabest.us.org/]Levitra 5mg[/url]

  10. [url=http://1stmetformin.com/]buy metformin on line[/url] [url=http://1stsynthroid.com/]synthroid[/url] [url=http://cafergotbest.us.org/]buy cafergot[/url] [url=http://diflucanbest.us.com/]diflucan 200 mg[/url] [url=http://1stprozac.com/]prozac[/url] [url=http://sildenafilbest.us.org/]sildenafil citrate[/url] [url=http://1stfurosemide.com/]website[/url] [url=http://xenicaloffers.us.org/]xenical pill[/url] [url=http://1stlasix.com/]lasix water pill[/url] [url=http://azithromycinbest.us.com/]buy azithromycin[/url]

  11. Way cool! Some very valid points! I appreciate you writing this post and the rest of the site is also really good.

  12. Very interesting information!Perfect just what I was searching for! It as the Brady Act taking manpower and crime-fighting capability off the streets. by Dennis Martin.

  13. [url=http://nexiumbest.us.org/]order nexium[/url] [url=http://1stacyclovir.com/]acyclovir[/url] [url=http://phenerganbest.us.com/]phenergan vc[/url] [url=http://1stlevitra.com/]as example[/url] [url=http://tadalafilbest.us.com/]tadalafil[/url] [url=http://retinabest.us.com/]buy retin a online[/url] [url=http://1stallopurinol.com/]allopurinol 300 mg[/url] [url=http://levitrabest.us.org/]levitra cost[/url] [url=http://diflucanbest.us.com/]diflucan fluconazole[/url] [url=http://xenicalbest.us.org/]xenical price[/url]

  14. Thank you a lot for providing individuals with an extraordinarily memorable chance to read articles and blog posts from this web site. It’s always so ideal and also jam-packed with a lot of fun for me and my office co-workers to search your site at least thrice in a week to find out the new issues you have got. And definitely, I am also certainly fascinated with your great knowledge you serve. Some two ideas in this posting are in fact the best we have all ever had.

Leave a Reply

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