Init
This commit is contained in:
68
Assets/Scripts/Drawer.cs
Normal file
68
Assets/Scripts/Drawer.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class Drawer : MonoBehaviour
|
||||
{
|
||||
public LineRenderer lineRenderer;
|
||||
public EdgeCollider2D edgeCollider2D;
|
||||
List<Vector3> points = new List<Vector3>();
|
||||
|
||||
|
||||
bool dragging = false;
|
||||
public void OnMouseDown(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
|
||||
dragging= true;
|
||||
startedPoint = ped.position;
|
||||
lineRenderer.SetPositions(new Vector3[0]);
|
||||
points = new List<Vector3>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Vector3 startedPoint = new Vector3();
|
||||
public void OnMouseDrag(BaseEventData e) {
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
|
||||
points.Add(Camera.main.ScreenToWorldPoint(ped.position));
|
||||
UpdateLine();
|
||||
}
|
||||
|
||||
public void OnMouseUp(BaseEventData e)
|
||||
{
|
||||
dragging= false;
|
||||
FinishDrawing();
|
||||
}
|
||||
|
||||
|
||||
void UpdateLine()
|
||||
{
|
||||
lineRenderer.positionCount = points.Count;
|
||||
lineRenderer.SetPositions(points.ToArray());
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void FinishDrawing()
|
||||
{
|
||||
List<Vector2> points3 = new List<Vector2>();
|
||||
foreach(Vector2 point in points)
|
||||
{
|
||||
points3.Add(point);
|
||||
}
|
||||
edgeCollider2D.SetPoints(points3);
|
||||
edgeCollider2D.GetComponent<Rigidbody2D>().simulated = true;
|
||||
}
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
if (edgeCollider2D == null)
|
||||
{
|
||||
edgeCollider2D = lineRenderer.GetComponent<EdgeCollider2D>();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Drawer.cs.meta
Normal file
11
Assets/Scripts/Drawer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09d7921f3426c1c4ea7ce5f8764d75f4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user