Jump to content

  • Log In with Google      Sign In   
  • Create Account

Gamieon's Journal

Unity Windows Phone 8 Facebook integration

Posted by , 30 June 2014 - - - - - - · 5,744 views

I wanted to add Facebook integration to Hamster Chase for Windows Phone 8 in an effort to have players get word out of the game through social media in exchange for a reward.

A search on Google led me to Chillster's Facebook SDK at http://forum.unity3d.com/threads/facebook-sdk-for-unity-plus-windows-phone-8.217907/ . It was free and also fairly new. I had a fair bit of trouble getting it to do what I wanted because of limitations I wouldn't accept, and because of my interpretations of the instructions. Ultimately I got it working. Here's a walkthrough I would have presented:

Facebook Setup

1. First you need to add your Windows Phone 8 app to your Facebook developer dashboard. Do this by going to https://developers.facebook.com/ and clicking on Apps at the top. If your app doesn't exist, use Create a New App and fill the proceeding form out.

2. If your app already exists (like you wrote your game for iOS/Android first), you still need to set it up. On the left side of the app's dashboard page on Facebook is a menu item for Settings. Click on it. Hidden beneath your existing platform listings is a "+ Add Platform" button. I swear I went to this page half a dozen times and never saw that button. Click on it and add your app product ID (which you get from your Windows Phone Dev Center account) to the second line. Remove the dashes and make sure you don't have curly brackets.

Phone Setup

I had to install the Facebook app on my phone before I could use Facebook integration.

Unity Setup

1. Add Chillster's Facebook SDK asset to your Unity project (or better yet make a test project).

2. Look up at your Unity menu bar (File, Edit, Assets...). There should now be a Facebook menu. Go there and choose "Edit Settings" and fill those out.

