This member has provided no bio about themself...

Report article RSS Feed Determine CPU and Count in Unity.

Posted by travisbulford on Jan 20th, 2014


OK as an introduction let me start by explaining why we want to identify the CPU in Unity. This is predominately for Windows Store but its quite passable it will be useful later as platform architectures expand.
We are close to releasing our game Toxic Bunny HD on the Windows Store and Phone. Due to a technical hiccup outside our control we have found ourselves with a little more time then we expected. With that time we have been dreaming up improvements to make the project better.
When we moved the game to the Windows Store obviously we had to target the lowest common denominator; the ARM CPU's on tablets. To achieve this we have had to remove a lot of things such as varlet managed ropes and slime. As well as some lighting and other cool effects that work perfectly on a desktop Windows 8 machine.
Naturally with enough time on our hands we decided we want those features back and the first thing we had to do was identify the CPU the game is running on. We could of course create 2 completely different Unity projects but that will lead to unpleasant version control.

The Meat.

So we decided to create a c++ DLL that can call the Windows function GetNativeSystemInfo this will return the CPU type as well as the number of cores which for now we will just consider to be the number of cpu's. For our immediate need we could see no use to the number of cores, but for the tiny little bit of effort we included them anyway.

Visual Studio

See below the code for the .h and .cpp files

cpp code:
#pragma once

extern "C" {
 __declspec(dllexport) int GetCPUType();
 __declspec(dllexport) int GetCPUCount();


cpp code:
#include "stdafx.h"
#include "UTOOLSSystem.h"
#include <windows.h>
#include <VersionHelpers.h>

__declspec(dllexport) int GetCPUType(){
 if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM){
  return 0;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ALPHA){
  return 1;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ALPHA64){
  return 2;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64){
  return 3;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA32_ON_WIN64){
  return 4;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64){
  return 5;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL){
  return 6;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_MIPS){
  return 7;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_MSIL){
  return 8;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_NEUTRAL){
  return 9;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_PPC){
  return 10;
 else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_SHX){
  return 11;
 return -1;

__declspec(dllexport) int GetCPUCount(){
 return info.dwNumberOfProcessors;


csharp code:
using UnityEngine;
using System.Runtime.InteropServices;

namespace UTOOLS

 public class System
  [DllImport ("UTOOLSSystem")]
  private static extern int GetCPUType ();
  [DllImport ("UTOOLSSystem")]
  private static extern int GetCPUCount ();
  public enum CPU
  public static CPU[] cpus=new CPU[]
  public static CPU CpuType()
   int cpu=GetCPUType();
   if (cpu>=0 &amp;&amp; cpu<cpus.Length)
    return cpus[cpu];
   return CPU.UNKNOWN;

  public static int CpuCount()
   return GetCPUCount();

Other things needed.

OK so that's pretty much the operating version. The methods called are guaranteed to work on Windows Phone too but for our version we created empty methods that return ARM and 1 CPU for the Windows Phone. We have 3 versions of the DLL added one in Plugins one in Plugins\WP8 and Plugins\Metro. For this version the base and Metro are identical.
Down the line we will split the solution up to work on MAC, iOS, Android and so on. But since the intimidate need was resolved we leaving it here.
One downside is you will need Unity Pro to use the DLL's I believe the feature of importing a DLL from c++ is a pro one.


It is our intention to turn this into a free Unity asset on the asset store when we get the time to package it properly. But in the mean time here is a download link for anyone that could use.


Also posted here

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Offline Since
Jun 3, 2014
South Africa South Africa
Member Watch
Track this member
61 (1 today)
Report Abuse
Report article