레이블이 rust인 게시물을 표시합니다. 모든 게시물 표시
레이블이 rust인 게시물을 표시합니다. 모든 게시물 표시

2020년 3월 23일 월요일

Unity3d에서 Rust함수 호출

Unity3d에서 Rust 라이브러리를 생성하여 rust함수를 호출해 보기

Rust를 사용하여 라이브러리를 생성/빌드 한 뒤 유니티에서 Plugin으로 dll import하여 함수 사용

-- Rust --

숫자 2개를 더해 리턴하는 간단한 라이브러리 생성

Library생성을 위해 Cargo.toml 에 lib정보 추가
[package]
name = "unity-rust"version = "0.1.0"authors = ["xxx "]
edition = "2018"
[dependencies]

[lib]
name = "unity_rust"crate-type = ["dylib"]

lib.rs 소스 추가(단순한 2개 숫자 합산후 리턴)
#[no_mangle]pub extern fn sum(input1: i32, input2: i32) -> i32 {
    input1 + input2
}

생성된 파일 ./unity-rust/target/debug/libunity_rust.dylib 파일을
Unity ./Assets/Plugins 에 복사


--- Unity ---

RustBehaviour.cs 파일 추가

클래스 안에 DllImport속성을 지정하여 함수 추가(dll확장자는 생략 가능)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public class RustBehaviour : MonoBehaviour
{
    [DllImport("libunity_rust")]
    private static extern int sum(int v1, int v2);

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("sum : " + sum(3, 4));
    }

}

아래와 같은 결과를 Console에서 확인 가능