Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Making a 2D game for iPhone/iPad and need better performance?

Discussion in 'iOS and tvOS' started by stuartjeff, Nov 24, 2010.

  1. stuartjeff

    stuartjeff

    Joined:
    May 28, 2009
    Posts:
    8
    I'm using Unity and SpriteManager 2 to make a 2D game for the iPhone/iPad. Using the shader included with SM2 and/or the mobile vertex colored shaders included with Unity left me extremely GPU/fillrate bound on the iPad in particular. I was seeing anywhere from 50-90ms per frame for "cpu-waits-gpu" from the internal profiler running on the iPad. I wrote my own shaders and dropped this to an average of 10ms per frame. I've still got a few optimizations to go on my game but I figured I'd share the shaders I wrote in case anyone else out there might find them useful.

    The best use for these shaders is if you are making a sprite-based game where you layer the sprites and use alpha to create a more traditional 2D looking game. These shaders do not support scene lighting at all but they do support vertex coloring. Basically if you just want your textures to look pixel perfect and unaltered when rendered in game then these might help.

    Also, be sure to edit your AppController.mm to disable OpenGL ES 2.0 or your framerate is going to suffer tremendously. Line 70 of your AppController.mm should read:

    #define USE_OPENGLES20_IF_AVAILABLE 0

    Here is the shader that supports alpha transparency and vertex coloring:

    Shader "Unlit Transparent Vertex Colored" {
    Properties {
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }

    Category {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    ZWrite Off
    Alphatest Greater 0
    Blend SrcAlpha OneMinusSrcAlpha
    SubShader {
    Pass {
    BindChannels {
    Bind "Vertex", vertex
    Bind "texcoord", texcoord
    Bind "Color", color
    }

    Fog { Mode Off }
    Lighting Off
    SetTexture [_MainTex] {
    Combine texture * primary, texture * primary
    }
    }
    }
    }
    }

    Here is a shader that just renders your texture without any lighting or alpha transparency:
    Shader "Unlit" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader {
    Pass {
    SetTexture [_MainTex] {
    Combine texture
    }
    }
    }
    }
     
    Last edited: Nov 24, 2010
  2. ParaLogic

    ParaLogic

    Joined:
    Aug 19, 2010
    Posts:
    173
    Wow, you just helped me getting my game to run a solid 30 fps instead of 20 fps on the iPad by changing one lousy number! (disabling OpenGL ES 2.0) Thanks! Can't believe I didn't know about that one before!
     
  3. mklaurence

    mklaurence

    Joined:
    Dec 26, 2008
    Posts:
    104
    OpenGL ES 2.0 line didn't do much for me, but HOLY CRAP your shader doubled my framerate! Mega-uber-thanks! Unity should add this as an official "2D" shader... :)
     
  4. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    Wow!
    thanks, great share!

    I always assumed that 2.0 was turned off when we selected Arm6 (OpenGL es1.1) rather than the universal option.

    Are you saying even if we make that selection we should still change that line?
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    with ARMV6 you indeed have no OES 2 support in at all.

    But problem is it is getting harder and harder to get ARMV6 only to apple as apple wants 3GS and 4th gen opted apps and that means ARMV7. don't expect to send them non-armv7 opted stuff for long anymore if it still works at all I should say.
     
  6. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    I dont think I apple really looks under the hood that much when it comes to
    Approval.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    They look massively under the hood.
    Something like this though will normally show up on your end already as the uploader already checks basic requirements and refuses to upload stuff that fails to comply to basic requirements. (for example if you do an ARMV7 only and target anything but iOS4 and/or forget to set the requirement in the plist while its an iphone app, the uploader will refuse to pick it up at all)
     
  8. Daniel-Brauer

    Daniel-Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,317
    The problem with performance on ARMv7 should go away with Unity 3.2. Aras has put a lot of work into making sure that the right precision modifiers are used when compiling GLSL from Cg, and that the built-in shaders use appropriate precision everywhere.
     
  9. ashmira

    ashmira

    Joined:
    Oct 19, 2010
    Posts:
    18
    where can i add this code... after i add this it got errors..

    Shader "Unlit Transparent Vertex Colored" {
    Properties {
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }

    Category {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    ZWrite Off
    Alphatest Greater 0
    Blend SrcAlpha OneMinusSrcAlpha
    SubShader {
    Pass {
    BindChannels {
    Bind "Vertex", vertex
    Bind "texcoord", texcoord
    Bind "Color", color
    }

    Fog { Mode Off }
    Lighting Off
    SetTexture [_MainTex] {
    Combine texture * primary, texture * primary
    }
    }
    }
    }
    }

    Here is a shader that just renders your texture without any lighting or alpha transparency:
    Shader "Unlit" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader {
    Pass {
    SetTexture [_MainTex] {
    Combine texture
    }
    }
    }
    }


    the errors is

    invalid suffix "D" on integer constant
    invalid suffix "D" on integer constant
    expected constructor, destructor, or type conversion before string constant
     
  10. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    200
    I am currently using the Unlit shaders, where they recently added or why do you not use them?

    Are your shaders faster than the Unlit ones Unity provide?
     
  11. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    The original post was from 2010. Not really valid anymore as Unity 3.3 comes with new super optimised shaders in the Standard Assets (Mobile) bundle.
     
  12. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    200
  13. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    19,690
    why not test on device.
     
  14. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    200
    Just tested these shaders, it's still not really clear to me which to use.

    The above one works just like I hoped it would, it gives me an unlit shader that has support for vertex colors with alpha.

    In the standard mobile assets we get one that also handles this, but it has a bunch of extra options like shininess and so on. I wonder which one is the faster, I can't tell from testing currently.

    Also there doesn't seem to be any good shader in the mobile category for simple unlit transparent textures. There is one called background which doesn't support transparency and one called Transparent -> Vertex Color but that one adds the vertex colors on top of the texture.

    This is a bit confusing, what should I use? Right now I am using the Unlit Transparent and Unlit ones (not from the mobile category) and the nice shader for vertex colors posten on the top of this thread.

    Is there an official recommended shader for use on the iPhone with transparent textures + one for vertex colors with alpha (unlit)?