Search Unity

  1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

How to use a self plugin in unity project

Discussion in 'Android' started by qyxls, Mar 5, 2012.

  1. qyxls

    qyxls

    Joined:
    Mar 30, 2011
    Posts:
    4
    Hi,everyone:
    I study Unity’s Plugin recently.I want to use the Java api I developed in my unity project.The Java code that I finished in Eclipse(The project is Android Project).Following is my steps:
    1、Create my android project and then I import the unity’s jar to my android project(UNITY_INSTALLATION\Editor\Data\PlaybackEngines\and roidplayer\bin and add classes.jar)
    Android project’s package:com.zpxh.plugin
    2、write a Java class inherit from UnityPlayerActivity with two methods:
    Code (csharp):
    1.  
    2. package com.zpxh.plugin;
    3.  
    4. import com.unity3d.player.UnityPlayerActivity;
    5.  
    6. import android.os.Bundle;
    7. import android.widget.Toast;
    8.  
    9. public class PluginActivity extends UnityPlayerActivity {
    10.     @Override
    11.     public void onCreate(Bundle savedInstanceState) {
    12.         super.onCreate(savedInstanceState);
    13.     }
    14.    
    15.     public void tip(){
    16.         Toast.makeText(this, "this is a tip", Toast.LENGTH_LONG).show();       
    17.     }
    18.    
    19.     public int getNumber(){
    20.         return 8;
    21.     }
    22. }
    23.  
    3、use the jar command to create my Plugin.
    Jar cvf com.zpxh.plugin.jar com\
    4、copy the jar plugin into my unity project.I put the jar in Assets\Plugins\Android\bin\com.zpxh.plugin.jar. furthermore the AndroidManifest.xml and the res folder finished in android project included.
    5、at last I write the C# to call my plugin:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PluginTest : MonoBehaviour {
    6.  
    7.     void Start () {
    8.         jc = new AndroidJavaClass("com.zpxh.plugin.PluginActivity");
    9.     }
    10.  
    11.     void OnGUI(){
    12.         if(GUI.Button(new Rect(30, 30, 100, 40), "CLICK ME")){
    13.             jc.Call("tip");
    14.             int num = jc.Call<int>("getNumber");
    15.             GameObject.Find("gText").guiText.text = "OK : " + num;
    16.         }
    17.     }
    18. }
    19.  
    20.  
    But I failed.I neither see a thost nor get the 8. The plugin have not called by the C#.
    I referenced http://unity3d.com/support/documentation/Manual/PluginsForAndroid.html
    Am I wrong in somewhere? thanx
     
  2. TeotiGraphix

    TeotiGraphix

    Joined:
    Jan 11, 2011
    Posts:
    128
    Hi,

    The only thing I can see that might be wrong is you are putting the jar file in the Plugin/bin folder. You need to put it in the Assets\Plugins\Android folder. IE Assets\Plugins\Android\com.zpxh.plugin.jar

    It should work after that, you jar just isn't loading so the calls can't do anything.

    Mike
     
  3. qyxls

    qyxls

    Joined:
    Mar 30, 2011
    Posts:
    4
    Hi,TeotiGraphix:
    Both the two ways did I test in my project,but the result was same.Now I found in my Java code I define my method with "static" keyword can succeed to call in the unity.eg:
    Code (csharp):
    1. package com.zpxh.plugin;
    2.  
    3.  
    4.  
    5. import com.unity3d.player.UnityPlayerActivity;
    6.  
    7. import android.os.Bundle;
    8. import android.widget.Toast;
    9.  
    10. public class PluginActivity extends UnityPlayerActivity {
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.     }    
    15.  
    16.     public void tip(){
    17.         Toast.makeText(this, "this is a tip", Toast.LENGTH_LONG).show();
    18.     }    
    19.  
    20.     public static int getNumber(){  // Here I define with "static" keyword
    21.         return 8;
    22.     }
    23. }
    I do not why.Maybe no-static method I did not get the object?But I used "jc = new AndroidJavaClass("com.zpxh.plugin.PluginActivity");" to init a Object. puzzling......
     
    Last edited: Mar 11, 2012
  4. TeotiGraphix

    TeotiGraphix

    Joined:
    Jan 11, 2011
    Posts:
    128
    Hi,

    To early in the morning. :)

    What you are trying to do is backwards. You are actually creating an instance of the activity which has to be throwing exceptions in Java which is why you code is not returning (I think). There is no reason to use anything that has to do with an Activity here.

    You need a normal class.

    try;

    Code (csharp):
    1.  
    2. public class MyClass{
    3.  
    4.     public void tip(){
    5.         Toast.makeText(this, "this is a tip", Toast.LENGTH_LONG).show();
    6.     }
    7.  
    8.     public int getNumber(){
    9.         return 8;
    10.     }
    11. }
    12.  
    13. ...
    14.  
    15.  
    16.     void Start () {
    17.         jc = new AndroidJavaClass("com.zpxh.blah.MyClass");
    18.     }
    19.  
    20.     void OnGUI(){
    21.         if(GUI.Button(new Rect(30, 30, 100, 40), "CLICK ME")){
    22.             jc.Call("tip");
    23.             int num = jc.Call<int>("getNumber");
    24.             GameObject.Find("gText").guiText.text = "OK : " + num;
    25.         }
    26.     }
    27.  

    Mike
     
  5. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    First of all, the most obvious: Are you testing it on your Android Device? JNI/Java Plugins don't work in Editor.

    Second: You "obtained" the class of (your PluginActivity, similar to Type myType = typeof(PluginActivity) in C#), but not a specific instance! So you can use that classes static methods (because they are not bound to a specific instance), but can't call it's member methods.

    You can obtain the instance of the current player activity by

    Code (csharp):
    1.  
    2. AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    3. AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    4.  
    This is the instance of UnityPlayerActivity. In order to use your selfdefined methods, you'd need to cast it PluginActivity (dunno how you do this in the C# using JNI, never used it I did the casting in Java instead).

    "AndroidJavaObject" is always an instance, "AndroidJavaClass" is just the class itself, NOT an specific instance. It can be used to create an instance or just to access it's static methods without the need of an instance.
     
  6. TeotiGraphix

    TeotiGraphix

    Joined:
    Jan 11, 2011
    Posts:
    128
    Tseng,

    My bad on the AndroidJavaClass, my eyes said AndroidJavaObject, that was where my reply was coming from. My mistake. :)

    And yeah I forgot to ask the obvious "are you testing in the editor", but then again he said he could get the static method to work, so it must have been on the device.

    Mike
     
  7. qyxls

    qyxls

    Joined:
    Mar 30, 2011
    Posts:
    4
    Thanks TeotiGraphix and Tseng,

    I test my project on my galaxy s ii( Samsung i9100 ).

    I develop the application on Android. To achieve some functions,I need to use android api.For learning and your answers,I understand the deeper about Java plugin.

    Thanks.
     
  8. TeotiGraphix

    TeotiGraphix

    Joined:
    Jan 11, 2011
    Posts:
    128
    @qyxls Yeah it's pretty powerful stuff. You might want to think about encapsulating some libraries now in C# and java. This way you have some set functionality that will work out of the box for all your future projects. :)

    Mike