-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClickHandler.cs
65 lines (57 loc) · 1.71 KB
/
ClickHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
public class ClickHandler : MonoBehaviour
{
Cursor pointer;
Vector3 screenPoint;
Vector3 offset;
[SerializeField]
GameObject redDot;
GameObject redDotInstance;
[SerializeField]
GameObject pointPos;
GameObject pointPosInstance;
private bool disableControl = false;
Action toggleDotListener;
Action finishListener;
void Start()
{
toggleDotListener = new Action(ToggleDot);
finishListener = new Action(DeleteDots);
EventManager.StartListening("ToggleDots", toggleDotListener);
EventManager.StartListening("Finish", finishListener);
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
if (!disableControl)
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
void ToggleDot()
{
if (redDotInstance == null)
redDotInstance = Instantiate(redDot, transform);
else
Destroy(redDotInstance);
}
void DeleteDots()
{
disableControl = true;
GameObject[] dots = GameObject.FindGameObjectsWithTag("Reddot");
for(int i = 0; i < dots.Length; i++)
{
Destroy(dots[i]);
}
}
}