大家也可以发挥自己的脑洞,随意写些内容,目前我们先实现通过EasyAR SDK 来实现扫描二维码 显示文字的功能。 Step 3:编辑代码 我们准备好了二维码,接下来就是在unity里编辑代码来实现功能,首先我们在"EasyAR_ImageTracker-1_QRCode-1" 下新建一个脚本,命名为"ARIsEasyBehaviour",
脚本下载地址:链接: https://pan.baidu.com/s/1dF5tigx 密码: 9ag5
using System.Collections;
using UnityEngine;
namespace EasyAR
{
public class ARIsEasyBehaviour : 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: HelloARQRCode (Unity)\n"
+ " Bundle ID: cn.easyar.samples.unity.helloarqrcode\n"
+ " 3. find the created item in the list and show key\n"
+ " 4. replace all text in TextArea with your key";
private bool startShowMessage;
private bool isShowing;
private string textMessage;
private void Awake()
{
var EasyARBehaviour = FindObjectOfType<EasyARBehaviour>();
if (EasyARBehaviour.Key.Contains(boxtitle))
{
#if UNITY_EDITOR
UnityEditor.EditorUtility.DisplayDialog(title, keyMessage, "OK");
#endif
Debug.LogError(title + " " + keyMessage);
}
EasyARBehaviour.Initialize();
foreach (var behaviour in ARBuilder.Instance.ARCameraBehaviours)
{
behaviour.TargetFound += OnTargetFound;
behaviour.TargetLost += OnTargetLost;
behaviour.TextMessage += OnTextMessage;
}
foreach (var behaviour in ARBuilder.Instance.ImageTrackerBehaviours)
{
behaviour.TargetLoad += OnTargetLoad;
behaviour.TargetUnload += OnTargetUnload;
}
}
void OnTargetFound(ARCameraBaseBehaviour arcameraBehaviour, TargetAbstractBehaviour targetBehaviour, Target target)
{
Debug.Log("<Global Handler> Found: " + target.Id);
}
void OnTargetLost(ARCameraBaseBehaviour arcameraBehaviour, TargetAbstractBehaviour targetBehaviour, Target target)
{
Debug.Log("<Global Handler> Lost: " + target.Id);
}
void OnTargetLoad(ImageTrackerBaseBehaviour trackerBehaviour, ImageTargetBaseBehaviour targetBehaviour, Target target, bool status)
{
Debug.Log("<Global Handler> Load target (" + status + "): " + target.Id + " (" + target.Name + ") " + " -> " + trackerBehaviour);
}
void OnTargetUnload(ImageTrackerBaseBehaviour trackerBehaviour, ImageTargetBaseBehaviour targetBehaviour, Target target, bool status)
{
Debug.Log("<Global Handler> Unload target (" + status + "): " + target.Id + " (" + target.Name + ") " + " -> " + trackerBehaviour);
}
private void OnTextMessage(ARCameraBaseBehaviour arcameraBehaviour, string text)
{
textMessage = text;
startShowMessage = true;
Debug.Log("got text: " + text);
}
IEnumerator ShowMessage()
{
isShowing = true;
yield return new WaitForSeconds(2f);
isShowing = false;
}
private void OnGUI()
{
if (startShowMessage)
{
if (!isShowing)
StartCoroutine(ShowMessage());
startShowMessage = false;
}
if (isShowing)
GUI.Box(new Rect(10, Screen.height / 2, Screen.width - 20, 30), textMessage);
}
}
}
我们在这段脚本文件实现的是首先Target 的识别然后是扫描二维码之后接收结果并实现绘制在屏幕上,对于Target 的found与load等方法相信大家已经很熟悉了。对于OnTextMessage()接收返回结果然后赋值给textMessage,并由OnGUI()进行绘制。我们Build测试,会实现如下的效果:
PS:在实际的开发中,我们不会像这样从零来搭建AR+二维码的开发环境,一般是直接在EasyAR官网的实例进行二次开发,这样会大大提高我们的效率。下一篇我们会实现二维码+AR的一个实例。