本帖最后由 QQ260185343 于 2016-4-13 16:25 编辑
首先说下大致思路
当卡片离开摄像头时间,ImageTarget-Image的SetActive (false),所以其子物体(model)也就不显示了,因此解决的办法就是在Target (false)时间将模型放到一个合适的位置,这样就能实现脱卡,当Target (true)时,再回到原来位置。具体放到什么位置合适
在EasyAR下面的Augmenter下建设一个空物体,用来存放脱卡后的模型命名ZhenF(真模型的Father),这个位置的模型不会移动,永远都在屏幕固定位置。然后在ZhenF下面建一个空物体用来保存模型的最佳位置和角度。
然后在ImageTarget-Image的下面建一个空物体用来保存target出现时间的最佳位置和角度。
- using UnityEngine;
- using System.Collections;
- public class ZSetactive : MonoBehaviour
- {
- public Transform GreatTransfrom; //脱卡后最佳位置
- public GameObject zhenf; //模型脱卡时存放位置
- public Transform[] TargetTransfrom; //模型在卡片上的最佳位置
- public GameObject[] Target; //卡片
- public GameObject[] zhen; //模型
- void Start ()
- {
- for (int i = 0; i < zhen.Length; i++) { //所有模型初始化全部不显示
- zhen [i].SetActive (false);
- }
- }
- public void tiaozheng () //模型倾斜时调整最佳位置
- {
- GreatTransfrom.localPosition = new Vector3 (0f, 0f, 0f);
- GreatTransfrom.localRotation = Quaternion.identity;
- for (int i = 0; i < zhen.Length; i++) {
- zhen [i].transform.localPosition = GreatTransfrom.localPosition;
- zhen [i].transform.localRotation = GreatTransfrom.localRotation;
- }
- }
- void Update ()
- {
- WhoShouldShow (); //哪个模型应该显示
- TargetT (); //有卡片时
- TargetF (); //无卡片时
- }
- int index = -1;
- void WhoShouldShow () //哪个模型应该显示
- {
- for (int i = 0; i < Target.Length; i++) {
- if (Target [i].activeSelf == true) {
- zhen [i].SetActive (true);
- index = i;
- }
- if (i != index) {
- zhen [i].SetActive (false);
- }
- }
- }
- void TargetT () //不脱卡
- {
- for (int i = 0; i < Target.Length; i++) {
- if (Target [i].activeSelf == true) {
- zhen [i].transform.parent = Target [i].transform;
- zhen [i].transform.position = TargetTransfrom [i].position;
- }
- }
- }
- void TargetF () //脱卡
- {
- for (int i = 0; i < Target.Length; i++) {
- if (Target [i].activeSelf == false) {
- zhen [i].transform.parent = zhenf.transform;
- zhen [i].transform.localPosition = GreatTransfrom.localPosition;
- }
- }
- }
- }
复制代码
|