GJM :Unity使用EasyAR实现脱卡功能

首先说下大致思路
当卡片离开摄像头时间,ImageTarget-Image的SetActive (false),所以其子物体(model)也就不显示了,因此解决的办法就是在Target (false)时间将模型放到一个合适的位置,这样就能实现脱卡,当Target (true)时,再回到原来位置。具体放到什么位置合适
 
在EasyAR下面的Augmenter下建设一个空物体,用来存放脱卡后的模型命名ZhenF(真模型的Father),这个位置的模型不会移动,永远都在屏幕固定位置。然后在ZhenF下面建一个空物体用来保存模型的最佳位置和角度。
 
然后在ImageTarget-Image的下面建一个空物体用来保存target出现时间的最佳位置和角度。

 

 

 

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ZSetactive : MonoBehaviour
  4. {
  5.         public Transform GreatTransfrom;//脱卡后最佳位置
  6.         public GameObject zhenf;//模型脱卡时存放位置
  7.         public Transform[] TargetTransfrom;//模型在卡片上的最佳位置
  8.         public GameObject[] Target;//卡片
  9.         public GameObject[] zhen;//模型
  10.         void Start ()
  11.         {
  12.                 for (int i = 0; i < zhen.Length; i++) {//所有模型初始化全部不显示
  13.                         zhen [i].SetActive (false);
  14.                 }
  15.         }
  16.         public void tiaozheng ()//模型倾斜时调整最佳位置
  17.         {
  18.                 GreatTransfrom.localPosition = new Vector3 (0f,0f,0f);
  19.                 GreatTransfrom.localRotation  = Quaternion .identity;
  20.                 for (int i = 0; i < zhen.Length; i++) {
  21.                         zhen [i].transform.localPosition  = GreatTransfrom.localPosition ;
  22.                         zhen [i].transform.localRotation  = GreatTransfrom.localRotation ;
  23.                 }
  24.         }
  25.         void Update ()
  26.         {
  27.                 WhoShouldShow();//哪个模型应该显示
  28.                 TargetT();//有卡片时
  29.                 TargetF();//无卡片时
  30.         }
  31.         int index = -1;
  32.         void WhoShouldShow(){//哪个模型应该显示
  33. //                for (int i = 0; i < Target .Length ; i++) {
  34. //                        if (Target [i].activeSelf == true) {
  35. //                        zhen [i].SetActive (true);
  36. //                        index = i ;
  37. //                                if (i != index ) {
  38. //                                zhen [i].SetActive (false);
  39. //                                }
  40. //                        }
  41. //                }
  42.                 if (Target [0].activeSelf == true) {
  43.                         zhen [0].SetActive (true);
  44.                         zhen [1].SetActive (false);
  45.                         zhen [2].SetActive (false);
  46.                         zhen [3].SetActive (false);
  47.                 }
  48.                 if (Target [1].activeSelf == true) {
  49.                         zhen [0].SetActive (false);
  50.                         zhen [1].SetActive (true);
  51.                         zhen [2].SetActive (false);
  52.                         zhen [3].SetActive (false);
  53.                 }
  54.                 if (Target [2].activeSelf == true) {
  55.                         zhen [0].SetActive (false);
  56.                         zhen [1].SetActive (false);
  57.                         zhen [2].SetActive (true);
  58.                         zhen [3].SetActive (false);
  59.                 }
  60.                 if (Target [3].activeSelf == true) {
  61.                         zhen [0].SetActive (false);
  62.                         zhen [1].SetActive (false);
  63.                         zhen [2].SetActive (false);
  64.                         zhen [3].SetActive (true);
  65.                 }
  66.         }
  67.         void TargetT(){
  68.                 for (int i = 0; i < Target.Length; i++) {//不脱卡
  69.                         if (Target [i].activeSelf == true) {
  70.                                 zhen [i].transform.parent = Target [i].transform;
  71.                                 zhen [i].transform.position = TargetTransfrom [i].position;
  72.                         }        
  73.                 }
  74.         }
  75.         void TargetF(){//脱卡
  76.                 for (int i = 0; i < Target.Length; i++) {
  77.                         if (Target [i].activeSelf == false) {
  78.                                 zhen [i].transform.parent = zhenf.transform;
  79.                                 zhen [i].transform.localPosition  = GreatTransfrom.localPosition;
  80.                         }
  81.                 }
  82.         }
  83. }

 

 

 

把这个脚本可以放在一个空物体上面。。

posted @ 2016-12-15 14:21  TDou  阅读(1228)  评论(0编辑  收藏  举报