using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using LuaFramework;
|
|
using System;
|
|
namespace LuaFramework
|
|
{
|
|
public class ParticleCollide : MonoBehaviour
|
|
{
|
|
private class Data
|
|
{
|
|
public float start_time;
|
|
public float elapse_time;
|
|
public int id;
|
|
public GameObject go;
|
|
}
|
|
|
|
private float del_time = 0.5f; //碰撞产生的粒子特效
|
|
private GameObject part_new; //碰撞特效销毁时间
|
|
|
|
private ParticleSystem part;
|
|
private Queue<GameObject> pool = new Queue<GameObject>();
|
|
private List<Data> list = new List<Data>();
|
|
private bool isInit = false;
|
|
private int id = 0;
|
|
private string resName = "";
|
|
private string abName = "";
|
|
private ResourceManager resMgr = null;
|
|
private Transform partRoot = null;
|
|
private List<ParticleCollisionEvent> collisionEvents = new List<ParticleCollisionEvent>();
|
|
|
|
|
|
public void InitData(string ab_name, string res_name, float time)
|
|
{
|
|
if (isInit) return;
|
|
isInit = true;
|
|
abName = ab_name;
|
|
resName = res_name;
|
|
del_time = time;
|
|
part = transform.GetComponent<ParticleSystem>();
|
|
resMgr = LuaHelper.GetResManager();
|
|
PanelManager panelMgr = LuaHelper.GetPanelManager();
|
|
if(panelMgr != null)
|
|
{
|
|
partRoot = panelMgr.GetParent("SceneContainer");
|
|
}
|
|
|
|
if (resMgr != null )
|
|
{
|
|
Action<UnityEngine.Object[]> call_back = (obj) =>
|
|
{
|
|
if (obj != null && obj[0] != null)
|
|
{
|
|
part_new = obj[0] as GameObject;
|
|
}
|
|
};
|
|
|
|
resMgr.LoadPrefab(resName, abName, call_back, 1);
|
|
}
|
|
}
|
|
|
|
|
|
public void DeleteMe()
|
|
{
|
|
isInit = false;
|
|
abName = "";
|
|
resName = "";
|
|
del_time = 0;
|
|
resMgr = null;
|
|
part = null;
|
|
partRoot = null;
|
|
id = 0;
|
|
part_new = null;
|
|
if (list.Count > 0)
|
|
{
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
Data d = list[i];
|
|
UnityEngine.Object.Destroy(d.go);
|
|
}
|
|
list.Clear();
|
|
}
|
|
while (pool.Count > 0)
|
|
{
|
|
GameObject go = pool.Dequeue();
|
|
UnityEngine.Object.Destroy(go);
|
|
}
|
|
}
|
|
|
|
|
|
//发生粒子碰撞的回调函数
|
|
private void OnParticleCollision(GameObject other)
|
|
{
|
|
if (part_new == null) return;
|
|
|
|
if (part == null) return;
|
|
|
|
int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
|
|
int i = 0;
|
|
while (i < numCollisionEvents)
|
|
{
|
|
GameObject go = GetGameObject();
|
|
go.gameObject.SetActive(true);
|
|
go.transform.position = collisionEvents[i].intersection;
|
|
|
|
Data d = new Data();
|
|
d.go = go;
|
|
d.start_time = 0;
|
|
d.elapse_time = 0;
|
|
d.id = id;
|
|
id++;
|
|
list.Add(d);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
private GameObject GetGameObject()
|
|
{
|
|
GameObject go = null;
|
|
if (pool.Count > 0)
|
|
{
|
|
go = pool.Dequeue();
|
|
}
|
|
else
|
|
{
|
|
go = GameObject.Instantiate(part_new) as GameObject;
|
|
if (partRoot != null)
|
|
{
|
|
go.transform.parent = partRoot;
|
|
}
|
|
}
|
|
return go;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isInit) return;
|
|
|
|
if (list.Count > 0)
|
|
{
|
|
List<int> del = new List<int>();
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
var d = list[i];
|
|
d.elapse_time += Time.deltaTime;
|
|
if (d.elapse_time > del_time)
|
|
{
|
|
del.Add(d.id);
|
|
}
|
|
}
|
|
if (del.Count > 0)
|
|
{
|
|
for (int i = 0; i < del.Count; i++)
|
|
{
|
|
for (int k = 0; k < list.Count; k++)
|
|
{
|
|
if (list[k].id == del[i])
|
|
{
|
|
Data d = list[k];
|
|
var go = d.go;
|
|
go.SetActive(false);
|
|
pool.Enqueue(go);
|
|
list.Remove(d);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|