Display Unity Scene as Sub View in android studio
What you are looking for is how to display Unity Scene as a subview.
Something like below:
This is described here on Unity's forum. And the code to load Unity scene:
package com.unity3d.viewexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import com.unity3d.player.UnityPlayer;
public class JavaCubeViewActivity extends Activity {
private UnityPlayer m_UnityPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the UnityPlayer
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
m_UnityPlayer.init(glesMode, trueColor8888);
setContentView(R.layout.main);
// Add the Unity view
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.addView(m_UnityPlayer.getView(), 0, lp);
}
}
You can export the Unity project to Android Android project then use that code above or you can write that Java code then compile it as a jar plugin and make Unity load it by modifying the Android Manifest in Unity. Both method should work.
Finally, you can call C# function on the Unity side from Java with UnityPlayer.SendMessage.
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.unity3d.unity" android:versionName="1.0" android:versionCode="1">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
<activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
</activity>
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
<meta-data android:name="android.app.lib_name" android:value="unity" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
</activity>
</application>
<uses-feature android:glEsVersion="0x00020000" />
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" />
</manifest>
EDIT:
If you want to call Unity's function from Java, use
UnityPlayer.UnitySendMessage("GameObjectName", "MethodName", "parameter to send");
You can find more information on this and how to import it into Android Studio here.
Invoking scene when loading Unity as subview in Android
If you plan to load Unity activity from Android, you should create an empty scene. Call this this Main Menu, then make it the default scene that loads through the Build Settings. Make it index 0.
The goal of this empty scene is to load specific scene when script attached to it is called.
In this Main Menu scene, create a GameObject called SceneLoader then attach the script below to it:
public class SceneLoader : MonoBehaviour
{
public void loadScene(string sceneIndex)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(Convert.ToInt16(sceneIndex));
}
}
You should also create a GameObject called SceneLoader in every other scene and attach the script above to all of them.
Now, load Unity Activity which automatically loads the default/empty scene. Since there is only one GameObject/SceneLoader in it, it will load very fast.
You can now load other scenes from Java with:
UnityPlayer.UnitySendMessage("SceneLoader", "loadScene", "9");
Android: Changing Unity Scene from UI components outside the scene
Make communicator between Unity side and Android.
It's possible to do that technically, if you make following function in unity. I've just wrote here how unity and android communicate so
before use this code you need to see brief of integration between Unity and Android to use native plugin from Unity so see this link to do that.https://docs.unity3d.com/Manual/PluginsForAndroid.html
After you understand that how to make plugin for android, then see codes. It will help you.
- GameObject Name: LoadGameSceneObject, loadScene.cs
public class loadScene : MonoBehaviour
{
public static void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
- SceneLoadActivie.java
public static void SendMessageToUnity(String gameObjectName, String methodName, String message) throws Exception
{
Log.d("unity", gameObjectName + " " + methodName + " " + message);
final Class<?> player = Class.forName("com.unity3d.player.UnityPlayer");
player.getMethod("UnitySendMessage", String.class, String.class,
String.class).invoke(null, gameObjectName, methodName, message);
}
...
// call this function.
public void LoadScene()
{
SceneLoadActivie.SendMessageToUnity("LoadGameSceneObject", "LoadScene", "SomeScene");
}
Related Topics
How to Update UI from Asynctask
Firebase Sort by Points Depending on Date
Getting Null Device Id While Registering to Gcm
How to Disable Crashlytics During Development
Debugging with Android Studio Stuck at "Waiting for Debugger" Forever
Cannot Find Zip-Align When Publishing App
Determine Angle of View of Smartphone Camera
Save Internal File in My Own Internal Folder in Android
Java.Lang.Illegalstateexception: the Specified Child Already Has a Parent
Advantages of Using Bundle Instead of Direct Intent Putextra() in Android
How to Display Both Icon and Title of Action Inside Actionbar
Android - Taking Photos and Saving Them with a Custom Name to a Custom Destination via Intent