Forgot password?
 立即注册
EasyAR Portal Tutorial View Content

EasyAR Developing Cases---AR Red Package(fundemental)

AlbertLee 2017-8-16 09:53

DevelopIntriguingAppswith EasyAR SDKDevelop AR red package with EasyAR SDKPreview:Step 1:Set up developing environmentDownload EasyAR SDK (Unity) and import it into Unity. Apply key from official web ...

Develop Intriguing Apps with EasyAR SDK

Develop AR red package with EasyAR SDK


Preview



RedPackage : https://drive.google.com/open?id=0B6vjKDkZUGVsNUl2RS1GMkJVZDA

Resources: https://drive.google.com/open?id=0B6vjKDkZUGVsR3FOQllTR25oM0U

NGUI : https://drive.google.com/open?id=0B6vjKDkZUGVsczJEa1MtdlJuN0k


Step 1:Set up developing environment

Download EasyAR SDK (Unity) and import it into Unity. Apply key from official website, delete Camera, drag EasyAR Startup into hierarchy and fill in the key. Note:t we are not using recognition function, so we don’t need ImageTarget.


Next, prepare red package model. Some people may lose their texture while importing, and we only need to add texture to the model again.


Here, we prepared two red package model to achieve different interactions. Modify their size to separate them. I name them Hong and HongBao. The detail parameters are like below

Hong:


HongBao:


Next, I add Tag to both red packages Hong and HongBao.

 



Add BoxCollider to both models. Tick Trigger, and modify size.

Finally, add an animation to our HongBao. Click model HongBao, find Window-Animation, and click Create after opened. 


Click Add Propertychoose Transfrom-Scale


Next, choose a frame, and change its parameters. You should enlarge the parameter to achieve shaking effect.


Step 2:Produce Red package

Create several random points, name them point1, point2, point3. They are the locations where red packages land. Reference parameters are like below. You can also DIY it.

point1:


point2:


point3:



Next, create a script to make the red package land. You can use Translate to achieve it. You can also use other ways, like Rigidbody. (Notes: destroy red package when Z coordinate is less than -8.

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class Move : MonoBehaviour {

public GameObject par;

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

transform.Translate (-transform.forward*2f*Time.deltaTime);

if (transform.position.z < -8f) {

Destroy (this.gameObject);

}

}

}

Next, create CreateHong object, put CreateHong.cs script onto it to generate random red package.

using UnityEngine;

using System.Collections;

public class CreateHong : MonoBehaviour {

public Transform[] points;

public GameObject[] hongbaos;

private int index;

// Use this for initialization

void Start () {

InvokeRepeating ("CreateHongbao",1f,1f);

}

// Update is called once per frame

void Update () {

}

void CreateHongbao(){

int i = Random.Range (0, 10);

if (i > 1) {

index = 0;

} else {

index = 1;

}

GameObject go = GameObject.Instantiate (hongbaos [index], points [Random.Range (0, points.Length)].position + new Vector3 (Random.Range (-0.5f, 0.5f), 0, 0), Quaternion.identity) as GameObject;

go.transform.Rotate (new Vector3 (270, 180, 0));

}

}

}

Step 3:Interaction

When clicking the shaking red package, it produces a cool particle effect. Add it into Move.cs like below.

void OnMouseDown(){

if (gameObject.tag == "Hong") {

Debug.Log ("ddd");

} else if(gameObject.tag=="HongBao") {

CreateHong._instace.isCreate = false;

GameObject go=GameObject.Instantiate (par,gameObject.transform.position,Quaternion.identity) as GameObject;

Destroy (go,2f);

}

}

Destroy the particles after two seconds.

Next, use NGUI plug-in to show coupons or red packages. (Learn the ideas)

Effects are like below

  


First, create Sprite

 

Add TweenScale

 

Notes:

 


Next, we can use Singleton pattern to achieve it in CreateHong.cs script.

First declare

publicstaticCreateHong_instace;

Next

void Awake()

{

_instace = this;

}


public void PlayScale()

{

daxiao.gameObject.SetActive (true);

daxiao.PlayForward ();

}

Re write them in Move.CS:

void OnMouseDown()

{

if (gameObject.tag == "Hong") {

Debug.Log ("ddd");

} else if(gameObject.tag=="HongBao") {