-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawLine.cs
77 lines (66 loc) · 2.09 KB
/
DrawLine.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
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class DrawLine : NetworkBehaviour
{
public GameObject linePrefab;
public GameObject currentLine;
public LineRenderer lineRender;
public List<Vector2> mousePositions;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Button Pressed!");
CmdDrawOnServer();
}
if (Input.GetMouseButton(0))
{
Vector2 tempMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Vector2.Distance(tempMousePosition, mousePositions[mousePositions.Count - 1]) > .1f)
{
CmdUpdateLineOnServer(tempMousePosition);
}
}
}
[Command]
private void CmdDrawOnServer()
{
CreateLine();
RpcDrawOnClient();
}
[ClientRpc]
private void RpcDrawOnClient()
{
CreateLine();
}
[Command]
private void CmdUpdateLineOnServer(Vector2 newMousePosition)
{
UpdateLine(newMousePosition);
RpcUpdateLineOnClient(newMousePosition);
}
[ClientRpc]
private void RpcUpdateLineOnClient(Vector2 newMousePosition)
{
UpdateLine(newMousePosition);
}
void CreateLine()
{
currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
lineRender = currentLine.GetComponent<LineRenderer>();
mousePositions.Clear();
mousePositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
mousePositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
lineRender.SetPosition(0, mousePositions[0]);
lineRender.SetPosition(1, mousePositions[1]);
}
void UpdateLine(Vector2 newMousePosition)
{
mousePositions.Add(newMousePosition);
lineRender.positionCount++;
lineRender.SetPosition(lineRender.positionCount - 1, newMousePosition);
}
}