如何更改Unity Android插件包名称 [英] how to change Unity Android plugin package name

查看:170
本文介绍了如何更改Unity Android插件包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过扩展 UnityPlayerNativeActivity 来制作第一个 Android插件。 名称为 com.inno.myPlugin ,该名称已注册 Manifest.xml 插件/ Android 文件夹。

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.

扩展 com.inno。 myPlugin MyClass

该插件编译为 .jar 文件,它在Unity端使用 AndroidJavaClass AndroidJavaObject 调用函数,并在插件中放置 / Android文件夹

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.

每次编译我的示例时,在我的Android设备上,应用程序包名称总是 com.inno.myPlugin < /强>。即使我从 Unity设置更改软件包名称,默认软件包名称仍然是 jar插件中的软件包名称,即 com.inno.myPlugin 包名称 重命名 jar文件源代码 ...

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...

我想发布 插件 Unity资源商店但是我有一个问题困扰着我。

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

如果包名 com.inno.myPlugin 已经使用 strong>在Android商店,这会阻止我的插件用户使用此插件上传他们的应用吗?

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?

我如何解决这个包命名的问题?

推荐答案

这是Unity Android插件的一个大问题。

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

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

b)你是否覆盖 MyClass 中的OnActivityResult?

b) Do you override OnActivityResult in MyClass?

c)两者都没有以上

如果你对(a)的回答是肯定的,那么除了你自己以外的任何人都可以使用这个插件,其他原因包括其他人无法将您的代码调整为冲突的包裹。

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.

如果您对(b)的回答是肯定的,那么这是一个不错的选择其他人可以使用它的可能性如果另一个插件也需要该权限,则他们无法使用它

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

如果你回答是(c)你应该是好人。

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.

另外,我建议您远离扩展UnityPlayerNativeActivity。扩展UnityPlayerNativeActivity的唯一理由是

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


  1. 获取currentActivity(即UnityPlayer.currentActivity)

  2. 要使用unitySendMessage

您可以使用反射来获取这两者。 currentActivity是一个字段,unitySendMessage是一个方法。我不会在这里详细说明,因为它与您的问题没有直接关系,但如果您需要一些细节,请随时ping。

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.

编辑根据OP的要求添加一些示例代码

Edit Adding some example code on request by the OP

这是我在不扩展UnityPlayerActivity或UnityPlayerNativeActivity的情况下制作的示例插件。请注意,我刚刚在这里输入了这个,所以代码中可能存在错误。我确定知道的一件事是本机代码需要try-catch块,但是当您粘贴代码时,任何IDE都应该显示错误。

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.

原生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#代码

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 } );
}






对于清单,将其更改为您要调用的软件包名称。请注意,不要必须匹配插件的包!所以,例如,这里我们的插件的包名是 com.myCompany.myProduct ,你可以使用像 com.someOtherCompany.someNewProduct 这样的包名

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"






如何测试此插件。

1.将上面的本机代码添加到MyClass.java,并创建一个jar。

2.将此jar复制到Unity项目的Assets / Plugins / Android文件夹中。

3.确保程序包名称不是Unity中的默认程序包名称

4.将上面的Unity C#代码复制到新脚本中

5.输入一些OnGUI代码打电话给初始& C#中的MakeToast方法

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#

来源 - 我为Unity制作插件的专长。

Source - My expertise making plugins for Unity.

这篇关于如何更改Unity Android插件包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