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.

107 lines
2.7 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointDrawer : MonoBehaviour
{
private CanvasGroup CanvasGroup;
public GameObject PointPrefab;
private Dictionary<int, RectTransform> PointGroup = new Dictionary<int, RectTransform>();
[SerializeField]
private Camera camera;
//Vector2 propertion;
//[AutoUI]
public bool Visible;
//[AutoUI]
private int PointSize = 256;
// Start is called before the first frame update
void Start()
{
CanvasGroup = GetComponent<CanvasGroup>();
//propertion = new Vector2
//((float)camera.targetTexture.width / (float)Screen.width , (float)camera.targetTexture.height / (float)Screen.height);
}
// Update is called once per frame
void Update()
{
CanvasGroup.alpha = Visible ? 1 : 0;
if (Visible == false)
{
if(PointGroup.Count > 0)
{
foreach (var touch in TUIOManager.Instance.touches)
RemovePoint(touch.Key);
}
return;
}
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)
{
RectTransform objRect = null;
if (PointGroup.TryGetValue(id, out objRect))
{
UpdatePoint(id , pos);
}
var obj = Instantiate(PointPrefab, this.transform);
obj.name = id.ToString();
objRect = obj.GetComponent<RectTransform>();
objRect.sizeDelta = Vector2.one * PointSize;
UpdatePos(objRect, pos);
PointGroup.Add(id , objRect);
}
public void UpdatePoint(int id, Vector2 pos)
{
if (PointGroup.TryGetValue(id, out var objRect))
{
UpdatePos(objRect , pos);
}
else
{
AddPoint(id, pos);
}
}
private void UpdatePos(RectTransform tran, Vector2 pos)
{
tran.anchoredPosition = pos;// * propertion;
}
public void RemovePoint(int id)
{
if(PointGroup.TryGetValue(id ,out var obj))
{
PointGroup.Remove(id);
Destroy(obj.gameObject);
}
}
}