Hello everybody!
Has anyone tried to import two unity projects in one android studio project?
I managed to import one unity project as fragment in android studio (and it works fine!!)… when I import a second unity project both of the unity scenes are not working… I think that maybe the problem is that both projects use the same libs/structure/names (for example munityplayer)! Can anyone help me… ?thanks in advance!
hi Sara!
first you have to export unity and your scenes as android and check google development. Afterwards,
you open any android project.
1.File ->new->import module … and you select your unity project.
2.Open the gradle of your unity project (ex.build.gradle( Module:unity)) and you delete “apply plugin:…” and applicationId and instead you add at the top “apply plugin: ‘com.android.library’”
3.You go to File->project structure and in modules you select your app.Then in dependencies, you press the + → module dependency and you select your unity project.
4.In java UnityPlayerActivity you extend the activity as Fragment, and you add this code:
public class UnityPlayerActivity 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 UnityPlayerActivity newInstance(int sectionNumber) {
UnityPlayerActivity fragment = new UnityPlayerActivity();
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); }
}*
- In UnityPlayerActivity you change protected void to public void!
And that’s it… you can use unity as fragment in any activity. Hope it helps…