Obfuscate your C# code in Unity3d

In this article I’m writing about how you can obfuscate your C# code in Unity3d. As you probably already know your C# script code is stored in a .dll file and that file can easily be decompiled and read just like an open book. It is pretty much impossible to prevent anyone to reverse engineer your code, anyone who got your application will be able to do it. However you can make it harder for someone to read your code by obfuscate it.

If you don’t know what obfuscation is then you can read about it here and source can be downloaded here.

Now to be honest, for most apps out there it will be a waste of time since it is highly unlikely that someone will look at your code and try to hack/steal it. And remember, if someone really wants to do it, they will do it, it is all just a matter of time.

So you still want to obfuscate your code and you may wonder, why is it so difficult? There are so many great programs out there to obfuscate .net code.

void Update()
{
//Some code
}
void T20()
{
//Some code
}
public class BahaviorEventBinder : MonoBehaviour
{
public event UpdateExecutedDelegate OnUpdateExecuted;
public delegate void UpdateExecutedDelegate();
public event OnEnableDelegate OnEnableExecuted;
public delegate void OnEnableDelegate();
public event OnDisableDelegate OnDisableExecuted;
public delegate void OnDisableDelegate();
void Update()
{
if (OnUpdateExecuted != null)
{
OnUpdateExecuted();
}
}
void OnEnable()
{
if (OnEnableExecuted != null)
{
OnEnableExecuted();
}
}
void OnDisable()
{
if (OnDisableExecuted != null)
{
OnDisableExecuted();
}
}
}
public class MyMonoBahavior : BahaviorEventBinder
{
public MyMonoBahavior()
{
OnEnableExecuted += MyMonoBahavior_OnEnableExecuted;
OnUpdateExecuted += MyMonoBahavior_OnUpdateExecuted;
OnDisableExecuted += MyMonoBahavior_OnDisableExecuted;
}
private void MyMonoBahavior_OnEnableExecuted()
{
// On enable code goes here
}
private void MyMonoBahavior_OnUpdateExecuted()
{
// On update code goes here
}
private void MyMonoBahavior_OnDisableExecuted()
{
// On disable code goes here
}
}
public class MyMonoBahaviorRegistrator
{
public void Registrate()
{
var gm = Resources.Load<GameObject>(“MyPrefab”);
var s = gm.GetComponent<MyMonoBahavior>();
if (s == null)
{
s = gm.AddComponent<MyMonoBahavior>();
}
}
}
public class Scene1Controller : BahaviorEventBinder
{
public Scene1Controller()
{
OnEnableExecuted += Scene1Controller_OnEnableExecuted;
}
private void Scene1Controller_OnEnableExecuted()
{
new MyMonoBahaviorRegistrator().Registrate();
}
}

Source

Can be downloaded here.

BTC

1CGu9Ctt1AuyXiWMJ2nEDoH1RRAKtStdjx

ETH

0xd2291b554075da7f61210db2648a7f0a2d006190

Kristoffer Karlsson

Written by

Software developer and indie game developer from Sweden. Been turning coffee into code since 2010.