So I have this helper script that I use to move points around manually while debugging:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class MovablePoints : MonoBehaviour
{
public List<Vector2> points = new List<Vector2>();
public List<int> moveablePoints = new List<int>();
[Range(.01f, 1f)] public float selectionRadius = .05f;
public bool selectionActive;
public Vector2 clickedPos;
public List<Vector2> offsets = new List<Vector2>();
private void Update()
{
Vector2 mosPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
clickedPos = mosPos;
for (int i = 0; i < points.Count; i++)
{
Vector2 point = points[i];
if (IsPointInRange(point, mosPos))
{
if (!moveablePoints.Contains(i))
{
offsets.Add(point - clickedPos);
moveablePoints.Add(i);
}
}
}
}
if (Input.GetMouseButton(0))
{
for (int i = 0; i < moveablePoints.Count; i++)
{
Vector2 offset = offsets[i];
points[moveablePoints[i]] = mosPos + offset;
}
}
if (Input.GetMouseButtonUp(0))
{
offsets.Clear();
moveablePoints.Clear();
}
}
[Range(.001f, .1f)] public float gizmoSize = .05f;
void OnDrawGizmos()
{
if (selectionActive)
{
Color selectionColor = Color.white;
selectionColor.a = 0.65f;
Handles.color = selectionColor;
if (Input.GetAxis("Mouse ScrollWheel") > 0f && selectionRadius < 1)
selectionRadius += .01f;
if (Input.GetAxis("Mouse ScrollWheel") < 0f && selectionRadius > 0)
selectionRadius -= .01f;
Vector2 mosPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Handles.DrawWireDisc(mosPos, new Vector3(0, 0, 1), selectionRadius);
for (int i = 0; i < points.Count; i++)
{
Vector2 point = points[i];
if (IsPointInRange(point, mosPos))
{
if (Input.GetMouseButton(0) && moveablePoints.Contains(i))
Gizmos.color = Color.green;
else
Gizmos.color = Color.red;
}
else
Gizmos.color = Color.white;
Gizmos.DrawSphere(point, gizmoSize);
}
}
}
bool IsPointInRange(Vector2 point, Vector2 mosPos)
{
float dx = Mathf.Abs(point.x - mosPos.x);
float dy = Mathf.Abs(point.y - mosPos.y);
if (dx > selectionRadius)
return false;
if (dy > selectionRadius)
return false;
if (dx + dy <= selectionRadius)
return true;
if (Mathf.Pow(dx, 2) + Mathf.Pow(dy, 2) <= Mathf.Pow(selectionRadius, 2))
return true;
return false;
}
}
This an example of how I use and subscribe points to the "Moveable Points"
public Vector2 A = new Vector2();
public Vector2 B = new Vector2();
public Vector2 C = new Vector2();
private void Start()
{
movablePoints.points.Add(A);
movablePoints.points.Add(B);
movablePoints.points.Add(C);
}
void Update()
{
A = movablePoints.points[0];
B = movablePoints.points[1];
C = movablePoints.points[2];
}
However, if multiple scripts are adding points to the MoveablePoints Script, it gets quite hard to remember what index is where. Is there a way to have the MoveablePoints script update the values of the subscriber automatically? Perhaps through reference?

