새소식

iOS/Unity

Unity에서 iOS 네이티브 코드를 사용해 보자.

  • -

[참고 영상]

https://youtu.be/krerK59xVPI


[학습 목표]

1. Unity에서 iOS Alert 화면을 보게 하자.

2. c#과 obj-c를 연결시켜서 Alert 코드를 만들자.


[구현 방법]

유니티를 활용해서 화면을 만들고 c#과 obj-c 를 이용해서 Alert 화면을 만들 것이다.

 

1. 유니티를 실행 후 화면을 만들어 주자.

Canvas를 이용해서 검은색 배경화면을 만들고 앞에 버튼으로 Show Alert 화면이 나오는 버튼을 만들었다. 이 때 버튼은 TextMeshPro가 아니라 예전 버튼인 Legacy를 이용했다.

 

2. Assets 폴더 내부에 폴더 만들기.

Assets 폴더 내부에 Plugins 내부에 iOS 폴더를 만들었고 iOSPlugin이라는 c# 파일을 생성하였다.

 

3. 확장자 변경

iOSPlugin.cs 파일을 .mm으로 바꿔주도록 하자.

바꾼뒤에 Unity로 돌아오면 확장자 파일이 바뀐것과 동시에 우측 상단에  

이와 같이 iOS에 체크가 되어 있는 것을 볼 수가 있을 것이다.

 

4. Unity 우측 상단에 Assets 밑에서 Open C# project를 실행시켜주자.

 

<iOSPlugin.mm>

#import <Foundation/Foundation.h> @interface iOSPlugin: NSObject @end @implementation iOSPlugin +(void)alertView:(NSString *)title addMessage:(NSString * message)showAlert { UIAlertController *alert = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: UIAlertControllerStyleAlert]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){}]; [alert addAction: defaultAction]; [self presentViewController: alert animated: YES completion: nil]; } @end extern "C" { void _showAlert(const char *title, const char *message) { [iOSPlugin [alert: title, message]] } }

 

5. Assets 내부에 Scripts 폴더를 만들고 해당 폴더에서 iOSPlugin.cs 파일과 UIBindinsg.cs 파일을 만들자.

<iOSPlugin.cs>

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; public class iOSPlugin : MonoBehaviour { #if UNITY_IOS [DllImport("__Internal")] private static extern void _ShowAlert(string title, string message); public static void ShowAlert(string title, string message) { Debug.Log("ShowAlert 이 제대로 실행되고 있습니다."); _ShowAlert(title, message); } #else public static void ShowAlert(string title, string message) { Debug.LogError("ShowAlert is only supported on iOS platform"); } #endif }

 

<UIBindins.cs>

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIBindings : MonoBehaviour { [SerializeField] private Button showAlertButton; // Start is called before the first frame update void Start() { showAlertButton.onClick.AddListener(ShowAlert); } void ShowAlert() { iOSPlugin.ShowAlert("Hello","World"); } }

 

6. Unity로 들어와서 Building Setting를 누르자.

 

Building Settings 내부에 있는 iOS에서 Player Settings 안에 들어와서 target SDK를 Device에서 simulatior 로 바꿔주도록 하자.

 

 

7. Build를 작동시키고 Unity-iPhone 파일을 실행시켜보자.

 

실행을 하면 오류가 날 것이다. 이 때 파일 내부에 있는 iOSPlugin 내부 내용을 아래와 같이 바꿔주도록 하자.

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> extern UIViewController *UnityGetGLViewController(); @interface iOSPlugin: NSObject @end @implementation iOSPlugin +(void)alertView:(NSString*)title addMessage:(NSString*) message { UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:defaultAction]; [UnityGetGLViewController() presentViewController:alert animated:YES completion:nil]; } @end extern "C" { void _ShowAlert(const char *title, const char *message) { [iOSPlugin alertView:[NSString stringWithUTF8String:title] addMessage:[NSString stringWithUTF8String:message]]; } }

[작동 화면]

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.