假如要在這兩個物體中間畫線的話
要做的就是往頂層節點clock上添加組件Line Renderer
添加結束后如圖,比較重要的各個屬性介紹
Cast Shadows 確定線是否投射陰影,是否應從線的一側或兩側投射陰影,或線是否只投射陰影而不被繪制
Positions 這些屬性描述了一個指向connec的向量3點數組就是畫線的終點和起點,里面可以加很多點,此例加兩個點,三點數組,所以我們要得到起點的位置和終點的位置,在clock的代碼中聲明兩個Transform,拖入那兩個節點就行
Color 用漸變來控制線條的顏色
Width 中間的圖是用來調整設置線的寬度
Material 這些屬性描述了用於繪制線條的材料數組。這條線將為數組中的每個材質繪制一次
Corner vertices 此屬性指示在繪制線條中的角時使用多少額外的頂點。增加這個值使線條角看起來更圓
End Cap Vertices此屬性指示在行上使用多少額外的頂點來創建結束符。增加這個值使行帽看起來更圓
Alignment 設置為“View”使線面向攝像機,或設置為“局部”使線基於變換組件的方向進行對齊
Texture Mode 控制紋理如何應用到線條上。使用Stretch將紋理映射應用於整個線條的長度,或者使用Wrap使紋理沿着線條的長度重復。使用材料的平鋪參數來控制重復率
Shadow Bias 沿着光的方向移動陰影,以去除陰影的工件
就這個意思
具體代碼就很好實現了,主要是要每時每刻更新兩個子節點的位置,因為這種東西都是動的
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Line : MonoBehaviour { LineRenderer lineRenderer; public Transform start; public Transform end; void Start() { lineRenderer = GetComponent<LineRenderer>(); } // Update is called once per frame void Update() { lineRenderer.SetPosition(0, start.position); lineRenderer.SetPosition(1, end.position); } }
Details
To create a Line Renderer:
In the Unity menu bar, go to GameObject
Create Empty
In the Unity menu bar, go to Component
Effects > Line Renderer
Drag a Texture or Material onto the Line Renderer. It looks best if you use a Particle Shader in the Material.
Hints
Line Renderers are useful for effects where you need to lay out all the vertices in one frame.
The lines might appear to rotate as you move the Camera. This is intentional when Alignment is set to View. Set Alignment to Local to disable this.
The Line Renderer should be the only Renderer on a GameObject.
Unity samples colors from the Color Gradient at each vertex. Between each vertex, Unity applies linear interpolation to colors. Adding more vertices to your Line Renderer might give a closer approximation of a detailed Color Gradient.