我的環境配置:Windows 10、Unity 5.3.4f1、Java JDK 1.8.0_51
文字漸層這效果其實是很常用到,且非常基本的,但可惜的是 UGUI 並沒有這項功能,所以必須自己製作了。
不過還好網路上有人製作了該功能,並且分享出來。
但他是繼承 BaseVertexEffect 後並製作漸層的效果,但 BaseVertexEffect 在比較新的 UGUI 中已經不能使用了,系統會要求使用 BaseMeshEffect。
所以以下是修改過後的版本
using UnityEngine; using UnityEngine.UI; /// <summary> /// 該效果為 UGUI Text 文字顏色漸層效果. /// </summary> [AddComponentMenu( "UI/Effects/Gradient" )] public class TextGradient : BaseMeshEffect { public Color32 topColor = Color.white; public Color32 bottomColor = Color.black; public override void ModifyMesh( VertexHelper vh ) { float bottomY = -1; float topY = -1; for ( int i = 0; i < vh.currentVertCount; i++ ) { UIVertex v = new UIVertex(); vh.PopulateUIVertex( ref v, i ); if ( bottomY == -1 ) bottomY = v.position.y; if ( topY == -1 ) topY = v.position.y; if ( v.position.y > topY ) topY = v.position.y; else if ( v.position.y < bottomY ) bottomY = v.position.y; } float uiElementHeight = topY - bottomY; for ( int i = 0; i < vh.currentVertCount; i++ ) { UIVertex v = new UIVertex(); vh.PopulateUIVertex( ref v, i ); v.color = Color32.Lerp( bottomColor, topColor, (v.position.y - bottomY) / uiElementHeight ); vh.SetUIVertex( v, i ); } } }
這邊可以直接複製貼上做使用,或者直接點擊文章最下方的腳本下載也可以。
直接將該腳本拉到 Unity 的 Assets 裡面就可以使用了。
使用方式為:
將 TextGradient 腳本拉到 Unity Assets 底下除了 Editor 資料夾以外的任何位置都可以,或者自己建立一個腳本再將程式碼複製貼上也行
點選有掛載 Text 腳本的物件
在選擇 Component > UI > Effects > Gradient 就可以了
效果如下
文章標籤
全站熱搜
留言列表