我将Unity整合为原生Android应用中的子视图。
我需要在应用程序启动时准备Unity,但不显示它,因为我需要在用户点击按钮时在场景之间切换,而不显示Unity屏幕演示文稿。
流程是:
用户在活动A中选择一个场景 - >使用UnityPlayer.UnitySendMessage
在PlayGameActivity中显示场景,不使用Unity启动/加载屏幕
在iOS中,我这样做了:
class AppDelegate: UIResponder, UIApplicationDelegate {
var currentUnityController: UnityAppController!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.currentUnityController = UnityAppController()
self.currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
但我在Android中找不到类似的东西。
这就是我在子视图中显示UnityPlayer的方式:
public class PlayGameActivity extends UnityPlayerActivity {
private static final String TAG = "PlayGameActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
// Create the UnityPlayer
mUnityPlayer = new UnityPlayer(this);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
// Add the Unity view
RelativeLayout layout = (RelativeLayout) findViewById(R.id.container_unity_play_game_activity);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) layout.getLayoutParams();
layout.addView(mUnityPlayer.getView(), 0, lp);
String unityScene = "some_scene";
Log.d(TAG, "calling scene " + unityScene);
UnityPlayer.UnitySendMessage("AController", "AMethodToChangeScene", unityScene);
}
}
我尝试使用播放器和下面的按钮更改活动中的场景,并且它有效,因为我等待播放器准备好。 Unity确实会收到更改场景的消息,并在加载后自行完成。
那么有没有一种方法可以在不显示iOS的情况下启动Unity?或者知道UnityPlayer何时准备好/没有显示启动画面的方法?
答案 0 :(得分:2)
我测试了一种我觉得可能适合你的方法。
首先,我创建了两个View变量,以便可以在整个类中操作视图。
public class PlayGameActivity extends UnityPlayerActivity {
private static final String TAG = "PlayGameActivity";
private View playerView;
private RelativeLayout layout;
RelativeLayout.LayoutParams lp
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
// Create the UnityPlayer
mUnityPlayer = new UnityPlayer(this);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
//MODIFIED VERSION OF YOUR CODE
playerView = mUnityPlayer.getView();
// Add the Unity view SLIGHTY MODIFIED
layout = (RelativeLayout) findViewById(R.id.container_unity_play_game_activity);
lp = (RelativeLayout.LayoutParams) layout.getLayoutParams();
String unityScene = "some_scene";
Log.d(TAG, "calling scene " + unityScene);
UnityPlayer.UnitySendMessage("AController", "AMethodToChangeScene", unityScene);
}
}
然后在我的测试中,我有两个按钮设置了听众。第一个按钮让我向Unity发送消息,
Button theButton = (Button)findViewById(R.id.button);
theButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUnityPlayer.UnitySendMessage("CallThis", "MessageFromAndroid", "HI!");
}
});
然后第二个按钮执行以下操作,
Button good = (Button)findViewById(R.id.button2);
good.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.addView(playerView);
mUnityPlayer.requestFocus();
}
});
因此,基本的想法是,在您准备好之前,不要使用layout.addView()
将mUnityPlayer添加到视图中。您可以让您的程序调用UnitySendMessage,然后从Unity内部调用Android,从我创建的按钮运行第二行。也就是说,只需将我使用的按钮替换为Unity可以调用的功能。你可能已经熟悉了它,但无论如何我都会链接到Android Scripting API。
编辑:我运行了另一个测试,没有必要将mUnityPlayer.requestFocus()放入将UnityPlayer添加到视图的方法中。