3. Now go to the link I mentioned at the start of this section (http://forum.unity3d.com/threads/facebook-sdk-for-unity-plus-windows-phone-8.217907/ ), and read Chillster's official documentation for getting set up starting with step 2.1. You've already done step 2.3, so you can skip that step.

Unity Usage

My goal was to make it so users could send app requests to friends which invited them to download my game. So when I found this in the code comments:

"... throw new UnityException("There is no Facebook AppRequest on Windows Phone 8"); ... "

I thought it was just an asset limitation. For a while I tried using FB.API() to get the job done starting with getting a friends list. I simply could not get that to work until I finally called "FB.API("me\friends"...)" (note the direction of the slash), and making sure to include the "user_friends" permission at the login. Then I tried a number of ideas to send an app request by using FB.API(); all failed. I ultimately settled for just doing a post on a player's feed by calling:

var wwwForm = new WWWForm();
wwwForm.AddField("link", "http://www.gamieon.com/hamsterchase");
wwwForm.AddField("linkName", "Hamster Chase for Windows Phone 8!");
wwwForm.AddField("linkCaption", myCaption);
FB.API("me/feed", Facebook.HttpMethod.POST, fb_OnFeedComplete, wwwForm);

It doesn't show the link name or caption in my feed; but I'm going to leave those in anyway in case they someday appear through either asset or Facebook-side updates.


I ultimately released Hamster Chase with Facebook integration. It works for posting to Facebook feeds, and it was nice to see it work without having to publish the game first (unlike other assets I integrated with). I'm sure it won't be long before future versions of Facebook integration assets are released with easier setups and more features; including app requests.

Porting Hamster Chase Part 2: Performance and Properties

Posted by , 25 June 2014 - - - - - - · 543 views

In a previous journal entry I described my experiences converting my Unity iOS/Android app to Windows Phone 8. Here I continue on about my experiences with combating app slowness as well as setting app properties from the generated VS 2013 project.

Transparency Performance Issues Resolved
Experience has taught me that iOS is efficient with transparent rendering; and Android not as much. When the Unity profiler reported that transparent rendering was very slow for Hamster Chase (~18ms per frame) in a very basic scene with a low polygon count, I was about to give up hope of porting any of my games entirely entirely until I discovered the problem:

I wrote a component that would smoothly fade meshes in and out. When they were invisible their renderers were enabled, but the main color's alpha value was set to zero. This turned out to be a huge performance killer as perhaps a quarter of the scene, consisting of prompts and menus, was this way.

Upon changing my component to disable the renderer when the alpha value was zero, the major performance problems went away. I still think it should have run faster regardless, but I digress.

Wondering how well another game of mine would perform, I converted and deployed a Master build of "Hyperspace Pinball" to my Windows Phone. It makes heavy use of particle systems and transparent rendering, so I expected it to get a low frame rate. After playing for just two minutes, I confirmed this to be so. It was a far cry from the very fast frame rates of every other pinball game I've ever played. I did not run the profiler or look into it further; perhaps someday I will.

Having eliminated the performance issue in Hamster Chase, all that was left to do before releasing it was to set up the splash screen, app icon, and app properties (app ID, permissions, etc.); and integrate third-party Facebook, In-App Purchase and Advertising assets.

Splash Screen and Icon
I was not able to configure the splash screen from my version of Unity, but I did find where it was located in the VS 2013 project. In the root of the project are five files:


As my game is landscape, I replaced the first three files with my splash screen image file. I made sure my image retained the same width, height, and orientation as the one I overwrote.

In the Assets folder I found ApplicationIcon.png, and replaced it with a png of the Hamster Chase icon with equal dimensions as well. After subsequent project builds from Unity, none of my changes got undone.

App Settings
Further poking around in the VS 2013 project led me to WMAppManifest.xml under the Settings folder. Here I found that I could set the app capabilities, requirements, product ID and publisher ID.

Those values also remained untouched by subsequent Unity builds.

Facebook, In-Apps and Ads
Integrating each third-party component to make Hamster Chase more feature-rich was a unique challenge. I plan to write about them in subsequent journal entries with the aim of making them helpful references to other developers.

Porting Hamster Chase to Windows Phone 8 (Unity 4.5)

Posted by , 01 June 2014 - - - - - - · 1,036 views

A couple weeks ago, a friend tipped me off to a free Microsoft Unity workshop that was held in Orlando, FL. Though reluctant at first, I decided to go. I needed to get out of my lone wolf developer cave, meet other folk, and see what Microsoft could do to help me port my mobile games to Windows Phone 8. Long story short, it was a good experience and I was on my way to getting those games ported.

Having released Hamster Chase for iOS and Android already, I figured getting it deployed to my Windows Phone would be a breeze...but it wasn't.

Upgrading the project to Unity 4

The first step was to make a branch of Hamster Chase for Unity 4. Right away I got a number of new compiler warnings; mostly about the GameObject.active setter being deprecated for GameObject.SetActive(). After fixing and re-testing all of the related code, I found another issue: in Unity 3.x, I would set certain objects to be "static" (motionless) objects at runtime when they wouldn't move, and then unset the static flag before they were to move again. I thought this would provide an optimization in rendering, and possibly with the physics too. In unity 4.5 at least, it would seem that once an object is static, it is always static and would never move again. I fixed this by simply never setting the static flag in the first place.

Those were the only two issues I dealt with during the upgrade process. With all the compiler warnings and static behavior fixed, I was ready to change the platform to Windows Phone 8.

Fun With Frameworks

After changing the platform, I attempted to build the project. Right away, I got errors related to my Prime31 Android & iOS plugins. Prime31 is an organization that develops plugins which enable developers to implement social network check-ins, ads and in-app purchases. It's a shame they also don't include a 'Monetization for dummies' manual with those plug-ins, but I digress. I wish I had retained the exact error messages for others to find on Google, but alas, I didn't think of it at the time. I ultimately fixed the errors by deleting all of the Prime31 plug-ins, and changing my side of the code to only look for them in the iOS and Android platforms.

My next build attempt gave me these more memorable error messages:
  • Error: 'WriteAllBytes' is not a member of 'System.IO.File'
  • Error: type `System.Xml.XmlDocument` doesn't exist in target framework.
  • Error: `System.Security.Cryptography.MD5CryptoServiceProvider` doesn't exist in target framework
There are three ways you can deal with these:
  • In Unity, go to File => Build Settings => Player Settings. In your Inspector window, expand Other Settings, and change your API Compatibility Level to .NET 2.0. (Note: I had already fixed System.IO and my MD5CryptoServiceProvider compatibility issues before trying this, but I think it should work)
  • Find an existing implementation that you can copy into your project.
  • Write your own code to replace the functions provided by those frameworks.
I opted to do 3 to keep the binary size at a minimum; of course that took a fair bit of time to do.

A "Successful" Build?

Attached Image

After fixing all that, I managed to get Hamster Chase to deploy to an emulator. I noticed the splash screen was of Unity and not from the game; nor was there a setting to change the splash screen in the Player settings. I hope to deal with that from the Visual Studio project itself later. I was happy...

...and then my happiness was shattered once I got it deployed onto my new Nokia Lumia 521. I was barely getting 15 FPS on the device! The game was choppy, overlays that should have faded in instead went straight to fully visible, and popup menu animations were even choppier than the game.

After attaching the Unity profiler to my phone via IP address (it takes like 45 seconds to connect), I pinned the main problem to a GameObject.FindObjectsOfType being called in every frame. After fixing that, the game was slightly faster. It was still far from the silky smoothness I see in most mobile games. The main culprit is now transparent rendering which is taking up over half the workload each frame. In the main menu alone it takes 18ms per frame. I had the same problem with Hyperspace Pinball; and it took months to optimize it just enough to even be releasable. I am not going through that again.

Thinking the problem was that the VertexLit shader was slow on mobile, I decided to try my hand at writing two transparent mobile shaders:

Unlit shader:
Shader "Mobile/Transparent/Unlit" {
    Properties {
        _MainTex ("Base (RGB) Transparency (A)", 2D) = "" {}
    SubShader {
        Pass {
            // Only render pixels with an alpha larger than 50%
            AlphaTest Greater 0.5
            SetTexture [_MainTex] 
            	combine texture
Simple lit shader:
Shader "Mobile/Transparent/Simple" {
    Properties {
        _MainTex ("Base (RGB) Transparency (A)", 2D) = "" {}
        _IlluminCol ("Self-Illumination color (RGB)", Color) = (1,1,1,1)
    SubShader {
        Pass {
            // Only render pixels with an alpha larger than 50%
            AlphaTest Greater 0.5
            SetTexture [_MainTex] 
            	constantColor [_IlluminCol]
            	combine texture * constant
After using these on the most prominent main menu objects, I didn't get a visible performance gain.

What's next?

It occurs to me that Hamster Chase was developed before Unity had Sprites and Sprite Renderers. In Hamster Chase, the existing "Sprites" are rendered using regular MeshRenderers and VertexLit shaders on simple four-corner squares. I think if I changed my "Sprites" to be actual Unity Sprites, things would render faster. I'll have to ponder if and how I would accomplish that because it could easily be a ton of work.

I still have trouble wrapping my head around the fact the game is this slow as is using out of the box shaders on such a sophisticated piece of equipment. It's notably faster on both iOS and Android; what am I missing here? I hope to find a solution and publish a follow-up journal entry on it.