问题
I've created an Eclipse project based on a Vuforia/Unity project by following the instructions here. That's up and running.
I am adding a button to my main activity, extends from QCARPlayerActivity. That also works, however, the button sits on top of the Unity player as the Unity splash screen plays.
Is there any way to detect when the Unity splash screen exits so I don't have controls in place before the scene loads?
UPDATE 3/18/13
I've added a static boolean to my main activity in Eclipse to track splash screen completion and modified the code that adds the controls to watch the boolean.
MainActivity.java
public static boolean splashComplete = false;
private View mControlViewContainer = null; // initialized in onCreate
class QCARViewFinderTask extends TimerTask {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
if (!QCAR.isInitialized()) return; //wait for QCAR init
//search for QCAR view if it hasn't been found
if (mQCARView == null)
{
View rootView = MainActivity.this.findViewById(android.R.id.content);
QCARUnityPlayer qcarView = findQCARView(rootView);
if (qcarView != null) {
mQCARParentView = (ViewGroup)(qcarView.getParent());
mQCARView = qcarView;
}
}
// add controls if QCAR view is located and the splash sequence is complete
if(mQCARView != null && splashComplete && mControlViewContainer != null){
mQCARParentView.addView(mControlViewContainer);
mViewFinderTimer.cancel();
mViewFinderTimer = null;
}
}
});
}
}
In Unity I created a simple script to set the static boolean in Java and attached it to the Vuforia ARCamera
SplashExit.js
function Start () {
var mainActivity = new AndroidJavaClass ("com.example.app.MainActivity");
mainActivity.SetStatic.("splashComplete",true);
}
This works fairly well in a project with a simple scene. My controls seem to load on splash exit. When I use this method with a more complicated scene, however, the controls come up a second or so before the splash screen disappears.
Is there a better place to attach my Unity script, or a better method within the script, that will more accurately reflect when the splash sequence has exited? Perhaps Jerdak's suggestion in the comments?
回答1:
Adding a yield statement did the trick. Full solution follows.
SplashExit.js should be attached to the ARCamera Game object in Unity. The start method will stall until the scene has loaded up, then set splashComplete to true in MainActivity.java.
As the timer in MainActivity.java repeatedly calls the run method of QCARViewFinderTask, the control view will be added to the Unity Player parent view as splashComplete transitions to true.
MainActivity.java
public class MainActivity extends QCARPlayerActivity {
private QCARUnityPlayer mQCARView = null;
private ViewGroup mQCARParentView = null;
private Timer mViewFinderTimer = null;
private View mControlViewContainer = null;
public static boolean splashComplete = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mControlViewContainer = getLayoutInflater().inflate(R.layout.control_layout, null);
}
@Override
public void onResume() {
super.onResume();
if (mQCARView == null) {
//search the QCAR view
mViewFinderTimer = new Timer();
mViewFinderTimer.scheduleAtFixedRate(new QCARViewFinderTask(), 1000, 1000);
}
}
@Override
public void onPause() {
super.onPause();
if (mViewFinderTimer != null) {
mViewFinderTimer.cancel();
mViewFinderTimer = null;
}
}
class QCARViewFinderTask extends TimerTask {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
if (!QCAR.isInitialized()) return; //wait for QCAR init
//search for QCAR view if it hasn't been found
if (mQCARView == null)
{
View rootView = MainActivity.this.findViewById(android.R.id.content);
QCARUnityPlayer qcarView = findQCARView(rootView);
if (qcarView != null) {
mQCARParentView = (ViewGroup)(qcarView.getParent());
mQCARView = qcarView;
}
}
// add controls if QCAR view is located and the splash sequence is complete
if(mQCARView != null && splashComplete && mControlViewContainer != null){
mQCARParentView.addView(mControlViewContainer);
mViewFinderTimer.cancel();
mViewFinderTimer = null;
}
}
});
}
private QCARUnityPlayer findQCARView(View view) {
if (view instanceof QCARUnityPlayer) {
return (QCARUnityPlayer)view;
}
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup)view;
for (int i = 0; i
SplashExit.js
function Start () { yield; // wait for the scene to fully load // Note that com.example.app.MainActivity should be updated to match your bundle identifier and class names var mainActivity = new AndroidJavaClass ("com.example.app.MainActivity"); mainActivity.SetStatic.("splashComplete",true); }
来源:https://stackoverflow.com/questions/15281513/detect-splash-screen-exit-on-unity-android-eclipse-project