我没有解决我的问题,但我设法找到一种方法来避免它:我决定制作一个更大的项目,其中包括两个不同的场景(一个使用 Vuforia AR,另一个使用一些 UI 文本),而不是导入两个小型 Unity 项目。接下来的挑战是找到一种方法,当我单击我在 Android 上设计的关联按钮时,调用正确的 Unity 场景。
Unity
在每个场景中我创建了一个空对象(我称之为SceneManagerObject
)将附加脚本GoToScene
。在此脚本中将有以下方法:
private void ChangeScene(string sceneName)
{
SceneManager.LoadScene (sceneName);
}
public void ReceiveJavaMessage(string message)
{
if (message.Equals ("Scene1") || message.Equals ("Scene2"))
{
ChangeScene (message);
}
else
{
Debug.Log ("The scene name is incorrect");
}
}
第二种方法将是从 Java 代码中调用的方法。
然后我在每个场景上创建了一个 UI 按钮,它将调用onClick
单击方法时,其目的是能够返回到 Android 而不破坏 Unity 活动,以便我们可以在需要时恢复它。为此,我使用脚本创建了另一个空对象GoBackToAndroid
随附的:
public class GoBackToAndroid : MonoBehaviour {
public string activityName; // contains the name of the activity I want to go when I quit this scene
public void onClick()
{
callJavaMethod();
}
private void callJavaMethod()
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer);
AnroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("ReceiveUnityMessage", activityName); // Will call a method in Java with activityName as parameter
}
在我将项目导出为 Google Android 项目并为下一步做好准备之后,Unity 就这样了。
安卓工作室
将 Unity 项目作为库导入后。我创建了三个活动:MainActivity
, SecondActivity
and MultipleScenesUnityActivity
:
public class MultipleScenesUnityActivity extends UnityPlayerActivity {
public static boolean unityIsRunning = false; // true if a Unity Activity is running on background
@Override protected void onDestroy()
{
unityIsRunning = false;
super.mUnityPlayer.quit();
super.onDestroy();
}
@Override protected void onResume()
{
Intent currentIntent = getIntent();
// If there's no Unity Activity inside the back stack
// (meaning it just had been created)
if (!unityIsRunning)
{
super.mUnityPlayer.UnitySendMessage("SceneManagerObject", "ReceiveJavaMessage", currentIntent.getStringExtra("sceneName");
}
unityIsRunning = true;
super.onResume();
super.mUnityPlayer.resume();
}
public void ReceiveUnityMessage (String messageFromUnity)
{
Intent intent;
if (messageFromUnity.equals("MainActivity"))
{
intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
else if (messageFromUnity.equals("SecondActivity"))
{
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
else
Log.d("Test", "The activity " + messageFromUnity + " doesn't exist");
}
}
The ReceiveUnityMessage
是我们离开 Unity Activity 时调用的方法,其目的是引导我们前往我们想去的 Activity。
然后MainActivity
(the SecondActivity
遵循相同的模式):
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goToActivity2(View view) // called when I click on a button
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
public void goToUnity(View view) // called when I click on another button
{
Intent intentUnity = new Intent(this, MultipleScenesUnityActivity.class);
if (unityIsRunning)
{
intentUnity.removeExtra("sceneName"); // We have to clean the extra before putting another one otherwise we'll get always the same Unity scene
/* If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought
* to the front of its task's history stack if it is already running. */
intentUnity.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
/* We send a string to Unity which, when received, will change the scene.
* This ONLY works when the Unity activity is running on background. */
UnitySendMessage("SceneManagerObject", "ReceiveJavaMessage", "Scene1");
}
else
{
/* The intent gets a key with its String value. The value has to be the name of the Unity
* scene and is received in UnityActivity1 when the activity starts.*/
intentUnity.putExtra("sceneName", "Scene1");
}
startActivity(intentUnity);
}
}
因此,我所做的就是通过发送消息来连接所有内容,以便我可以管理我想要启动的场景,活动也是如此。
我确信有更好的方法可以做到这一点,我仍然是一个新手,我可能会很笨拙。无论如何,我设法避免了我原来的问题,所以如果有人面临同样的问题,这个答案可能是一个有趣的选择。