/unity_rust_plugin_example

Example creating native unity plugin using rust

Primary LanguageRust

Native Unity Plugin in Rust Example

A quick and simple steps to create native unity plugins with rust lang.

Steps

  1. Create rust lib
  cargo new my_rust_plugin --lib
  1. Add [lib] section in cargo.toml file
  [lib]
  name = "my_rust_plugin_1" //your plugin file name
  crate-type = ["dylib"]  //compile to dll file
  1. 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()
  }
  1. Build with cargo build --release and find your_plugin_file_name.dll in target/release folder
  2. Copy the .dll file and paste it in your /asset folder
  3. 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());
    }
}

create C# script | create plugin