Unity3D畫直線、畫點外掛Vectrosity簡介

xy849288321發表於2012-12-10
U3D目前發現的一個畫線最好的工具。

畫一條直線
[plain] view plaincopyprint?
// Make Vector2 array; in this case we just use 2 elements...
var linePoints = [Vector2(0, Random.Range(0, Screen.height)), // ...one on the left side of the screen somewhere
Vector2(Screen.width-1, Random.Range(0, Screen.height))]; // ...and one on the right

// Make a VectorLine object using the above points and the default material, with a width of 2 pixels
var line = new VectorLine("Line", linePoints, null, 2.0);

// Draw the line
Vector.DrawLine(line);

畫線我們肯定需要LinePoints,注意:Vector2是Screen以畫素為單位的點,如果用Vector3的話那麼就是world Space裡面的點畫線。
畫直線用Vector.DrawLine,如果給了材質和貼圖,可以Vector.SetTextureScale(line, textureScale)設定一下圖片的scale,
[plain] view plaincopyprint?
// Draw a line from the lower-left corner to the upper-right corner
Vector.SetLine (Color.white, Vector2(0, 0), Vector2(Screen.width-1, Screen.height-1));
以上可以簡單畫一條線,有點像Debug.DrawLine()那樣就畫一條細細的線。同理Debug.DrawRay()也在這裡有Vector.SetRay()也可以,但是SetLine可以用Vector2和Vector3的點,但是SetRay只能是Vector3世界中的點。

ADD+ Real 3D Lines
為什麼會有這個真正的3D線??當我們用SetRay在世界中畫線的時候,這個線在3D物體前面。
[plain] view plaincopyprint?
Vector.SetRay3D()
用以上畫即可。

ADD+Update & Timing
如果在Update裡面不斷呼叫SetRay()和SetLine之類的生成線方法,那麼要注意了。這些線會一直存在!!
[plain] view plaincopyprint?
VectorLine SetLine (Color color, float time, params Vector2[] points)
以上方法第二個引數可以傳一個time進去,多少s會消失掉。

如果要建立1次,然後在update裡面不斷更新這個線,怎麼做?
SetLine和Set……是建立一個VectorLine的Object物件,我們在update(最好在LateUpate)裡面動態改變這個返回值裡面的引數,然後呼叫一下drawLine即可!
PS:在LateUpate裡面每一幀更新會比在Update裡面好。

ADD+active
相比destroy這個物件,我們喜歡關閉和顯示可以直接用
[plain] view plaincopyprint?
myLine.active = false;
myLine.active = true;
那麼關閉時候,使用drawLine將沒有作用。

ADD+MakeLine
Vector.MakeLine這類方法相當於複製一個快捷方式,我們可以重複製作一樣屬性的Line,
在執行MakeLine前面需要執行Vector.SetLineParameters()設定一下和初始屬性不一樣的線,相當於製作一個快捷方式,以後用MakeLine將都一樣。


畫點


和VectorLines一樣,可以new VectorPoints()建立一個點集物件,和VL一樣,可以設定顏色,屬性選項等。
然後用Vector.DrawPoints(myPoints);就可以畫出來了,基本上和vl的概念是一樣的。

ADD+Utilities*****
在Vector類裡面,提供了很多額外的實用工具方法給我們,一般有Set和Get以及Make開頭。
例如SetColor和SetColors……等
標星號,表示這個類裡面提供的這些實用工具除了畫點和線,可以製造我們常用的曲線和方框等。。


ADD+VectorManager
[plain] view plaincopyprint?
// Make a Vector3 array that contains points for a cube that's 1 unit in size
var cubePoints = [Vector3(-0.5, -0.5, 0.5), Vector3(0.5, -0.5, 0.5), Vector3(-0.5, 0.5, 0.5), Vector3(-0.5, -0.5, 0.5), Vector3(0.5, -0.5, 0.5), Vector3(0.5, 0.5, 0.5), Vector3(0.5, 0.5, 0.5), Vector3(-0.5, 0.5, 0.5), Vector3(-0.5, 0.5, -0.5), Vector3(-0.5, 0.5, 0.5), Vector3(0.5, 0.5, 0.5), Vector3(0.5, 0.5, -0.5), Vector3(0.5, 0.5, -0.5), Vector3(-0.5, 0.5, -0.5), Vector3(-0.5, -0.5, -0.5), Vector3(-0.5, 0.5, -0.5), Vector3(0.5, 0.5, -0.5), Vector3(0.5, -0.5, -0.5), Vector3(0.5, -0.5, -0.5), Vector3(-0.5, -0.5, -0.5), Vector3(-0.5, -0.5, 0.5), Vector3(-0.5, -0.5, -0.5), Vector3(0.5, -0.5, -0.5), Vector3(0.5, -0.5, 0.5)];

// Make a line using the above points and material, with a width of 2 pixels
var line = new VectorLine("Cube", cubePoints, Color.white, lineMaterial, 2.0);

// Make this transform have the vector line object that's defined above
// This object is a rigidbody, so the vector object will do exactly what this object does
VectorManager.ObjectSetup (gameObject, line, Visibility.Dynamic, Brightness.None);

以上是實現構造一個3DObject的形狀,需要點集,需要根據這些點構造VL線段,然後再通過VectorManager.ObjectSetup()建立即可。

相關文章