Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active Nov 30, 2019
Embed
What would you like to do?
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);
}
}
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();
}
}
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);
}
}
using UnityEngine;
public class GetInput3 : MonoBehaviour
{
private DrawLine line;
void Awake()
{
line = GetComponent<DrawLine>();
}
void Update()
{
var pos = Input.mousePosition;
line.AddPosition(pos);
}
}
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);
}
}
}
@tsubaki

This comment has been minimized.

Copy link
Owner Author

@tsubaki tsubaki commented Nov 30, 2019

anim (50)
anim (51)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment