Last active
October 8, 2022 09:16
-
-
Save tsubaki/1070a670ad4f1706cdc6e8ffd4d76a58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class DrawLine : MonoBehaviour | |
{ | |
private Queue<Vector3> queue; | |
private Vector3[] positions; | |
private Camera cachedCamera; | |
const int capacity = 100; | |
[SerializeField] | |
private LineRenderer line; | |
void Awake() | |
{ | |
positions = new Vector3[capacity]; | |
queue = new Queue<Vector3>(); | |
cachedCamera = Camera.main; | |
} | |
public void AddPosition(Vector2 pos) | |
{ | |
var targetPos = cachedCamera.ScreenToWorldPoint(new Vector3(pos.x, pos.y, 10)); | |
queue.Enqueue(targetPos); | |
if (queue.Count > capacity) | |
queue.Dequeue(); | |
if (queue.Count != 0) | |
Draw(); | |
} | |
void Draw() | |
{ | |
queue.CopyTo(positions, 0); | |
line.positionCount = queue.Count; | |
line.SetPositions(positions); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.InputSystem.Utilities; | |
public class GetInput1 : MonoBehaviour | |
{ | |
private DrawLine line; | |
private InputAction inputAction; | |
private InputActionTrace trace; | |
void Awake() | |
{ | |
line = GetComponent<DrawLine>(); | |
inputAction = GetComponent<PlayerInput>().currentActionMap["Position"]; | |
trace = new InputActionTrace(); | |
} | |
void OnDestroy() | |
{ | |
trace.Dispose(); | |
} | |
void OnEnable() | |
{ | |
trace.SubscribeTo(inputAction); | |
} | |
void OnDisable() | |
{ | |
trace.UnsubscribeFrom(inputAction); | |
} | |
void Update() | |
{ | |
foreach (var record in trace) | |
{ | |
var v = record.ReadValue<Vector2>(); | |
line.AddPosition(v); | |
} | |
trace.Clear(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class GetInput2 : MonoBehaviour | |
{ | |
private DrawLine line; | |
private InputAction inputAction; | |
void Awake() | |
{ | |
inputAction = GetComponent<PlayerInput>().currentActionMap["Position"]; | |
} | |
void OnEnable() | |
{ | |
inputAction.performed += Input_performed; | |
line = GetComponent<DrawLine>(); | |
} | |
void OnDisable() | |
{ | |
inputAction.performed -= Input_performed; | |
} | |
void Input_performed(InputAction.CallbackContext c) | |
{ | |
var v = c.ReadValue<Vector2>(); | |
line.AddPosition(v); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class GetInput3 : MonoBehaviour | |
{ | |
private DrawLine line; | |
void Awake() | |
{ | |
line = GetComponent<DrawLine>(); | |
} | |
void Update() | |
{ | |
var pos = Input.mousePosition; | |
line.AddPosition(pos); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.InputSystem.LowLevel; | |
using UnityEngine.InputSystem.Utilities; | |
public class GetInput4 : MonoBehaviour | |
{ | |
private DrawLine line; | |
InputStateHistory history; | |
void Awake() | |
{ | |
line = GetComponent<DrawLine>(); | |
history = new InputStateHistory<Vector2>(Mouse.current.position); | |
} | |
void OnDestroy() | |
{ | |
history.Dispose(); | |
} | |
void OnEnable() => history.StartRecording(); | |
void OnDisable() => history.StopRecording(); | |
void Update() | |
{ | |
foreach( var record in history) | |
{ | |
var pos = record.ReadValue<Vector2>(); | |
line.AddPosition(pos); | |
} | |
} | |
} |
Author
tsubaki
commented
Nov 30, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment