550 likes | 757 Views
Deep dive: Tips & tricks for porting games from other platforms to Windows 8. Randy Spong Field Engineer Unity Technologies . The Goal. Understand the work needed to bring your existing Unity project over to Microsoft’s Windows Store for Windows 8 and Windows RT devices. The Goal.
E N D
Deep dive: Tips & tricks for porting games from other platforms to Windows 8 Randy Spong Field Engineer Unity Technologies
The Goal Understand the work needed to bring your existing Unity project over to Microsoft’s Windows Store for Windows 8 and Windows RT devices
The Goal Understand the work needed to bring your existing Unity project over to Microsoft’s Windows Store for Windows 8 and Windows RT devices Knowing the unique features & behaviors of Windows 8/RT and Unity for Windows Store Apps, you’ll be better prepared to estimate the amount of work required to port your project
The Goal Understand the work needed to bring your existing Unity project over to Microsoft’s Windows Store for Windows 8 and Windows RT devices Knowing the unique features & behaviors of Windows 8/RT and Unity for Windows Store Apps, you’ll be better prepared to estimate the amount of work required to port your project Your knowledge of the changes to the runtime APIs for WindowsStore Apps will make your porting work go more quickly
The Goal Understand the work needed to bring your existing Unity project over to Microsoft’s Windows Store for Windows 8 and Windows RT devices Knowing the unique features & behaviors of Windows 8/RT and Unity for Windows Store Apps, you’ll be better prepared to estimate the amount of work required to port your project Your knowledge of the changes to the runtime APIs for WindowsStore Apps will make your porting work go more quickly Your understanding of performance hotspots and optimization techniques will help you extract maximum performance from Windows 8 and Windows RT devices
The Goal Understand the work needed to bring your existing Unity project over to Microsoft’s Windows Store for Windows 8 and Windows RT devices Knowing the unique features & behaviors of Windows 8/RT and Unity for Windows Store Apps, you’ll be better prepared to estimate the amount of work required to port your project Your knowledge of the changes to the runtime APIs for WindowsStore Apps will make your porting work go more quickly Your understanding of performance hotspots and optimization techniques will help you extract maximum performance from Windows 8 and Windows RT devices
Agenda Overview of Windows 8 and Windows RT Overview of Unity’s Windows Store add-on Porting Existing Code Optimizing Performance Best Practices for porting your Unity project
Hardware Landscape (Slide provided courtesy of Microsoft, 2012)
New Category of Windows PCs • New design point • Always on, always connected • System on a chip • Battery is primary power source • Focus on low power • Both x86/x64 and ARM-based systems • Covers a range of form factors (Slide provided courtesy of Microsoft, 2012)
Windows 8/RT Device Capabilities • Windows 8 & RT devices scale in performance from phone-equivalent all the way to high-end multi-GPU gaming desktop • Touch is a first-class citizen • Minimum device resolution is 1366x768 • Windows RT has a minimum DirectX feature level of 9_1 • May not have 4k texture support • May not have simultaneous render targets
Differences • Unity’s Windows Store runtime is built on .NET • Unity apps for Windows Store can only consume WinRT Components (no unmanaged DLLS) • Many new Windows devices have a trackpad and touchscreen that can be used simultaneously
Other Things to Watch Out For • No 64-bit Windows Store Apps just yet • Boo, JavaScript not fully implemented • Network classes not supported (WWW is fully implemented, though) • Cloth not supported • Microphone not implemented
Other Things to Watch Out For, Continued • Animation of script variables is not allowed • AnimationEvent callback functions with arguments (Supported: a function with no arguments or with AnimationEvent argument) • GameObject.SendMessage - The function argument types the message receiver must exactly match the message (we don’t have type conversion) • No fog for devices with DX feature level less than 9.3 • Example workaround at http://files.unity3d.com/tomas/Metro/Examples/MyCustomFog.shader
Things to Get Excited About • C# debugging in Visual Studio • Building the Unity project generates a tweakable Visual Studio project • Simplifies native code plugin integration • Your ported code pretty much ‘just works’ on Windows Phone 8
Overview • Disallowed Windows APIs • Sharing plugin code between the Editor and your Windows Store App
Disallowed Windows APIs • HashTable • ArrayList • System.Xml.XmlDocument • System.Threading.Thread • And a few thousand Win32 functions
Disallowed Windows APIs HashTable Use a Dictionary • Better performance for value types • Not necessarily thread-safe • Type-constrained
Disallowed Windows APIs ArrayList Use a Dictionary or a List<T> • Possibly a small loss of performance
Disallowed Windows APIs System.Xml.XmlDocument Don’t use XML • But if you have to… use System.Xml.Linq • Better is to use JSON or YAML
Disallowed Windows APIs System.Threading.Thread Use ThreadPools http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.threadpool
Disallowed Windows APIs Win32 APIs Winsock2, CreateThread, HeapCreate, Sleep, etc. (SuspendThread, GetThreadContext, SetUnhandledExceptionFilter are the ones that really threw us for a loop) Find Windows 8/RT -compatible alternatives athttp://msdn.microsoft.com/en-us/library/windows/apps/hh464945.aspx
Plugins in Windows Store Apps Windows Store Apps can only consume WinRT Components • Unmanaged legacy native code DLLs aren’t allowed Check out the details at http://msdn.microsoft.com/en-us/library/windows/apps/hh441569.aspx The Unity Editor is still based on Mono • Make sure your WinRT Component can also be built as a legacy native code DLL (Windows Store functionality can be stubbed out)
Component Caching Interop in Unity for Windows Store Apps is expensive Minimize the Unity APIs you call each frame, including Component references
Component Caching Bad public class example : MonoBehaviour { void Update() { transform.Translate(0, 0, 5); } }
Component Caching Good public class example : MonoBehaviour { private TransformmyTransform; void Awake() {myTransform= transform; } void Update() {myTransform.Translate(0, 0, 5); }}
Component Caching Terrible public class example : MonoBehaviour { void Update() { for (inti = 0; i < 1000; ++i) { transform.Translate(0, 0, transform.position.z + 0.005f); } } }
GameObject Pooling Instantiating and Destroying GameObjects is expensive • Reuse when possible!
GameObject Pooling Instantiating and Destroying GameObjects is expensive • Reuse when possible! Instead of Destroying: gameObject.SetActive(false); MyPoolManager.AddToFreePool(gameObject); Instead of Instantiating: GameObjectgameObject = MyPoolManager.GetFreeObject(); SetupObjectDefaults(gameObject); gameObject.SetActive(true);
Draw Calls - Dynamic Batching • Works automatically • Maximum of 900 vertex attributes per mesh • Differently scaled objects won’t batch together • Dynamic Batching incurs some CPU overhead at runtime
Draw Calls - Static Batching • Supports arbitrarily complex geometry • Can significantly reduce CPU usage at runtime for setting up draw calls • Objects cannot move, rotate, or scale • Objects must be marked as static in Unity • Uses lots of device memory
Draw Calls - Texture Atlasing • Batching only works when objects share materials • Combine object textures into a Texture Atlas
Unity Features on Windows RT Don’t Use • Desktop shaders • Terrains • Realtime shadows • Dense particles • Non-tessellated sprites
Sprite Meshes (Images provided courtesy of Bento Studio [Uni2D], 2013)
Use Our Documentation The Unity documentation is full of concise and useful optimization advice Practical Guide to Optimization for Mobiles Optimizing Graphics Performance Mobile Developer Checklist – Optimizations
Animated Loading Screen • Camouflages your loading times • Gives the user a sense of progress
Target Device Testing • Regularly use different devices for development testing • Including the crappy ones
Stay Compliant • Run the WACK tool regularly • Aim for daily runs when you’re refactoring for Windows 8 support or laying down new .NET code
Recap - Windows 8 and Windows RT • Windows 8 and RT are mobile operating systems • Touch is a first-class citizen • Broad range of form factors • Broad range of performance capabilities
Recap – Unity for Windows Store • Need to handle both trackpad and mouse input in the same app • Native code plugins have to be rewritten as WinRT Components • The Editor can’t consume WinRT Components (need to use a version of the com
Recap – Porting Existing Code • Convert usage of HashTable and ArrayList to Dictionary • Rewrite native code plugins as WinRT Components • Keep a native code version (which stubs Windows Store functionality) for using in the Editor’s Play Mode