A quick and simple steps to create native unity plugins with rust lang.
- Create rust lib
cargo new my_rust_plugin --lib
- Add [lib] section in cargo.toml file
[lib]
name = "my_rust_plugin_1" //your plugin file name
crate-type = ["dylib"] //compile to dll file
- Create logic
use rand::{thread_rng, Rng};
//be sure to include #[no_mangle] and extern
//to each of the function that you to be called from outside
#[no_mangle]
pub extern fn random_num() -> i32 {
thread_rng().gen()
}
- Build with
cargo build --release
and findyour_plugin_file_name.dll
intarget/release
folder - Copy the .dll file and paste it in your
/asset
folder - Create a C# script and just run it in the unity.
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class myRustPlugin : MonoBehaviour {
[DllImport("your_plugin_file_name")]
private static extern int random_num();
void Start() {
Debug.Log(random_num());
}
}