Calling Android NDK function from Unity Script

前端 未结 2 992
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 04:11

So I\'m creating an Android app that uses Unity ... I am getting some assetbundle from Unity but I do not know the url before Unity starts. Thus Unity needs to call a funct

2条回答
  •  孤街浪徒
    2021-01-07 04:47

    I suggest ditch the NDK. Use the Android java plugin.

    In YourPlugin.cs:

    using UnityEngine;
    using System.Collections;
    using System.IO;
    
    #if UNITY_ANDROID
    public class UnityUrlPlugin {
    
      // Logs the user out and invalidates the token
      public static string getUrl() {
        if( Application.platform != RuntimePlatform.Android )
          return;
    
        var pluginClass = new AndroidJavaClass("com.you.UnityUrlPlugin") ;
        AndroidJavaObject plugin = pluginClass.CallStatic("instance");
        return plugin.Call("getURL");
      } 
    }
    #endif
    

    Then, in UnityUrlPlugin.java:

    package com.you;
    
    import android.content.ContentValues;
    import android.content.Intent;
    import android.os.Environment;
    
    public class UnityUrlPlugin {
    
      private static UnityUrlPlugin m_instance;
    
      public static UnityUrlPlugin instance() {
        if(m_instance == null)
          m_instance = new UnityUrlPlugin();
        return m_instance;
      }
    
      private UnityUrlPlugin(){
      }
    
      public String getURL() {
        return "http://blah.com";
      }
    }
    

    And throw UnityUrlPlugin.jar in to Assets/Plugins/Android folder.

    No need for NDK!

提交回复
热议问题