六、制作红包雨效果

1.新建一个脚本,名为为“RedPacketItem”。(代码如下)

using UnityEngine;

//管理红包的下落和旋转效果
public class RedPacketItem : MonoBehaviour
{
/// <summary>红包存在的边界</summary>
public float boundaryY { get; set; }
/// <summary>移动速度</summary>
public float moveSpeed { get; set; }
/// <summary>旋转速度</summary>
public float rotateSpeed { get; set; }
private bool isShow = true;

private RedPackEffect redPackEffect;

private void OnEnable()
{
isShow = true;
}

private void OnDisable()
{
isShow = false;
}

private void Start()
{
redPackEffect = FindObjectOfType<RedPackEffect>();
}

private void Update()
{
if (!isShow) return;
transform.Translate(new Vector3(0, moveSpeed, 0));

if (transform.localPosition.y < boundaryY)
{
redPackEffect.Death(transform.gameObject);
}

transform.Rotate(new Vector3(0, rotateSpeed, 0));
}
}

2.新建一个脚本,名为为“RedPackEffect”。(代码如下)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>管理红包效果</summary>
public class RedPackEffect : MonoBehaviour
{
/// <summary>模型父物体</summary>
private Transform parent;
/// <summary>红包的预制体</summary>
public GameObject redPackPrefab;
/// <summary>孵化器</summary>
public List<Transform> nestArr = new List<Transform>();

/// <summary>已经死亡的红包(对象池)</summary>
private Queue deathRedPacket = new Queue();

/// <summary>红包下落边界</summary>
public float boundaryY=-1000;
/// <summary>生成红包的速率</summary>
public float rate = 0.2f;
/// <summary>红包下落最小速度</summary>
public float moveMiniSpeed = -3f;
/// <summary>红包下落最大速度</summary>
public float moveMaxSpeed = -10f;
/// <summary>红包旋转最小速度</summary>
public float rotateMiniSpeed = -5;
/// <summary>红包旋转最大速度</summary>
public float rotateMaxSpeed = 5;
/// <summary>最小的红包</summary>
public float miniSize = 1000;
/// <summary>最大的红包</summary>
public float maxSize = 2000;

private void Awake()
{
createParent();
hideNest();

InvokeRepeating("generateRedPack", 0, rate);
}

/// <summary>
/// 红包死亡
/// </summary>
/// <param name="obj">死亡的红包</param>
public void Death(GameObject obj)
{
obj.SetActive(false);
deathRedPacket.Enqueue(obj);
}

/// <summary>隐藏孵化器的显示</summary>
private void hideNest()
{
for(int i=0;i< nestArr.Count;i++)
{
MeshRenderer meshRenderer= nestArr[i].gameObject.GetComponent<MeshRenderer>();
if (meshRenderer != null) meshRenderer.enabled = false;
}
}

/// <summary>创建红包的父物体</summary>
private void createParent()
{
GameObject go = new GameObject("RedPackParent");
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
go.transform.localEulerAngles = Vector3.zero;
parent = go.transform;
}

/// <summary>生成红包效果</summary>
private void generateRedPack()
{
generateOneRedPack();
}

/// <summary>生成一个红包</summary>
private void generateOneRedPack()
{
GameObject obj = getRedPack();
obj.transform.position = getPostion();
obj.transform.localScale = getSize();

RedPacketItem redPacketItem = obj.GetComponent<RedPacketItem>();
if (redPacketItem == null)
{
redPacketItem = obj.AddComponent<RedPacketItem>();
}
redPacketItem.moveSpeed = getMoveSpeed();
redPacketItem.rotateSpeed = getRotateSpeed();
redPacketItem.boundaryY = boundaryY;

}

/// <summary>获取一个红包模型(优先从对象池中获取红包)</summary>
private GameObject getRedPack()
{
if (deathRedPacket == null || deathRedPacket.Count == 0)
{
GameObject item = Instantiate(redPackPrefab, parent);
return item;
}

GameObject obj = (GameObject)deathRedPacket.Dequeue();
return obj;
}

/// <summary>获取模型的大小(随机生成模型大小)</summary>
private Vector3 getSize()
{
float size = Random.Range(miniSize, maxSize);
return new Vector3(size, size, size);
}

/// <summary>生成红包的位置</summary>
private Vector3 getPostion()
{
Transform nest = nestArr[Random.Range(0, nestArr.Count)];
Vector3 nestPos = nest.transform.localPosition;
Vector3 nestSize = nest.transform.localScale;

float x = Random.Range(0, nestSize.x / 2);
float z = Random.Range(0, nestSize.z / 2);
int randomX = Random.Range(0, 2);
int randomZ = Random.Range(0, 2);
x = (randomX == 0) ? nestPos.x + x : nestPos.x - x;
z = (randomZ == 0) ? nestPos.z + z : nestPos.z - z;

return new Vector3(x, nestPos.y, z);
}

/// <summary>获取红包下落的速度</summary>
private float getMoveSpeed()
{
float speed = Random.Range(moveMiniSpeed, moveMaxSpeed);
return speed;
}

/// <summary>获取红包旋转速度</summary>
private float getRotateSpeed()
{
float speed = Random.Range(rotateMiniSpeed, rotateMaxSpeed);
return speed;
}

}

3.新建一个空文件命名为“RedPackEffect”,将“RedPackEffect.cs”挂载上去

4.创建红包孵化模型,用来标记红包孵化的范围

AR红包雨特效 三_AR红包雨

5.给RedPackEffect 类,属性赋值(如下图所示)

AR红包雨特效 三_红包雨_02

6.将“Main Camera”的 Clear Flags 模式改为 Depth only

AR红包雨特效 三_增强现实_03

7.在Canvas下创建一个 Camera ,命名为“UICamera”。

AR红包雨特效 三_红包雨_04

8.将 UICamera 的属性设置如下图

AR红包雨特效 三_AR红包雨_05

9.将 Canvas 的Render Mode 改为 ScreenSpace-Camera,并将Render Camera 赋值为 UICamera(如下图所示)

AR红包雨特效 三_赋值_06

10.大功告成!

文件资料

源码下载链接:  https://pan.baidu.com/s/1t2xQQY-IcGT2GboRUIL-yA 提取码: nwo1

欢迎对AR技术感兴趣的朋友,加入QQ群:883655607 讨论