UnityPlayerActivity全局初始化

时间:2017-06-07 10:40:49

标签: java c# android unity3d vuforia

如果你能就我的问题提出建议,我会很感激。

我正在使用适用于Unity3D和Android插件的Vuforia SDK处理AR应用。 我的场景中有几个ImageTargets和3D模型。 我使用Android插件的类看起来像这样:

public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler
{    
    void Start()
    {
        StartCoroutine(DownloadAndCache());
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
        init();        
    }
    public void OnTrackableStateChanged(
    TrackableBehaviour.Status previousStatus,
    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
        newStatus == TrackableBehaviour.Status.TRACKED ||
        newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            if (!mAttached && mBundleInstance)
            {
                // if bundle has been loaded, let's attach it to this trackable
                //...        
            }
            OnTrackingFound();
        }
        else
        {
            OnTrakingLost();

        }

    }  

     private void OnTrackingFound()
     {
         if (mTrackableBehaviour is ImageTargetAbstractBehaviour)
         {
             GetJavaObject().Call("OnMarkerFound");
         }
     }   

    void onButtonClicked(int index)
    {
        //Changing current 3D model material
    }

#if UNITY_ANDROID
        private AndroidJavaObject javaObj = null;
        //LISTENING TO BUTTON CLICK EVENTS FROM ANDROID
        private sealed class EventListner : AndroidJavaProxy
        {
            private AssetBundleAugmenter mReceiver;

            public EventListner(AssetBundleAugmenter receiver)
             : base("com.mypackage.myapp.ImageTargetTracker$Listner")
            {
                mReceiver = receiver;
            }

            public void onButtonClicked(int index) //change color of model
            {
                mReceiver.onButtonClicked(index);
            }
        }


        private AndroidJavaObject GetJavaObject()
        {
            if (javaObj == null)
            {
                javaObj = new AndroidJavaObject("com.mypackage.myapp.ImageTargetTracker");
            }
            return javaObj;
        }
        AndroidJavaObject activity;
        private void init()
        {
            // Retrieve current Android Activity from the Unity Player
            AndroidJavaClass jclass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            activity = jclass.GetStatic<AndroidJavaObject>("currentActivity");

            // Pass reference to the current Activity into the native plugin,
            GetJavaObject().Call("setActivity", activity, new EventListner(this));

        }   


    #else
        void init() {}
    #endif
}

所以我将这个脚本附加到场景中的所有ImageTargets,我知道这一定是错的,因为UnityPlayerinit()函数中多次初始化。

我尝试将脚本附加到我的场景中的ARCamera,并仅在那里编写初始化,但我不确定如何在使用{{1}的脚本中访问currentActivity }。另外,我使用了监听器 - 我的插件中的界面,它监听按钮点击以统一激发某些功能。

我的插件代码:

ImageTargets

那么,我怎样才能在Unity中一次性全局初始化Activity和我的插件类,并在我的所有脚本中使用它们?

1 个答案:

答案 0 :(得分:0)

正如评论中所讨论的,我建议使用singleton模式。