본문 바로가기
개발/Unity

Unity) Unity 에서 Android Callback 처리 방법

by 테샤르 2022. 6. 2.

Unity 에서 Android Callback 처리 방법

Native Callback 처리하는 방법을 정리하는 포스팅이다.

 

반응형

JAVA interface Code

package com.example.android;
public interface PluginCallback {
    public void onSuccess(String videoPath);
    public void onError(String errorMessage);
}

Callaback 

public void myPluginMethod(PluginCallback callback) {
    // Do something
    callback.onSuccess("onSuccess");
    // Do something horrible
    callback.onError("onError");
}

C# interface

class AndroidPluginCallback : AndroidJavaProxy
    {
        public AndroidPluginCallback() : base("com.example.android.PluginCallback") { }

        public void onSuccess(string videoPath) {
            Debug.Log("ENTER callback onSuccess: " + videoPath);
        }
        public void onError(string errorMessage)
        {
            Debug.Log("ENTER callback onError: " + errorMessage);
        }
    }

Unity Callback 전달

AndroidJavaObject pluginClass = new     AndroidJavaObject("com.example.android.MyPlugin");
pluginClass.Call("myPluginMethod", new AndroidPluginCallback());

간단하게 Android에서 myPluginMethod를 통해서 AndroidPluginCallback을 등록해서 Native(Android)에서 PluginCallback의 onSuccess, onError의 이벤트를 전달해주는 역활을 한다.

 

Unity AndroidJavaObject : [ 링크 ]

 

Unity - 스크립팅 API: AndroidJavaObject

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기

docs.unity3d.com

Unity Callback Listener : [ 링크 ]

 

Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android

I have an android library project and imported the library project in the Unity project. Now, I want to implement a callback in Unity project, which will execute according to the response given by ...

stackoverflow.com

 

 

 

 

★☆☆☆☆

 

반응형

댓글