在Unity3D中集成.jar文件

时间:2014-07-14 09:15:17

标签: java c# android plugins unity3d

我创建了一个具有单一活动名称" MainActivity"的示例android项目,我在此活动中创建了一个简单的静态和非静态函数。然后我将这个项目转换为LibraryProject,所以我在bin文件夹中找到了这个项目的jar文件。当我在Unity的Assets / Plugins / Android文件夹中复制此.jar文件和AndroidManifest.xml文件时。静态函数和非静态函数都不会被调用。

using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
        {
            obj_Activity .CallStatic("showMap");
        }
    }

Android代码

public class MainActivity extends UnityPlayerActivity {

    com.google.android.gms.maps.MapView mapView;
    String apiKey="AIzaSyA-CxbAb1fA7QLJEvq1eSCSvHq9zh1mgEI";
    static WebView view;
    static LinearLayout layout;
    public static Context context;
    static LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      layout = new LinearLayout(this);       
    setContentView(layout);
    context=this;
}


public static void showMap()
{
    Log.d("looter", "google.com");
     view=new WebView(context);
     view.setLayoutParams(params);


     view.setWebViewClient(new WebViewClient() {
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
           //  Log.i(TAG, "Processing webview url click...");
             view.loadUrl(url);
             return true;
         }

         public void onPageFinished(WebView view, String url) {
             //Log.i(TAG, "Finished loading URL: " +url);

         }

         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

         }
     });
     view.loadUrl("http://www.google.com");
     layout.addView(view);
}

1 个答案:

答案 0 :(得分:0)

我怀疑你的问题可能与这一行有关:

using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))

我怀疑该类正在返回Unity类而不是重写的类,或者它可能正在返回该类,但保存基类作为它的值。尝试使用您的包ID查找您的活动类。例如com.yourcompanyname.yourClassName。