检测Unity Android / Eclipse项目上的启动画面退出

时间:2013-03-07 20:53:49

标签: android eclipse unity3d splash-screen

我按照here的说明创建了一个基于Vuforia / Unity项目的Eclipse项目。这已经开始运行了。

我在主要活动中添加了一个按钮,从QCARPlayerActivity扩展。这也有效,但是当Unity启动画面播放时,按钮位于Unity播放器的顶部。

有没有办法检测Unity启动画面何时退出,所以在场景加载之前我没有控件?

更新3/18/13

我在Eclipse中的主要活动中添加了一个静态布尔值来跟踪启动画面的完成情况,并修改了添加控件以观察布尔值的代码。

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;
                }
            }
        });
    }
}

在Unity中我创建了一个简单的脚本来在Java中设置静态布尔值并将其附加到Vuforia ARCamera

SplashExit.js

function Start () {
    var mainActivity = new AndroidJavaClass ("com.example.app.MainActivity");
    mainActivity.SetStatic.("splashComplete",true);
}

在具有简单场景的项目中,这非常有效。我的控件似乎在启动出口时加载。但是,当我将此方法用于更复杂的场景时,控制会在启动画面消失之前出现一秒左右。

是否有更好的地方可以附加我的Unity脚本或脚本中更好的方法,以便更准确地反映启动顺序何时退出?也许Jerdak在评论中提出了建议?

1 个答案:

答案 0 :(得分:0)

添加一个yield语句就可以了。完整解决方案如下

SplashExit.js应附加到Unity中的ARCamera Game对象。 start方法将停止,直到场景加载完毕,然后在MainActivity.java中将splashComplete设置为true。

由于MainActivity.java中的计时器反复调用QCARViewFinderTask的run方法,因此当splashComplete转换为true时,控件视图将添加到Unity Player父视图中。

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);
}