我能够导出Unity3d android项目并在android studio中导入它,但我如何将多个unity3d项目导入android studio? 我尝试将其作为模块导入但不起作用。
每个unity3d项目都依赖于unity-classes.jar文件,因此当我导入多个项目时会抛出错误
错误:任务执行失败:app:transformNative_libsWithMergeJniLibsForDebug'。 com.android.build.api.transform.TransformException:com.android.builder.packaging.DuplicateFileException: 在APK lib / armeabi-v7a / libmain.so
中复制的重复文件
答案 0 :(得分:1)
我在这里解决了这个问题。
首先你必须将团结和你的场景导出为android并检查谷歌开发。之后,你打开任何android项目。
1.File - > new->导入模块......然后选择你的团结项目。
2.打开团结项目的gradle(ex.build.gradle(Module:unity))并删除“apply plugin:...”和applicationId
而你在顶部添加“apply plugin:'com.android.library'”
3.您可以转到文件 - >项目结构,在模块中选择您的应用程序。然后在依赖项中,按下+ - >模块依赖和
你选择你的团结项目。
4.在java UnityActivity中,您将活动扩展为Fragment,然后添加此代码。
public class UnityActivity extends Fragment {
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
private static final String ARG_SECTION_NUMBER = "section_number";
public static UnityActivity newInstance(int sectionNumber) {
UnityActivity fragment = new UnityActivity();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
getActivity().getWindow().takeSurface(null);
getActivity().setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getActivity().getWindow().setFormat(PixelFormat.RGB_565);
mUnityPlayer = new UnityPlayer(getActivity());
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true)) {
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
mUnityPlayer.windowFocusChanged(true);
mUnityPlayer.setX((float)-5);
View playerView = mUnityPlayer.getView();
return playerView;
}
// Quit Unity
@Override public void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
@Override public void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
@Override public void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
// This ensures the layout will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
public void onWindowFocusChanged(boolean hasFocus)
{
super.getActivity().onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.getActivity().dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
}
答案 1 :(得分:0)
当我尝试将两个Unity项目作为两个库导入Android Studio时,我遇到了完全相同的问题。你有两个解决方案:
在你的应用程序的graddle.build中添加:
android {
...
packagingOptions {
pickFirst 'lib/armeabi-v7a/libmain.so'
pickFirst 'lib/armeabi-v7a/libmono.so'
pickFirst 'lib/armeabi-v7a/libunity.so'
pickFirst 'lib/x86/libmain.so'
pickFirst 'lib/x86/libmain.so'
pickFirst 'lib/x86/libmain.so'
}
...
}
或者转到项目窗口 - > Android - >在您的一个Unity项目下找到文件夹 jniLibs 并删除文件夹 armeabi-v7 和 x86 。