코우지의 개발일기

[iOS/Unity] Unity 프로젝트를 Framework로 사용하여 iOS native App에 통합하기 (1) 본문

iOS 개발일기/Unity iOS

[iOS/Unity] Unity 프로젝트를 Framework로 사용하여 iOS native App에 통합하기 (1)

Kouzi 2023. 4. 6. 15:28

안녕하세요 !
두근두근 첫 포스팅!
 
Unity 프로젝트를 Framework로 사용하여 iOS native App에 통합해보려고 합니다.
iOS Native 앱을 만드신다음에 거기에 유니티 뷰를 띄운다던지.. 하려고 할때 유니티를 라이브러리로 사용하실수 있어요 !
(Unity 공식사이트 문서참조: https://docs.unity3d.com/kr/current/Manual/UnityasaLibrary-iOS.html)

Unity를 네이티브 iOS 애플리케이션에 통합 - Unity 매뉴얼

이 페이지에서는 Unity as a Library 기능을 사용하여 Unity 런타임 라이브러리를 iOS 네이티브 애플리케이션에 통합하는 방법을 설명합니다.

docs.unity3d.com

 
 
다음과 같은 순서로 설명해보려고 합니다.
1. Unity 프로그램 설치 및 프로젝트 생성후 iOS App으로 빌드
2. 빌드된 Unity Project를 iOS Native App으로 import 하여 Native App에서 Unity 호출하기
3. iOS Native App과 Unity Project 사이에 데이터 전달 방법 (Objective-C Bridge 활용)
정도의 순서로 설명해볼까 합니다.
 
오늘은 가장 기초 순서인 Unity 프로그램 설치 및 프로젝트 생성후 iOS App으로 빌드에 대해 설명드리겠습니다.
저는 2021.3.16f1 버전을 사용했습니다.


 

(1) Unity Project 만들기

프로젝트를 적당히 만들고.. 

 
뷰에다가 버튼 2개 / Text1개를 만들어줍니다.

Text에는 iOS Native App으로 받는 Data를 표시해주고 ,
버튼은 1개는 Native App위에 떠있는 Unity를 종료하는 버튼, 또 하나는 iOS Native App으로 Unity의 값을 전달하는 용도가 될겁니다.
 
이렇게 하고 exitUnity에 대한 코드 부터 작성해줍시다.

ExitUnity C#파일 생성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ExitUnity : MonoBehaviour
{
    Button button;

	public void OnExitButtonClick()
	{
	    Application.Unload();
	}

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

ExitUnity의 코드를 이렇게 짜줍니다.
그리고 버튼에 추가시켜주면되겠죠 ?

exitButton에 추가!

 
그리고 3가지 스크립트를 더 만들겁니다.
iOS Native App으로 데이터를 전송하는 스크립트와 받는 스크립트, 모바일 앱으로 데이터를 전송하기 위한 NativeAPI라는 스크립트를 만들겁니다.
먼저 iOS Native App으로 부터 받는 스크립트입니다.

//ReceiveDataFromiOS.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;

public class MyClass
{
	//받을 데이터 포맷
    public int testNumber;
    public string testString;
}

public class ReceiveDataFromiOS : MonoBehaviour
{
    public Text receiveText;	//받은 데이터를 보여줄 텍스트

    void setData(string jsonString) {
        MyClass myObject = JsonUtility.FromJson<MyClass>(jsonString);
        receiveText.text = "testNumber: " + myObject.testNumber + "," + "testString: " + myObject.testString;
    	//받은 데이터를 receiveText에 받아서 화면에 출력
    }

    // Start is called before the first frame update
    void Start()
    {
        receiveText.text = "receiveData!";
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 
다음으로는 iOS Native App으로 보내는 스크립트입니다.

//SendDataToUnity.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Data 
{
	//모바일로 보낼 데이터 타입
    public int m_int;
    public string m_string;
} 

public class SendDataToUnity : MonoBehaviour
{
    Button button;

	public void OnSendMessageButtonClick()
	{
        Data data = new Data();
        
        //보낼 테스트 데이터
        data.m_int = 1234;
        data.m_string = "testJson String";
 
        string str = JsonUtility.ToJson(data);
	    NativeAPI.sendMessageToMobileApp(str);
	}
    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(OnSendMessageButtonClick);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 
그리고 NativeAPI입니다

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

public class NativeAPI {
    [DllImport("__Internal")]
    public static extern void sendMessageToMobileApp(string message);
}

 
이렇게 까지 하셨으면 버튼과 텍스트에 스크립트를 추가해줍시다.

fromiOSData
sendDataButton

 
여기까지하셨으면 Unity 프로젝트 세팅은 적당히 되었고, 이제 Xcode로 넘어가보겠습니다.
2편에서 계속...

Comments