This member has provided no bio about themself...

Latest Media

We're sorry, but no images, videos or audio files have been added to this gallery.

If you would like to share media with the community, sign up and you can.

Blog RSS Feed Report abuse Latest Blog: Determine CPU and Count in Unity.

0 comments by travisbulford on Jan 20th, 2014


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

cpp code:
#pragma once

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

 UTOOLSSystem.cpp

cpp code:
#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

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
  {
   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

Groups
Celestial Games

Celestial Games

Developer & Publisher with 4 members, must apply to join

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
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

Level
Avatar
Avatar
Offline Since
Jun 3, 2014
Country
South Africa South Africa
Gender
Male
Member Watch
Track this member
Statistics
Activity Points
91
Rank
22,596 of 468,647
Watchers
0 members
Time Online
1 second
Comments
5
Site Visits
287
Profile Visitors
1,112 (1 today)
Contact
Private Message
Send Now
Email
Members Only
Nico_Schoeman
Nico_Schoeman friends since May 30, 2013
FyreWolfe
FyreWolfe friends since Dec 6, 2012
Chanel.Fortune
Chanel.Fortune friends since Dec 10, 2012
PLYoung
PLYoung friends since Jun 27, 2013
JuandreSwart
JuandreSwart friends since Dec 4, 2012