简体   繁体   中英

how to change Unity Android plugin package name

I am making my first Android plugin by extending UnityPlayerNativeActivity . The package name is com.inno.myPlugin and that name is registered in Manifest.xml in Plugins/Android folder.

The class that extends " com.inno.myPlugin " is " MyClass ".

The plugin is compiled as .jar file and it uses " AndroidJavaClass " and " AndroidJavaObject " on Unity side to call functions and is placed in Plugins/Android folder .

Each time I compile my example, on my android device, the app package name is always " com.inno.myPlugin ". Even when I change the package name from Unity settings , the default package name is still the package name from the jar plugin which is " com.inno.myPlugin ". The package name can only be renamed from the source code of the jar file ...

I want to publish the plugin to Unity Asset store but I have a question that is bugging me.

If the package name " com.inno.myPlugin " is already used on the Android store, will this prevent the user of my plugin from uploading their app with this plugin?

How do I solve this problem of package naming ?

This is one of the larger issues with Unity Android Plugins.

There's a few possibilities here. Use the below to get to the right answer

a) Does your plugin require that the Main Activity (ie Launcher activity) be "com.inno.myPlugin.MyClass"?

b) Do you override OnActivityResult in MyClass ?

c) Neither of the above

If you answered yes to (a) , then there's very little possibility that anyone but yourself can use this plugin, for a variety of reasons ranging from others being unable to tweak your code to conflicting packages.

If you answered yes to (b) , then there's a decent possibility that others can use it, but they cannot use it if ANOTHER plugin requires that privilege as well

If you answered yes to (c) you should be good.

Easiest way to test it is to use your plugin in a fresh project, with a different package name, and check to see if it works.

Also, I'd advice that you stay away from extending UnityPlayerNativeActivity. The only reasons to extend UnityPlayerNativeActivity are

  1. To get the currentActivity (ie UnityPlayer.currentActivity)
  2. To use unitySendMessage

You can get both of these using reflection. currentActivity is a field, and unitySendMessage is a method. I won't go into detail here since it's not directly relevant to your question, but feel free to ping if you need some details.


Edit Adding some example code on request by the OP

Here's a sample plugin I made without extending UnityPlayerActivity or UnityPlayerNativeActivity. Note that I've just typed this straight in here, so there are probably errors in the code. One thing I know for sure is that the native code requires try-catch blocks, but any IDE should show up the error when you paste the code in.

Native Android Code

package com.myCompany.myProduct;

public class MyClass {
    private Class<?> unityPlayerClass;
    private Field unityCurrentActivity;
    private Method unitySendMessage;

    public void init () {

        //Get the UnityPlayer class using Reflection
        unityPlayerClass = Class.forName("com.unity3d.player.UnityPlayer");
        //Get the currentActivity field
        unityCurrentActivity= unityPlayerClass.getField("currentActivity");
        //Get the UnitySendMessage method
        unitySendMessage = unityPlayerClass.getMethod("UnitySendMessage", new Class [] { String.class, String.class, String.class } );
    }

    //Use this method to get UnityPlayer.currentActivity
    public Activity currentActivity () {

        Activity activity = (Activity) unityCurrentActivity.get(unityPlayerClass);
        return activity;            
    }


    public void unitySendMessageInvoke (String gameObject, String methodName, String param) {
        //Invoke the UnitySendMessage Method
        unitySendMessage.invoke(null, new Object[] { gameObject, methodName, param} );
    }

    public void makeToast (final String toastText, final int duration) {
        currentActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), toastText, duration).show();
            }
        });
    }
}



Unity C# code

AndroidJavaObject myClass = null;

public void Init () {
    if(myClass == null)
        myClass = new AndroidJavaObject("com.myCompany.myProduct.MyClass");

    if(myClass != null)
        myClass.Call("init", new object[0]);
}


public void MakeToast (string toastText, int duration) {
    if(myClass != null)
        myClass.Call("makeToast", new object[] { toastText, duration } );
}



For the Manifest, change it to whatever you're calling the package name to be. Note that it DOES NOT have to match the package for the plugin! So, for example, here our plugin's package name is " com.myCompany.myProduct ", you can use a package name like " com.someOtherCompany.someNewProduct "



How to test this plugin.
1. Add the native code above to "MyClass.java", and create a jar.
2. Copy this jar into Assets/Plugins/Android folder in your Unity Project.
3. Ensure that the package name is not the default package name in Unity
4. Copy the Unity C# code above into a new script
5. Put in some OnGUI code to call the "Init" & "MakeToast" methods in C#

Source - My expertise making plugins for Unity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM