Mochi Ads for Unity3D 3.5.7 & 4 Flash Games

UPDATE: Yes, an update before the bridge and this article are even released… Some time ago, Unity decided to stop selling the Flash add-on. So this article is now only for those who have already bought the flash add-on for Unity 4 or use the beta version in Unity 3.5.7.

Pure love

Since the release of Unity 4 earlier this year, the Flash exporting function of Unity came out of beta to become a full-featured add-on. During the beta period, I’ve created a bridge to enable MochiAds integration into a Unity flash game. That original bridge and article can be found here, but isn’t compatible with the current build of Unity anymore. Besides that, Mochi Media hasn’t been sleeping either and introduced their new Mochi API 4.1.1. Reason enough to update the bridge. Now because of the amount of changes in the Mochi API (new CPA ads etc.) and the major changes in the Unity Flash exporter, this update needed some redesigning. And since both Mochi and Unity are awesome, the cupid inside of me made the love just a little bit bigger by adding support for more features than in the first bridge version (like inter-level ads and lots of callbacks).

 

Let’s get ready

First you’ll need to download Unity, FlashDevelop and the bridge files: Awesomeness

Ready! Next please!

So you’re one step closer to awesomeness? Then lets not waste any more time and get Mochi Ads integrated intro your next big hit game!

First, set up your game in your Mochi Ads Dashboard

  • If you don’t have a Mochi Ads account yet, sign up here.
  • Add the game to your account.
  • Once added, you can create leader boards, awards etc..

Second, let’s prepare your game in Unity3D

  • Import the MochiUnityBridge_v0.9.unitypackage file (Assets->Import Package->Custom Package)
  • In your FIRST scene, drag the MochiObject prefab (from the prefabs folder) to your hierarchy
  • *Optionally, you can set the Award notice in the inspector when you’ve selected the MochiObject*Format can be: “ShortForm”, “LongForm” or “NoForm”Alignment can be: “ALIGN_TL”, “ALIGN_T”, “ALIGN_TR”, “ALIGN_L”, “ALIGN_C”, “ALIGN_R”, “ALIGN_BL”, “ALIGN_B”, “ALIGN_BR”. For Top Left, Top, Top Right, etc.
  • You only have to call the methods to the API from here on! Check the list of functions below.
  • Build the game (File -> Build Settings)*Make sure all the scenes you use are added to the build and the selected platform is flash. Uncheck “development build” and set the Flash version to 11.2 or higher (I suggest a minimum of 11.2 due to more supported features and video cards). Also keep in mind that the Mochi Live Updates wrapper might run a little behind (you can check the latest supported version in the drop-down when uploading a game using Live Updates, currently up to FP 11.5 is supported)

Next, making a build with FlashDevelop

  • Open FlashDevelop and create a new project (Project -> New project -> AS3 project)
  • Set the project properties (Project -> properties) to the Flash Player version you also used to export you game with in Unity and your game dimensions.
  • Add the with-Unity-created .swf file to the “src” folder.
  • Add the embeddingapi.swc (UnityShared.swc in Unity 3) file to the “lib” folder and add it to the library (In FlashDevelop- right click -> Add to Library)
  • Download the latest Mochi API from your dashboard.
  • Add the mochi folder to the “lib” folder. Right click on the “lib” folder and check “Source Path”.
  • Replace Main.as in the “src” folder with the Main.as from the resources downloaded earlier.
  • In this new Main.as change MOCHI_GAME_ID to your mochi game id.
  • Also in this Main.as change GAME_NAME.swf to your games swf name (should be the same as in the “src” folder)
  • Add MochiStuff.as from the resources to the “src” folder
  • Create 3 images in the games dimensions that should be shown 1. while loading, 2. If wmode is not direct, 3. if Stage3D is not being used. Add these images to your “src” folder with the names: “loading.jpg”, “wmode_not_direct.jpg” and “no_stage3d.jpg”
  • Build your project! (set configuration to “Release” then go to Project -> Build Project).
  • The game should now have been published to the “bin” folder. You can upload this .swf file in your MochiAds dashboard! Make sure to use live updates if you want to use a pre-game ad.
  • Ad “Stage3D” as a keyword in the game settings. This will make sure the game shows up as a Stage3D enabled game in the feeds and will set the mode to direct on Mochi’s game page.

 

Do you have a demo of how this stuff works?

Of course! See the little play button in the image below? Click it!

More demo’s, I want more demo’s!

Alright then, here’s a demo video.

Stage3D

Keep in mind that all Flash games created with Unity3D use Stage3D, therefore the game HAS TO BE embedded with the wmode set to direct. Most portals don’t do this yet, which is obviously bad for distribution. Don’t forget to use “Stage3D” as a keyword on your Mochi Ads game settings page.

I did it! Hooray!

That’s awesome. Please share your game with us on the forums!

I tried, but your bridge sucks!

Didn’t get it to work? Let me know on the forums and I’ll try to help!

Available MochiAds methods

// Log a gameplay MochiObject.startPlay();

// Log a gameplay end MochiObject.endPlay();

// Show a leader board MochiObject.showLeaderBoard(string leaderBoardID);

// Send a score to a leader board MochiObject.sendScore(string leaderBoardID, int score);

// Send a score to a leader board with a player name MochiObject.sendScoreWithName(string leaderBoardID, int score, string playerName);

// Show awards MochiObject.showAwards();

// Unlock an award/achievement MochiObject.unlockAchievement(string achievementID);

// Show a click-away ad at position x, y MochiObject.showClickAwayAd(int xpos, int ypos);

// Hide the click-away ad MochiObject.hideClickAwayAd();

// – X AND Y: This represents an X, Y offset from the center. Default: 0

// – adCount: The number of ads to display in the showcase. Default: 5

// – adSize: Force dimensions for all Ads displayed. Default: 90

// – userClose: Allow the use to close the Showcase ad. Default: true MochiObject.loadShowCaseAd(int x, int y, int adCount, int adSize, bool userClose)

// Close the showcase ad MochiObject.closeShowCaseAdGroup();

// Open the closed showcase ad MochiObject.openShowCaseAdGroup();

// – position: Position of the Dock ads. Default: “bottom”. Options are: “top”, “bottom”, “left”, “right”, “top_left”, “top_right”, “bottom_left”, “bottom_right”, “left_top”, “left_bottom”, “right_top”, “right_bottom”.

// – adCount: The number of ads to display in the showcase.

// – adSize: Force dimensions for all Ads displayed. MochiObject.loadDockAd(string position, int adCount, int adSize);

// Close the dock ad MochiObject.closeDockAdGroup();

// Open the closed dock ad MochiObject.openDockAdGroup();

// Slide the dock ad open MochiObject.openDockSlider();

// Show an interlevel ad. showBackGround default: true MochiObject.showInterLevelAd(bool showBackGround);

 

Callback functions

There are also a bunch of callback functions available, such as for when the interlevel ad is finished, when a user closes a showcase ad, etc. For the complete list of functions and to use them, you can find them in MochiObject.cs