This member has provided no bio about themself...

RSS My Blogs


Why

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
UTOOLSSystem.h

#pragma once

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

UTOOLSSystem.cpp

#include "stdafx.h"
#include "UTOOLSSystem.h"
#include <windows.h>
#include <VersionHelpers.h>

__declspec(dllexport) int GetCPUType(){
 _SYSTEM_INFO info;
 GetNativeSystemInfo(&amp;info);
 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(){
 _SYSTEM_INFO info;
 GetNativeSystemInfo(&amp;info);
 return info.dwNumberOfProcessors;
}

Unity

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 
  {
   ARM,
   ALPHA,
   ALPHA64,
   AMD64,
   IA32_ON_WIN64,
   IA64,
   INTEL,
   MIPS,
   MSIL,
   NEUTRAL,
   PPC,
   SHX,
   UNKNOWN
  }
 
  public static CPU[] cpus=new CPU[]
  {
   CPU.ARM,
   CPU.ALPHA,
   CPU.ALPHA64,
   CPU.AMD64,
   CPU.IA32_ON_WIN64,
   CPU.IA64,
   CPU.INTEL,
   CPU.MIPS,
   CPU.MSIL,
   CPU.NEUTRAL,
   CPU.PPC,
   CPU.SHX
  };
  
  
  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.

Download

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.

Download

Also posted here

Start a group Groups
Celestial Games

Celestial Games

4 members Developer & Publisher

Celestial is a South African game development studio. Formed in 1994. We successfully published 2 PC titles and then took a little break. Recently re-opened...

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account:

X