using UnityEngine; public class DragManager : MonoBehaviour { IDrag[] drags; Target[] targets; IDrag currDrag; Target currTarget; bool right; public void Init() { drags = GetComponentsInChildren(true); targets = GetComponentsInChildren(true); for (int i = 0; i < drags.Length; i++) { drags[i].Init(); } for (int i = 0; i < targets.Length; i++) { targets[i].Init(); } } void Update() { if (Input.GetMouseButton(0)) { if (currTarget) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit hit, 1000, 1 << LayerMask.NameToLayer("Target"))) { if (hit.collider.name == currDrag.name) { currTarget.Focus(); right = true; } } else { currTarget.UnFocus(); right = false; } } } if(Input.GetMouseButtonUp(0)) { if (currTarget) { if (right) { currTarget.Execute(); } currTarget.Hide(); currTarget = null; } } } public void Generate(string name) { currDrag = GenerateDrag(name); currTarget = GenerateTarget(name); } IDrag GenerateDrag(string name) { for (int i = 0; i < drags.Length; i++) { if(name == drags[i].name) { drags[i].Show(); return drags[i]; } else { drags[i].Hide(); } } return null; } Target GenerateTarget(string name) { for (int i = 0; i < targets.Length; i++) { if(name == targets[i].name) { targets[i].Show(); return targets[i]; } else { targets[i].Hide(); } } return null; } }