您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在Unity中利用LineRenderer繪制兩點之間的直線,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
private LineRenderer line; //畫線 line = this.gameObject.AddComponent<LineRenderer>(); //只有設置了材質 setColor才有作用 line.material = new Material(Shader.Find("Particles/Additive")); line.SetVertexCount(2);//設置兩點 line.SetColors(Color.yellow, Color.red); //設置直線顏色 line.SetWidth(0.01f, 0.01f);//設置直線寬度 //設置指示線的起點和終點 line.SetPosition(0, initPosition); line.SetPosition(1, newPosition);
下面是以物體position為圓心,半徑為R,在xz平面上的畫圓
public float R;//半徑 public int N;//不要超過45 line.SetVertexCount(N+1);//這里要加1,達成閉合曲線 for (int i = 0; i < N + 1; i++) { float x = R * Mathf.Cos((360 / N * i) * Mathf.Deg2Rad) + transform.position.x; //確定x坐標 float z = R * Mathf.Sin((360 / N * i) * Mathf.Deg2Rad) + transform.position.z; //確定z坐標 line.SetPosition(i, new Vector3(x, transform.position.y, z)); }
補充:Unity 通過LineRenderer組件畫線段
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DrawLineByMouse : MonoBehaviour { //畫線組件預制體 public Transform gestureOnScreenPrefab; private int strokeId = -1; private Vector3 virtualKeyPosition = Vector2.zero; private Rect drawArea; private RuntimePlatform platform; private int vertexCount = 0; private List<LineRenderer> gestureLinesRenderer = new List<LineRenderer>(); private LineRenderer currentGestureLineRenderer; //GUI private string message="what is this ?"; private bool recognized; private string newGestureName = ""; void Start() { platform = Application.platform; drawArea = new Rect(0, 0, Screen.width - Screen.width / 3, Screen.height); } void Update() { if (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer) { if (Input.touchCount > 0) { virtualKeyPosition = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y); } } else { if (Input.GetMouseButton(0)) { virtualKeyPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y); } } if (drawArea.Contains(virtualKeyPosition)) { if (Input.GetMouseButtonDown(0)) { ++strokeId; // Transform tmpGesture = Instantiate(gestureOnScreenPrefab, transform.position, transform.rotation) as Transform; // currentGestureLineRenderer = tmpGesture.GetComponent<LineRenderer>(); GameObject go = new GameObject("LineRenderer"); go.transform.position = Camera.main.transform.position; go.transform.rotation = Camera.main.transform.rotation; currentGestureLineRenderer = go.AddComponent<LineRenderer>(); currentGestureLineRenderer.startWidth = 0.1f; gestureLinesRenderer.Add(currentGestureLineRenderer); vertexCount = 0; } if (Input.GetMouseButton(0)) { currentGestureLineRenderer.SetVertexCount(++vertexCount); currentGestureLineRenderer.SetPosition(vertexCount - 1, Camera.main.ScreenToWorldPoint(new Vector3(virtualKeyPosition.x, virtualKeyPosition.y, 10))); } } } void OnGUI() { GUI.Box(drawArea, "Draw Area"); GUI.Label(new Rect(10, Screen.height - 40, 500, 50), message); if (GUI.Button(new Rect(Screen.width - 100, 10, 100, 30), "clear line")) { foreach (LineRenderer lineRenderer in gestureLinesRenderer) { lineRenderer.SetVertexCount(0); Destroy(lineRenderer.gameObject); } gestureLinesRenderer.Clear(); recognized = true; } } }
關于怎么在Unity中利用LineRenderer繪制兩點之間的直線就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。