Beispiel #1
0
    private void OnTrackingFound()
    {
        ModelManager._instance.ModelCountPlusOne();         //发现的模型数目+1
        //在发现模型之后,应该重置手机的休息时间。
        CountDown._instance.ResetTime();
        if (ModelManager._instance.isOffCard && (ModelManager._instance.offCardTargetTransform && ModelManager._instance.offCardTargetTransform == this.transform))
        {
            Utility.LogPrint("refound");
//			ModelManager._instance.offCardModelIsMoving = false;
//			ModelManager._instance.ModelCountPlusOne();//发现的模型数目+1
//			float disY = ModelManager._instance.offCardModelTransform.GetComponent<ModelSelfManager> ().tooTall ? Screen.height / 4 : Screen.height / 2;
//			Vector3 position = new Vector3 (Screen.width/2, disY, ModelManager._instance.zAxisDis);
//			position = ModelManager._instance.offCardCamera.ScreenToWorldPoint (position);
//			ModelManager._instance.offCardModelTransform.position = position;
//			ModelManager._instance.offCardModelTransform.localRotation = Quaternion.Euler(new Vector3(29,151,-8));
//			Utility.LogPrint ("Refound --" + mTrackableBehaviour.TrackableName);
            return;
        }
        //如果模型没有加载,去加载。
        if (!isModelLoaded)
        {
            StartCoroutine(LoadModel());
        }
        else
        {
            //这里先触发了Trigger,然后才调用的SetActive(true),这样应该能避免自己也收到关闭音效的事件。待测试 (没问题)
            if (ModelManager._instance.modelCount > 1)
            {
                EventManager.TriggerEvent("Found");
            }
            if (ModelManager._instance.modelCount == 1)
            {
                Utility.PrepareForSendMessage("FoundTarget", "1");
            }
            if (this.transform.childCount >= 1)
            {
                this.transform.GetChild(0).gameObject.SetActive(true);
                //这行代码是方便测试用的,正式使用应该要用LoadModel方法中的。
                if (isFirstFound)
                {
                    AssMemoryManager.AddGOToMemoryManager(mTrackableBehaviour.TrackableName, this.gameObject);
                }
                ModelManager._instance.currentRotationTransform = this.transform.GetChild(0);
            }
            else
            {
                Utility.LogPrint(mTrackableBehaviour.TrackableName + "--child count is 0");
            }
            //每次扫描过后,更新model的Index值,后面AssMemoryManager会根据这个值来管理这些模型
            AssMemoryManager.UpdateModelIndex(mTrackableBehaviour.TrackableName);
            if (ModelManager._instance.isOffCard)
            {
                EventManager.TriggerEvent("OffCard");
            }
            //Utility.LogPrint ("--  "+Vector3.Distance (transform.position, Camera.main.transform.position));
        }
        Utility.LogPrint("Trackable " + mTrackableBehaviour.TrackableName + " --- found");
    }
        void OffCard()
        {
            //string trackName = mTrackableBehaviour.TrackableName; //AssMemoryManager.GetGameojectNameWithMaxIndex ();

            string trackName = AssMemoryManager.GetGameojectNameWithMaxIndex();

            if (mTrackableBehaviour.TrackableName.Equals(trackName))
            {
                //SetModelToCamera ();
                modelIsOffCard = true;
                EventManager.StartListening("RecoveryModelToTarget", recoveryModelListener);
            }
            else
            {
                for (int i = 0; i < this.transform.childCount; i++)
                {
                    Utility.HideRendererAndCollider(this.transform.GetChild(i));
                }
                modelIsOffCard = false;
                Debug.Log("---" + mTrackableBehaviour.TrackableName + "is not max index");
            }
        }
        private void OnTrackingFound()
        {
            if (ModelManager._instance.isOffCard && modelIsOffCard)
            {
                Debug.Log("handle reFound");
                ModelManager._instance.offCardModelIsMoving = false;
                if (LeanTouch.GetCamera(ref camera1) == true)
                {
                    ModelManager._instance.ModelCountPlusOne();                                         //发现的模型数目+1
                    //float dis = go.transform.position.z - camera1.transform.position.z; //计算与相机之间的距离,确定z = 多少
                    Vector3 position = new Vector3(Screen.width / 2, Screen.height / 2, zAxisDistance); // 经调试,这里Z直接设置成20,显示在屏幕中的模型大小比较合适
                    position = camera1.ScreenToWorldPoint(position);
                    ModelManager._instance.offCardModelTransform.position = position;
                    //go.transform.localRotation = modelOriginQua;
                    //这个角度的模型脱卡后稍微好看一点
                    //Utility.offCardModelTransform.localRotation = Quaternion.Euler(new Vector3(-29,-29,8));
                    ModelManager._instance.offCardModelTransform.localRotation = Quaternion.Euler(new Vector3(29, 151, -8));
                    return;
                }                                               //return;
            }

            if (!isModelLoaded)
            {             //if no model bind
                StartCoroutine(LoadModel());
            }
            ModelManager._instance.ModelCountPlusOne();            //发现的模型数目+1
            if (ModelManager._instance.modelCount == 1)
            {
                Utility.PrepareForSendMessage("FoundTarget", "1");
            }
            else
            {
                Debug.Log("----current model count ----" + ModelManager._instance.modelCount);
            }
            if (ModelManager._instance.isOffCard)
            {
                EventManager.StopListening("RecoveryModelToTarget", recoveryModelListener);
                EventManager.TriggerEvent("RecoveryModelToTarget");                 //还原其他模型到target。因为脱卡只留一个模型
            }
            //RecoveryModelToTarget();
            if (this.transform.childCount >= 1)
            {
                //show the first child transform
                Transform foundTransform = this.transform.GetChild(0);
                if (isFirstFound)
                {
                    EventManager.StartListening("OffCard", offCardListener);
                    //这行代码是方便测试用的,正式使用应该要用LoadModel方法中的。
                    AssMemoryManager.AddGOToMemoryManager(mTrackableBehaviour.TrackableName, this.gameObject);
                    //这里记录初始位置,便于以后各种手势之后可以恢复原来的状态。第一次found的时候肯定是初始状态了
                    modelOriginLocalPos = foundTransform.localPosition;
                    modelOriginLocalQua = foundTransform.localRotation;
                    modelOriginPos      = foundTransform.position;
                    modelOriginQua      = foundTransform.rotation;
                    modelOriginScale    = foundTransform.localScale;
                    isFirstFound        = false;
                }
                else
                {
                    foundTransform.localPosition = modelOriginLocalPos;
                    foundTransform.localRotation = modelOriginLocalQua;
                    foundTransform.localScale    = modelOriginScale;
                }
                Utility.ShowRendererAndCollider(foundTransform);
                //ParticleSystem foundParticle = GetTypeParticle (foundTransform, "Found_Particle");
                Animator            animator        = GetComponentInChildren <Animator> ();
                LeanRotationControl rotationControl = GetComponentInChildren <LeanRotationControl> ();
                if (rotationControl)
                {
                    rotationControl.enabled = true;
                }
                //if (foundParticle)		{	foundParticle.Play ();			}
                if (animator)
                {
                    animator.Play("Take 001", 0, 0);
                }
                ParticleControl(true, "Found_Particle", this.transform.GetChild(0));

                if (!ModelManager._instance.isOffCard)
                {
                    ModelManager._instance.currentRotationTransform = this.transform.GetChild(0);
                }
            }
            else
            {
                Debug.Log("child count is 0");
            }
            //每次扫描过后,更新model的Index值,后面AssMemoryManager会根据这个值来管理这些模型
            //AssMemoryManager.UpdateModelIndex(mTrackableBehaviour.TrackableName);
            if (ModelManager._instance.isOffCard)
            {
                EventManager.TriggerEvent("OffCard");
            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " --------------------------------  found");
        }
        private IEnumerator LoadModel()
        {
            string path = modelSource;

            Debug.Log("DynamicModelTrackableEventHandler LoadModel " + path);
            if (File.Exists(path))
            {
                Utility.PrepareForSendMessage("LoadAssetBundle", "0", targetInfo ["targetName"]);
                string path1  = "file://" + modelSource;
                WWW    bundle = new WWW(path1);
                yield return(bundle);

                GameObject [] gos = bundle.assetBundle.LoadAllAssets <GameObject>();
                for (int i = 0; i < gos.Length; ++i)
                {
                    GameObject go;
                    yield return(go = Instantiate(gos [i]));

                    if (go)
                    {
                        //Instantiate success
                        go.transform.SetParent(this.gameObject.transform);
                        if (i == 0)
                        {
                            continue;                                   // the first gameobject needn't hidden
                        }
                        Utility.HideRendererAndCollider(go.transform);
                    }
                    else
                    {
                        Debug.Log("model loaded but instantiate fail");
                    }
                }
                #region MultiTargetManager
                for (int i = 0; i < this.transform.childCount - 1; ++i)
                {
                    LeanFingerTap tap = this.transform.GetChild(i).GetComponentInChildren <LeanFingerTap> (true);
                    tap.nextARObject = this.transform.GetChild(i + 1).gameObject;
                }
                if (this.transform.childCount > 1)
                {
                    GameObject    startGo = this.transform.GetChild(0).gameObject;
                    LeanFingerTap tap     = this.transform.GetChild(this.transform.childCount - 1).GetComponentInChildren <LeanFingerTap> (true);
                    tap.nextARObject = startGo;
                }
                #endregion                 //MultiTargetManager

                bundle.assetBundle.Unload(false);
                isModelLoaded = true;
                Debug.Log("DynamicModelTrackableEventHandler LoadModel Created");

                #region ass memorymanager

                /*
                 * 这里AssetBundle的管理形式是:当程序中已经加载的AssetBundle已经超过一定数目时(现在暂定5个),就从中删除一个index最小的模型,此时在对应加载最新的模型
                 */
                if (AssMemoryManager.instance.dicCount >= 5)
                {
                    Debug.Log("need to delete some model from memory");
                    AssMemoryManager.RemoveGoFromMemoryManager();
                }
                AssMemoryManager.AddGOToMemoryManager(mTrackableBehaviour.TrackableName, this.gameObject);
                Utility.LogPrint("add gameobject to target,the trackName is:" + mTrackableBehaviour.TrackableName);
                #endregion                 //memorymanager end

                //notify mobile load ass success
                Utility.PrepareForSendMessage("LoadAssetBundle", "1", targetInfo);
            }
            else
            {
                //file not exist,notify mobile to load the ass;
                Utility.LogPrint("load model error,because the file path is not exist");
                Utility.PrepareForSendMessage("DownAssetBundle", "1", targetInfo);
            }
        }
