Use EasyAR SDK to develop fun AR applications

EasyAR Developer Exchange Group: 605785368

Part ONE: Preface

This tutorial is to teach you to implement a very popular case in the VISUAL + APP, in addition to their own AR APP is very popular several features. Tutorial points in the middle down to achieve.

Upper part: the main realization of fingers can move THE AR model to the appropriate position, both hands can enlarge the model, click the model to play animation to achieve interaction.

Middle part: the main realization is to click the screen AR model to move to the specified position.

Lower part: Mainly achieve the capture of wonderful moments to save and share to the relevant social platforms.

Part two: Preview



See + demo

Part THREE: Development environment preparation

Links to related resources: monster model: : https://pan.baidu.com/s/1nv165mD password: s8s6

Download EasyAR SDK to build the most basic environment of EasyAR development.

Ok, next we delete Unity’s original Camera and drag the EasyAR Camera into the panel.






Then drag the imported monster model into the panel (note: we do not use Imagetarget here, because there is no need to identify the function, you can imagine, to add a buffer display effect for the model display, here I will not implement, mainly AR mobile terminal core knowledge to share with you)






Part four: Modify related parameters

First modify the Angle of the AR camera so that its X value is rotated 270 degrees






Next, rotate the Y value of the monster 180 degrees and enlarge it by 2 times. Change the default animation (you can also leave it unchanged, but it will look more amazing).






Add the BoxCollider to the monster and check Trigger

The fifth part: the realization of click monster play animation to achieve interaction

First add an Animation to the object. Add it to the Animation as you like






End down a new section of code to achieve animation interaction, the code is very simple

using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public Animation anim; void Start() { anim = GetComponent();

}

void Update()

{

if (! anim.isPlaying) {

anim.Play (“2HitCombo”);

}

}

void OnMouseDown()

{

anim.Play (“jumpAttack_RM”);

}

}

Part 6: Realizing finger drag model to any position

Add the following code for unity Input mobile screen operations

Add to Update () : First define a variable Speed

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)

{

Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

transform.Translate(-touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);

}

Part seven: Realizing the double hand zoom model

using UnityEngine;

using System.Collections;

public class SelfRotate1 : MonoBehaviour {

private Touch oldTouch1; // Last touch point 1(finger 1)

private Touch oldTouch2; // Last touch point 2(finger 2)

void Start()

{

}

void Update () {

// There is no touch

if ( Input.touchCount <= 0 ){

return;

}

// Multi-touch, zoom in and out

Touch newTouch1 = Input.GetTouch (0);

Touch newTouch2 = Input.GetTouch (1);

// Point 2 just touched the screen, only record, no processing

if( newTouch2.phase == TouchPhase.Began ){

oldTouch2 = newTouch2;

oldTouch1 = newTouch1;

return;

}

// Calculate the distance between the old two points and the new one

float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);

float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

// The difference between the two distances is positive for zoom gesture and negative for zoom gesture

float offset = newDistance – oldDistance;

// Zoom factor, 0.01 times per pixel (100 adjustable)

float scaleFactor = offset / 100f;

Vector3 localScale = transform.localScale;

Vector3 scale = new Vector3(localScale.x + scaleFactor,

localScale.y + scaleFactor,

localScale.z + scaleFactor);

// Minimum zoom to 0.3x

Y > 0.3f && scale.z > 0.3f) {if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f) {

transform.localScale = scale;

}

// Remember the latest touch point for next use

oldTouch1 = newTouch1;

oldTouch2 = newTouch2;

}

}