EasyAR从入门到精通开发系列教程(4)---脱卡功能总结

 使用EasyAR SDK 来开发好玩的 AR 应用

脱卡是目前AR 应用开发中最普遍的功能,因此我今天给大家借助EasyAR  SDK介绍两种最普遍的脱卡功能。



实现脱卡第一种方案

思路:我们知道的,当所识别的卡片离开摄像头时,EasyImageTargetBehaviour  这段脚本中的Image的SetActive为false,因此其子物也就不显示了,因此解决的办法就是在Target (false)时间将模型放到一个合适的位置,这样就能实现脱卡,当Target (true)时,再回到原来位置。

1.首先在EasyAR下面的Augmenter下建设一个空物体,用来存放脱卡后的模型命名cube,这个位置的模型不会移动,永远都在屏幕固定位置。然后在cube下面建一个空物体用来保存模型的最佳位置和角度。(具体位置与角度大家根据自己的要求自行调节)



2.然后在ImageTarget的下面建一个空物体用来保存target(名字随便起)出现时间的位置和角度。



3.建一个空物体并挂上这段脚本(记住:这个空物体千万不要建到ImageTarget 下,否则脱卡功能会失效)

[csharp]  view plain  copy
  1. using UnityEngine;  
  2.   
  3. using System.Collections;  
  4.   
  5. public class ZSetactive : MonoBehaviour  
  6.   
  7. {  
  8.   
  9. public Transform GreatTransfrom;                  //脱卡后最佳位置  
  10.   
  11. public GameObject cubePos;                              //模型脱卡时存放位置  
  12.   
  13. public Transform[] TargetTransfrom;              //模型在卡片上的最佳位置  
  14.   
  15. public GameObject[] Target;                          //卡片  
  16.   
  17. public GameObject[] cube;                            //模型  
  18.   
  19. void Start ()  
  20.   
  21. {  
  22.   
  23. for (int i = 0; i < cube.Length; i++) {          //所有模型初始化全部不显示  
  24.   
  25. cube [i].SetActive (false);  
  26.   
  27. }  
  28.   
  29. }  
  30.   
  31. public void tiaozheng ()                              //模型倾斜时调整最佳位置  
  32.   
  33. {  
  34.   
  35. GreatTransfrom.localPosition = new Vector3 (0f, 0f, 0f);  
  36.   
  37. GreatTransfrom.localRotation = Quaternion.identity;  
  38.   
  39. for (int i = 0; i < cube.Length; i++) {  
  40.   
  41. cube [i].transform.localPosition = GreatTransfrom.localPosition;  
  42.   
  43. cube [i].transform.localRotation = GreatTransfrom.localRotation;  
  44.   
  45. }  
  46.   
  47. }  
  48.   
  49. void Update ()  
  50.   
  51. {  
  52.   
  53. WhoShouldShow ();                            //哪个模型应该显示  
  54.   
  55. TargetT ();                                          //有卡片时  
  56.   
  57. TargetF ();                                        //无卡片时  
  58.   
  59. }  
  60.   
  61. int index = -1;  
  62.   
  63. void WhoShouldShow ()                      //哪个模型应该显示  
  64.   
  65. {  
  66.   
  67. for (int i = 0; i < Target.Length; i++) {  
  68.   
  69. if (Target [i].activeSelf == true) {  
  70.   
  71. cube [i].SetActive (true);  
  72.   
  73. index = i;  
  74.   
  75. }  
  76.   
  77. if (i != index) {  
  78.   
  79. cube [i].SetActive (false);  
  80.   
  81. }  
  82.   
  83. }  
  84.   
  85. }  
  86.   
  87. void TargetT ()                                    //不脱卡  
  88.   
  89. {  
  90.   
  91. for (int i = 0; i < Target.Length; i++) {  
  92.   
  93. if (Target [i].activeSelf == true) {  
  94.   
  95. cube [i].transform.parent = Target [i].transform;  
  96.   
  97. cube [i].transform.position = TargetTransfrom [i].position;  
  98.   
  99. }  
  100.   
  101. }  
  102.   
  103. }  
  104.   
  105. void TargetF ()                                    //脱卡  
  106.   
  107. {  
  108.   
  109. for (int i = 0; i < Target.Length; i++) {  
  110.   
  111. if (Target [i].activeSelf == false) {  
  112.   
  113. cube [i].transform.parent = zhenf.transform;  
  114.   
  115. cube [i].transform.localPosition = GreatTransfrom.localPosition;  
  116.   
  117. }  
  118.   
  119. }  
  120.   
  121. }  
  122.   
  123. }  

第三部分:实现脱卡第二种方案

思路:大致思路就是,托卡后将模型的父类设为ARCamera下的一个游戏物体,并调整好位置。

1.EasyAR下的Augmenter下也添加一个模型(与ImageTarget 模型一样),并设置这个模型在你相机下的位置大小各种参数。



[csharp]  view plain  copy
  1. using UnityEngine;  
  2.   
  3. using System.Collections;  
  4.   
  5. public class Tuoka : MonoBehaviour  
  6.   
  7. {  
  8.   
  9. public GameObject Target;//卡片  
  10.   
  11. public GameObject zhen;//识别图上的模型  
  12.   
  13. public GameObject zhen2;//相机下的模型  
  14.   
  15. bool firstFound = false;//是否是第一次识别  
  16.   
  17. void Start()  
  18.   
  19. {  
  20.   
  21. zhen.SetActive(false);  
  22.   
  23. zhen2.SetActive(false);  
  24.   
  25. }  
  26.   
  27. void Update()  
  28.   
  29. {  
  30.   
  31. if (Target.activeSelf == true)  
  32.   
  33. {  
  34.   
  35. zhen.SetActive(true);  
  36.   
  37. zhen2.SetActive(false);//不显示脱卡状态的模型  
  38.   
  39. zhen2.transform.GetComponent().Stop("Take 001");//停止脱卡状态下模型的动画  
  40.   
  41. zhen.GetComponent().Stop("Take 001");//识别图上的模型不播放动画  
  42.   
  43. firstFound = true;  
  44.   
  45. }  
  46.   
  47. if (Target.activeSelf == false && firstFound == true)  
  48.   
  49. {  
  50.   
  51. zhen.SetActive(false);  
  52.   
  53. zhen2.SetActive(true);//显示模型  
  54.   
  55. zhen2.transform.GetComponent().Play("Take 001");  
  56.   
  57. }  
  58.   
  59. }  
  60.   
  61. }  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值