본문 바로가기
개발/Unity

Unity) Native Plugin Tutorial

by 테샤르 2025. 7. 7.

Native Plugin Tutorial

 

Unity Learning 에 포함된 Natvie Plugin을 만드는 Tutorial이다.

<요약>

이 튜토리얼에서는 Unity에서 기본 플러그인을 만들고 사용하는 방법을 살펴보겠습니다. 네이티브 플러그인은 일반적으로 C 기반 언어로 작성되고 Unity 외부에서 컴파일되어 사전 빌드된 라이브러리로 임포트되는 코드입니다. 네이티브 플러그인은 iOS를 제외한 모든 빌드 대상 플랫폼에서 사전 컴파일된 바이너리로, Xcode 프로젝트에 포함하기 위해 대신 원본 코드를 가져옵니다. 네이티브 플러그인을 사용하면 Unity 프로젝트에서 기존 C/Objective-C/C++ 코드를 사용하거나 미들웨어 라이브러리와 통합하고 라이브러리가 사용 가능한 하드웨어와 작업할 수 있습니다.

 

반응형

 

< 예제 코드  C++ (Native) >

#include <iostream>
#include <ctime>
#include <cstdlib>

// Exported function for Unity to call
extern "C" float GetRandomFloat()
{
    // Seed 설정
    std::srand(static_cast<unsigned int>(std::time(nullptr)));

    // 0에서 1 사이의 랜덤한 부동 소수점 값을 반환
    return static_cast<float>(std::rand()) / RAND_MAX;
}
반응형

 

< 예제 코드 C# >

using UnityEngine;
using System.Runtime.InteropServices;

public class RandomPlugin : MonoBehaviour
{
    // Native Plugin 함수 선언
    [DllImport("RandomPlugin")]
    private static extern float GetRandomFloat();

    void Start()
    {
        // Native Plugin에서 Random 값을 가져와 출력
        float randomValue = GetRandomFloat();
        Debug.Log("Random Value from Native Plugin: " + randomValue);
    }
}

 

< 환경(유의 사항)  > 

Unity에서는 네이티브 플러그인을 사용할 때 특정 플랫폼에 따라 라이브러리 파일의 이름이 달라질 수 있다.
 위의 코드에서는 "RandomPlugin"이라고 명시가되어있고
플러그인의 이름은 해당 플러그인을 빌드할 때 사용된 이름과 일치해야 한다.

OS 경로
Windows Assets/Plugins 또는 Assets/Plugins/x86_64  하위 경로
macOS .bundle 파일이 Assets/Plugins 하위 경로



Working with Native Plugins  : [링크]

 

Working with Native Plugins - 2019.3 - Unity Learn

In this tutorial, we’ll explore creating and using native plug-ins in Unity. Native plug-ins are code that is written — typically in a C-based language — compiled outside of Unity, and imported as a prebuilt library. Native plug-ins are precompiled b

learn.unity.com

 

 

 

★☆☆☆☆

 

반응형

댓글