0

我按照此处的说明创建了一个基于 Vuforia/Unity 项目的 Eclipse 项目。这是启动和运行。

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

有什么方法可以检测 Unity 启动画面何时退出,因此在场景加载之前我没有适当的控件?

2013 年 3 月 18 日更新

我在 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 在评论中的建议?

4

1 回答 1

0

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

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

由于 MainActivity.java 中的计时器重复调用 QCARViewFinderTask 的 run 方法,控制视图将被添加到 Unity Player 父视图中作为 splashComplete 转换为 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);
}
于 2013-03-18T20:43:49.973 回答