You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.5 KiB
136 lines
3.5 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ParticleMover : MonoBehaviour
|
|
{
|
|
[SerializeField] private Camera _renderCam;
|
|
[SerializeField] private ParticlePool _particlePool;
|
|
private Dictionary<int, Transform> _pointGroup = new Dictionary<int, Transform>();
|
|
private Queue<Transform> _recycle = new Queue<Transform>();
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
if(Input.GetMouseButtonDown(0))
|
|
{
|
|
AddPoint(-1, Input.mousePosition);
|
|
}
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
UpdatePoint(-1, Input.mousePosition);
|
|
}
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
RemovePoint(-1);
|
|
}
|
|
|
|
foreach (var touch in Input.touches)
|
|
{
|
|
switch (touch.phase)
|
|
{
|
|
case TouchPhase.Began:
|
|
AddPoint(touch.fingerId, touch.position);
|
|
break;
|
|
case TouchPhase.Moved:
|
|
UpdatePoint(touch.fingerId, touch.position);
|
|
break;
|
|
case TouchPhase.Ended:
|
|
RemovePoint(touch.fingerId);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
foreach (var touch in TUIOManager.Instance.touches)
|
|
{
|
|
switch (touch.Value.phase)
|
|
{
|
|
case TouchPhase.Began:
|
|
AddPoint(touch.Key, touch.Value.position);
|
|
break;
|
|
case TouchPhase.Moved:
|
|
UpdatePoint(touch.Key, touch.Value.position);
|
|
break;
|
|
case TouchPhase.Ended:
|
|
RemovePoint(touch.Key);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddPoint(int id, Vector2 pos)
|
|
{
|
|
|
|
Transform newTransform = null;
|
|
if(_pointGroup.TryGetValue(id, out newTransform))
|
|
{
|
|
UpdatePos(newTransform, pos);
|
|
return;
|
|
}
|
|
|
|
if(_recycle.Count > 0)
|
|
{
|
|
newTransform = _recycle.Dequeue();
|
|
}
|
|
else
|
|
{
|
|
newTransform = new GameObject().transform;
|
|
newTransform.parent = transform;
|
|
}
|
|
_pointGroup.Add(id, newTransform);
|
|
UpdatePos(newTransform,pos);
|
|
}
|
|
|
|
public void UpdatePoint(int id, Vector2 pos)
|
|
{
|
|
if (_pointGroup.TryGetValue(id, out var objRect))
|
|
{
|
|
UpdatePos(objRect, pos);
|
|
}
|
|
else
|
|
{
|
|
AddPoint(id, pos);
|
|
}
|
|
}
|
|
|
|
private const float distance = 10;
|
|
|
|
private void UpdatePos(Transform tran, Vector2 pos)
|
|
{
|
|
Vector3 newPos = _renderCam.ScreenToWorldPoint(new Vector3(pos.x,pos.y, distance));
|
|
tran.position = newPos;
|
|
if(tran.childCount == 0)
|
|
{
|
|
if(_particlePool.Take(out var paritcle))
|
|
{
|
|
paritcle.transform.parent = tran;
|
|
paritcle.transform.localPosition = Vector3.zero;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RemovePoint(int id)
|
|
{
|
|
if (_pointGroup.TryGetValue(id, out var obj))
|
|
{
|
|
_pointGroup.Remove(id);
|
|
var paritcle = obj.GetComponentInChildren<ParticleSystem>();
|
|
if(paritcle != null)
|
|
_particlePool.Put(paritcle);
|
|
_recycle.Enqueue(obj);
|
|
|
|
}
|
|
}
|
|
}
|
|
|