LineRenderer Line renderers are mainly used in 3D Render line segments in , Pay attention here LineRenderer The two endpoints of the rendered line segment are 3D Points in the world , That is, it belongs to the world coordinates (World
Space) In .

Here I set up a set of radio buttons , Control line color and thickness through radio button . For the convenience of viewing lines , I created a new one Plane As background , According to the position when the mouse is pressed , To continue drawing line segments

The following is the main code
using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.EventSystems; public class MsPaint : MonoBehaviour { // line color
private Color paintColor = Color.red; // Line thickness private float paintSize = 0.1f;
// Used to store mouse position private List<Vector3> paintPos = new List<Vector3>(); private bool
isPressed;// Long press the mouse private LineRenderer ren; private Vector3 lastPos; // Line material
public Material material; #region Radio click events public void OnRedChange(bool isOn) {
if (isOn) { paintColor = Color.red; } } public void OnGreenChange(bool isOn) {
if (isOn) { paintColor = Color.green; } } public void OnBlueChange(bool isOn) {
if (isOn) { paintColor = Color.blue; } } public void OnPaint1(bool isOn) { if
(isOn) { paintSize = 0.1f; } } public void OnPaint2(bool isOn) { if (isOn) {
paintSize = 0.2f; } } public void OnPaint4(bool isOn) { if (isOn) { paintSize =
0.4f; } } #endregion private void Update() { if (Input.GetMouseButtonDown(0)) {
if (EventSystem.current.IsPointerOverGameObject()) { // Judge whether to click UI return; }
// Create an empty object , Add it to him dynamically LineRenderer assembly GameObject line = new GameObject();
line.transform.SetParent(transform); ren = line.AddComponent<LineRenderer>();
ren.material = material;// Set material ren.startColor = paintColor;// Set color ren.endColor
= paintColor; ren.startWidth = paintSize;// Sets the width of the line ren.endWidth = paintSize;
ren.numCapVertices = 2;// Set end smoothness ren.numCornerVertices = 2;// Set corner smoothness , More vertices, smoother
lastPos = GetMousePosition();// Gets the position of the mouse in world coordinates paintPos.Add(lastPos);
ren.positionCount = paintPos.Count;// Sets the number of points that make up the line
ren.SetPositions(paintPos.ToArray()); isPressed = true; } if (isPressed) {
// Long press the mouse to start drawing , The distance between two points is greater than 0.1 Start adding points if (Vector3.Distance(lastPos, GetMousePosition()) >
0.1f) { paintPos.Add(GetMousePosition()); lastPos = GetMousePosition(); }
ren.positionCount = paintPos.Count; ren.SetPositions(paintPos.ToArray()); } if
(Input.GetMouseButtonUp(0)) { isPressed = false; paintPos.Clear(); ren = null;
} } /// <summary> /// Get mouse position , Convert screen coordinates to world coordinates /// </summary> /// <returns></returns>
private Vector3 GetMousePosition() { Ray ray =
Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; bool
isCollider = Physics.Raycast(ray, out hit); if (isCollider) { return hit.point
- new Vector3(0, 0, 0.01f);// Avoid occlusion } return Vector3.zero; } }
 

Technology