How To Incorporate C# from Xamarin into Unity3D?

Hi, My friends and I are working on an Android Apps. He had pass me a C# written in Xamarin and ask if I can used it in Unity3D.

I would like to ask is that possible?

Sample of C# code from Xamarin as below:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Bluetooth;
using Android.Content;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System;

namespace bScan
{
[Activity(Label = “bScan”, MainLauncher = true, Icon = “@drawable/icon”)]
public class cpdActivity : Activity, BluetoothAdapter.ILeScanCallback
{
private TextView proximityInfo;
// bluetooth scan related variables
private Handler scanHandler;
private BluetoothManager myBluetoothManager;
private BluetoothAdapter myBluetoothAdapter;
private int scanPeriod = 1000; //default;

// bluetooth data processing variables
private List beaconScreenName = new List();
private List beaconRSS = new List();
private List beaconDetectedTime = new List();

Unity uses C# as well, so translating an app from Xamarin to Unity is less work than, say, porting something from Unreal to Unity.

That said, both IDEs use their own APIs for a lot of the actual work you see, so you’re probably looking at rewriting the entire thing, still. For example, Android and its (presumably) included classes of BluetoothManager and BluetoothAdapter don’t exist in Unity out of the box (at least, not in the way they’re being used in Xamarin) so you’ll need to find replacements for things like them. But stuff like those last 3 lines will be able to be carried over exactly as is.

It’s not a trivial matter, but absolutely possible from a code perspective.

The other side of it is the UI of your applications. Xamarin and Unity use entirely different systems, and all your UI (and UI-related code) will need to be recreated from scratch.