Jump to content

  • Log In with Google      Sign In   
  • Create Account






Porting Hamster Chase to Windows Phone 8 (Unity 4.5)

Posted by Gamieon, 01 June 2014 · 283 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.




I hope you are able to fix your slowing issue. Looking forward to hear the cause.

PARTNERS