第一部分:前言
实现功能:打开APP或运行该demo后,会从天而降红包,有些红包是空的(大一点的),抖动的红包里面“有钱”,点击之后会产生相应的交互。
第二部分:预览
所用到的资源:
红包模型:链接: https://pan.baidu.com/s/1pKWvqsj 密码: x4h2
粒子特效:链接: https://pan.baidu.com/s/1eSxIbmi 密码: 5suh
NGUI插件:链接: https://pan.baidu.com/s/1hsMa0Ig 密码: uh3a
第三部分:开发环境搭建
首先我们设置下开发环境,我们最终导出的是 APK文件,所以我们playSetting为Android,并修改屏幕大小为480x800。
接着我们下载EasyAR SDK (unity版本)并导入到unity中,并到官网申请开发时所用到的Key值,在unity中,删除原有的Camera,将EasyAR_Startup 拖入到面板中,并将key之填入。注意:在这里我们并没有用到图像的识别,因此没必要用ImageTarget。
<ignore_js_op>
接下来,我们准备红包模型,有些人在导入红包模型的过程中可能会遇到贴图丢失的情况,在这里,我们只需将红包贴图重新挂到材质上即可。
<ignore_js_op>
在这里,我们准备两个红包预制体,来实现不同的交互。并修改它们的大小以便区分。在这里我给他们命名分别为Hong,HongBao。具体详细参数如下
Hong:
HongBao:
<ignore_js_op>
<ignore_js_op>
接下来,我们给两个红包添加Tag,分别为Hong,HongBao。
<ignore_js_op>
<ignore_js_op>
为两个红包预制体添加BoxCollider,并勾选Trigger。大小自己调节。
最后,我们为我们所交互的那个红包HongBao添加个动画。选中它,并在菜单栏Window-,打开后,点击Create,并保存命名。
<ignore_js_op>
接着点Add Property,选Transfrom-Scale
<ignore_js_op>
接下来,在中间某一帧选中,并改变大小,值应该大一点,这样才会有抖动的效果:
<ignore_js_op>
第四部分:编写代码产生AR红包
首先我们先创建几个随机点,分别命名point1,point2,point3,这是红包所降落的位置。参考数值如下:大家可以自行设置
point1:
<ignore_js_op>
point2:
<ignore_js_op>
point3:
<ignore_js_op>
接下来,我们创建一段代码来使得红包可以降落,在这里用Translate来实现,当然大家可以用其他方法,比如添加Rigidbody,给个受力也可以,不过那样有点麻烦。(补充:当红包的Z坐标小于-8时,就销毁)
[AppleScript] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
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); } }} |
接下来,创建CreateHong空物体,在上面挂上CreateHong.cs脚本,实现随机产生红包。
[AppleScript] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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)); } } } |
第五部分:实现交互
我们实现当点击普通红包时由于时间关系我没做任何处理,当点击抖动红包时我们产生炫酷的粒子特效,将如下方法添加到Move.cs中
[AppleScript] 纯文本查看 复制代码
|
1
2
3
4
5
6
7
8
9
|
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); } } |
并在2s后销毁该粒子
好了,接下来,我们用NGUI插件实现产生优惠卷或红包(这不重要,重要的是实现思路与方法)
效果如下
首先,我们创建Sprite
接着添加TweenScale
<ignore_js_op>
注意:
接下来我们使用单例模式在CreateHong.cs脚本中实现:
首先声明:
public static CreateHong _instace;
接着:
[AppleScript] 纯文本查看 复制代码
|
1
2
3
4
|
void Awake() { _instace = this; } |
然后实现方法供外界调用
[AppleScript] 纯文本查看 复制代码
|
1
2
3
4
|
public void PlayScale() { daxiao.gameObject.SetActive (true); daxiao.PlayForward (); } |
在Move.CS中实现:
[AppleScript] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
|
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); CreateHong._instace.PlayScale (); } } |
第六部分:完整代码
Move.cs(挂在红包上):
[AppleScript] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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); } } 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); CreateHong._instace.PlayScale (); } }} |
CreateHong.cs:
[AppleScript] 纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
using UnityEngine;using System.Collections;public class CreateHong : MonoBehaviour { public static CreateHong _instace; public Transform[] points; public GameObject[] hongbaos; private int index; public bool isCreate; public TweenScale daxiao; void Awake() { _instace = this; isCreate = true; } // Use this for initialization void Start () { InvokeRepeating ("CreateHongbao",1f,1f); } // Update is called once per frame void Update () { } void CreateHongbao(){ if (isCreate) { 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)); } else { return; } } public void PlayScale() { daxiao.gameObject.SetActive (true); daxiao.PlayForward (); }} |