简体   繁体   English

如何更改Unity Android插件包名称

[英]how to change Unity Android plugin package name

I am making my first Android plugin by extending UnityPlayerNativeActivity . 我正在通过扩展UnityPlayerNativeActivity制作我的第一个 Android插件。 The package name is com.inno.myPlugin and that name is registered in Manifest.xml in Plugins/Android folder. 程序包名称为com.inno.myPlugin,并且该名称已在Plugins / Android文件夹中的Manifest.xml注册

The class that extends " com.inno.myPlugin " is " MyClass ". 扩展com.inno.myPlugin ”的是“ 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 . 该插件被编译为.jar文件,它在Unity端使用“ AndroidJavaClass ”和“ AndroidJavaObject ”来调用函数,并放置Plugins / Android文件夹中

Each time I compile my example, on my android device, the app package name is always " com.inno.myPlugin ". 每次我编译示例时,在我的android设备上,应用程序包名称始终为“ 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 ". 即使从Unity设置更改了软件包名称,默认的软件包名称仍然是jar插件中软件包名称com.inno.myPlugin ”。 The package name can only be renamed from the source code of the jar file ... 软件包名称 只能jar文件源代码重命名 ...

I want to publish the plugin to Unity Asset store but I have a question that is bugging me. 我想 插件 发布Unity Asset store,但是我有一个困扰我的问题。

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? 如果软件包名称“ com.inno.myPlugin在Android商店中使用,这会阻止我的插件用户使用该插件上传他们的应用程序吗?

How do I solve this problem of package naming ? 如何解决 软件包命名的问题?

This is one of the larger issues with Unity Android Plugins. 这是Unity Android插件的较大问题之一。

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"? a)您的插件是否要求Main活动(即Launcher活动)为“ com.inno.myPlugin.MyClass”?

b) Do you override OnActivityResult in MyClass ? b)您是否在MyClass中重写OnActivityResult?

c) Neither of the above c)以上都不是

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. 如果您对(a)的回答是“是”,那么除了您自己之外,其他任何人都不可能使用此插件,其原因有很多,从其他人无法调整您的代码到有冲突的软件包。

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 如果您对(b)回答“是”,那么其他人很有可能会使用它, 但是如果ANOTHER插件也需要该特权,他们就无法使用它

If you answered yes to (c) you should be good. 如果您对(c)的回答是肯定的,那么您应该很好。

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. 另外,我建议您不要扩展UnityPlayerNativeActivity。 The only reasons to extend UnityPlayerNativeActivity are 扩展UnityPlayerNativeActivity的唯一原因是

  1. To get the currentActivity (ie UnityPlayer.currentActivity) 获取currentActivity(即UnityPlayer.currentActivity)
  2. To use unitySendMessage 使用unitySendMessage

You can get both of these using reflection. 您可以使用反射来获得这两者。 currentActivity is a field, and unitySendMessage is a method. currentActivity是一个字段,unitySendMessage是一个方法。 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. 由于它与您的问题没有直接关系,因此在此不做详细介绍,但是如果您需要一些细节,请随时ping。


Edit Adding some example code on request by the OP 编辑根据OP的要求添加一些示例代码

Here's a sample plugin I made without extending UnityPlayerActivity or UnityPlayerNativeActivity. 这是我在不扩展UnityPlayerActivity或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. 我肯定知道的一件事是,本机代码需要try-catch块,但是将代码粘贴到其中时,任何IDE都应该显示错误。

Native Android Code 本机Android代码

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 Unity C#代码

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 " 因此,例如,此处我们插件的包名称为“ com.myCompany.myProduct ”,您可以使用“ com.someOtherCompany.someNewProduct ”之类的包名称



How to test this plugin. 如何测试此插件。
1. Add the native code above to "MyClass.java", and create a jar. 1.将上面的本机代码添加到“ MyClass.java”,然后创建一个jar。
2. Copy this jar into Assets/Plugins/Android folder in your Unity Project. 2.将此jar复制到Unity项目中的Assets / Plugins / Android文件夹中。
3. Ensure that the package name is not the default package name in Unity 3.确保程序包名称不是Unity中的默认程序包名称
4. Copy the Unity C# code above into a new script 4.将上面的Unity C#代码复制到新脚本中
5. Put in some OnGUI code to call the "Init" & "MakeToast" methods in C# 5.放入一些OnGUI代码以在C#中调用“ Init”和“ MakeToast”方法

Source - My expertise making plugins for Unity. 资料来源-我为Unity制作插件的专业知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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