Beispiel #5
0
    private IEnumerator LoadModel()
    {
        string path = modelSource;

        Utility.LogPrint("DynamicModelTrackableEventHandler LoadModel " + path);
        if (File.Exists(path))
        {
            Utility.PrepareForSendMessage("LoadAssetBundle", "0", mTrackableBehaviour.TrackableName);
            string path1  = "file://" + modelSource;
            WWW    bundle = new WWW(path1);
            yield return(bundle);

            GameObject [] gos = bundle.assetBundle.LoadAllAssets <GameObject>();
            for (int i = 0; i < gos.Length; ++i)
            {
                GameObject go;
                yield return(go = Instantiate(gos [i]));

                if (go)
                {
                    go.transform.position = this.transform.position;
                    go.transform.SetParent(this.gameObject.transform);

                    ModelSelfManager modelSelfManager = go.GetComponent <ModelSelfManager> ();
                    modelSelfManager.modelOriginLocalPos = go.transform.localPosition;
                    modelSelfManager.modelOriginLocalQua = go.transform.localRotation;
                    modelSelfManager.modelOriginScale    = go.transform.localScale;

                    if (i == 0)
                    {
                        continue;
                    }                                                   // the first gameobject needn't hidden
                    go.SetActive(false);
                }
                else
                {
                    Utility.LogPrint(mTrackableBehaviour.TrackableName + "--model loaded but instantiate fail");
                }
            }
            bundle.assetBundle.Unload(false);

            isModelLoaded = true;
            Utility.LogPrint(mTrackableBehaviour.TrackableName + "--Model Created");
            //------------------
            //这里先触发了Trigger,然后才调用的SetActive(true),这样应该能避免自己也收到关闭音效的事件。待测试 (没问题)
            if (ModelManager._instance.modelCount > 1)
            {
                EventManager.TriggerEvent("Found");
            }
            if (ModelManager._instance.modelCount == 1)
            {
                Utility.PrepareForSendMessage("FoundTarget", "1");
            }
            if (this.transform.childCount >= 1)
            {
                this.transform.GetChild(0).gameObject.SetActive(true);
                //这行代码是方便测试用的,正式使用应该要用LoadModel方法中的。
                if (isFirstFound)
                {
                    AssMemoryManager.AddGOToMemoryManager(mTrackableBehaviour.TrackableName, this.gameObject);
                }
                ModelManager._instance.currentRotationTransform = this.transform.GetChild(0);
            }
            else
            {
                Utility.LogPrint(mTrackableBehaviour.TrackableName + "--child count is 0");
            }
            //每次扫描过后,更新model的Index值,后面AssMemoryManager会根据这个值来管理这些模型
            AssMemoryManager.UpdateModelIndex(mTrackableBehaviour.TrackableName);
            if (ModelManager._instance.isOffCard)
            {
                EventManager.TriggerEvent("OffCard");
            }
            //-----------------------
            #region ass memorymanager

            /*
             * 这里AssetBundle的管理形式是:当程序中已经加载的AssetBundle已经超过一定数目时(现在暂定5个),就从中删除一个index最小的模型,此时在对应加载最新的模型
             */
            if (AssMemoryManager.instance.dicCount >= 5)
            {
                Utility.LogPrint("when load " + mTrackableBehaviour.TrackableName + "--need to delete some model from memory");
                AssMemoryManager.RemoveGoFromMemoryManager();
            }
            AssMemoryManager.AddGOToMemoryManager(mTrackableBehaviour.TrackableName, this.gameObject);
            Utility.LogPrint("add gameobject to target,the trackName is--" + mTrackableBehaviour.TrackableName);
            #endregion             //memorymanager end
            //notify mobile load ass success
            Utility.PrepareForSendMessage("LoadAssetBundle", "1", mTrackableBehaviour.TrackableName);
        }
        else
        {
            //file not exist,notify mobile to load the ass;
            Utility.LogPrint(mTrackableBehaviour.TrackableName + "--load model error,because the file path is not exist");
            Utility.PrepareForSendMessage("DownAssetBundle", "1", targetInfo);
        }
    }