ImageTarget动态创建

来源:互联网 发布:淘宝订单要不要清洗 编辑:程序博客网 时间:2024/05/21 16:54
using UnityEngine;
using System.Linq;
using EasyAR;


namespace Sample
{
/***固定格式无需更改******/
    public class HelloARTarget : MonoBehaviour
    {
        private const string title = "Please enter KEY first!";
        private const string boxtitle = "===PLEASE ENTER YOUR KEY HERE===";
        private const string keyMessage = ""
            + "Steps to create the key for this sample:\n"
            + "  1. login www.easyar.com\n"
            + "  2. create app with\n"
            + "      Name: HelloARMultiTarget-MultiTracker (Unity)\n"
            + "      Bundle ID: cn.easyar.samples.unity.helloarmultitarget.mt\n"
            + "  3. find the created item in the list and show key\n"
            + "  4. replace all text in TextArea with your key";


        private void Awake()//唤醒
        {
            if (FindObjectOfType<EasyARBehaviour>().Key.Contains(boxtitle))
            {
#if UNITY_EDITOR
                UnityEditor.EditorUtility.DisplayDialog(title, keyMessage, "OK");
#endif
                Debug.LogError(title + " " + keyMessage);
            }
        }
/****固定格式无需更改******/


        void CreateTarget(string targetName, out SampleImageTargetBehaviour targetBehaviour)//通过图片动态加载ImageTarget
        {
            GameObject Target = new GameObject(targetName);
            Target.transform.localPosition = Vector3.zero;
            targetBehaviour = Target.AddComponent<SampleImageTargetBehaviour>();
        }


        void Start()
        {
            SampleImageTargetBehaviour targetBehaviour;

            ImageTrackerBehaviour tracker = null;//初始化Target


            foreach (var behaviour in FindObjectsOfType<ImageTrackerBehaviour>())
            {
                if (behaviour.name == "ImageTracker-3")
                    tracker = behaviour;
            }
            if (!tracker)
                return;
            tracker.SimultaneousNum = 2;
//分为图片动态创建和json动态创建

            // dynamically load from image (*.jpg, *.png)
            CreateTarget("argame01", out targetBehaviour);//绑定一个tracker,必须绑定了,才能被tracker追踪,然后识别里面的target。调用之前的createTarget方法,创建ImageTargetBaseBehaviour
            targetBehaviour.Bind(tracker);
            targetBehaviour.SetupWithImage("sightplus/argame01.jpg", StorageType.Assets, "argame01", new Vector2());
            GameObject duck02_1 = Instantiate(Resources.Load("duck02")) as GameObject; //识别出图片后,让模型的坐标跟着imagetarget object的坐标走
            duck02_1.transform.parent = targetBehaviour.gameObject.transform;


            // dynamically load from json file
            CreateTarget("argame00", out targetBehaviour);
            targetBehaviour.Bind(tracker);
            targetBehaviour.SetupWithJsonFile("targets.json", StorageType.Assets, "argame");
            GameObject duck02_2 = Instantiate(Resources.Load("duck02")) as GameObject;
            duck02_2.transform.parent = targetBehaviour.gameObject.transform;


            // dynamically load from json string
            string jsonString = @"
{
  ""images"" :
  [
    {
      ""image"" : ""sightplus/argame02.jpg"",
      ""name"" : ""argame02""
    }
  ]
}
";
            CreateTarget("argame02", out targetBehaviour);
            targetBehaviour.Bind(tracker);
            targetBehaviour.SetupWithJsonString(jsonString, StorageType.Assets, "argame02");
            GameObject duck02_3 = Instantiate(Resources.Load("duck02")) as GameObject;
            duck02_3.transform.parent = targetBehaviour.gameObject.transform;


            // dynamically load all targets from json file
            var targetList = ImageTargetBaseBehaviour.LoadListFromJsonFile("targets2.json", StorageType.Assets);
            foreach (var target in targetList.Where(t => t.IsValid).OfType<ImageTarget>())
            {
                CreateTarget("argame03", out targetBehaviour);
                targetBehaviour.Bind(tracker);
                targetBehaviour.SetupWithTarget(target);
                GameObject duck03 = Instantiate(Resources.Load("duck03")) as GameObject;
                duck03.transform.parent = targetBehaviour.gameObject.transform;
            }


            targetBehaviour = null;
        }
    }
}
原创粉丝点击