unity ugui特效實現方式

2021-10-02 06:24:24 字數 4792 閱讀 8794

將unity 粒子系統的每個粒子計算顏色,位置,以及uv計算,對映乙個長方形內,繼承maskablegraphic類,並重寫protected override void onpopulatemesh(vertexhelper vh)方法,將生成的rect 通過vh.adduivertexquad(_quad);畫出。

**:

using unityengine;

using unityengine.ui;

/// /// add by ggr at 2018/07/30

/// ui層的粒子特效,能解決的問題:

/// 1、粒子特效在滾動列表區外裁剪

/// 2、粒子特效層級調整,即可以在夾在任意兩個ui元素之間

/// 3、不同解析度的適配

/// 使用:

/// 主要是特效的美術人員使用。

/// 1、在粒子系統的物體上掛上這個指令碼

/// 2、此時會禁用和清空粒子系統的 renderer 模組

/// 3、把材質球拖到指令碼上的material欄位上

///

[executeineditmode]

[requirecomponent(typeof(canvasrenderer), typeof(particlesystem))]

public class uiparticlesystem : maskablegraphic

}protected bool initialize()

if (psystem == null)

mainmodule = psystem.main;

//最大存活粒子數限制為15,不要太大,影響效率

if (psystem.main.maxparticles > maxparticlecount)

prenderer = psystem.getcomponent();

if (prenderer != null)

currentmaterial = material;

if (currentmaterial && currentmaterial.hasproperty("_maintex"))

material = currentmaterial;

// automatically set scaling

mainmodule.scalingmode = particlesystemscalingmode.hierarchy;

particles = null;

}if (particles == null)

particles = new particlesystem.particle[psystem.main.maxparticles];

imageuv = new vector4(0, 0, 1, 1);

// prepare texture sheet animation

texturesheetanimation = psystem.texturesheetanimation;

texturesheetanimationframes = 0;

texturesheetanimationframesize = vector2.zero;

if (texturesheetanimation.enabled)

return true;

}protected override void awake()

protected override void onpopulatemesh(vertexhelper vh)

}#endif

// prepare vertices

vh.clear();

if (!gameobject.activeinhierarchy)

vector2 temp = vector2.zero;

vector2 corner1 = vector2.zero;

vector2 corner2 = vector2.zero;

// iterate through current particles

int count = psystem.getparticles(particles);

for (int i = 0; i < count; ++i)

else if (texturesheetanimation.frameovertime.curve != null)

else if (texturesheetanimation.frameovertime.constant > 0)

frameprogress = mathf.repeat(frameprogress * texturesheetanimation.cyclecount, 1);

int frame = 0;

switch (texturesheetanimation.animation)

frame %= texturesheetanimationframes;

particleuv.x = (frame % texturesheetanimation.numtilesx) * texturesheetanimationframesize.x;

particleuv.y = mathf.floortoint(frame / texturesheetanimation.numtilesx) * texturesheetanimationframesize.y;

particleuv.z = particleuv.x + texturesheetanimationframesize.x;

particleuv.w = particleuv.y + texturesheetanimationframesize.y;

}temp.x = particleuv.x;

temp.y = particleuv.y;

_quad[0] = uivertex.******vert;

_quad[0].color = color;

_quad[0].uv0 = temp;

temp.x = particleuv.x;

temp.y = particleuv.w;

_quad[1] = uivertex.******vert;

_quad[1].color = color;

_quad[1].uv0 = temp;

temp.x = particleuv.z;

temp.y = particleuv.w;

_quad[2] = uivertex.******vert;

_quad[2].color = color;

_quad[2].uv0 = temp;

temp.x = particleuv.z;

temp.y = particleuv.y;

_quad[3] = uivertex.******vert;

_quad[3].color = color;

_quad[3].uv0 = temp;

if (rotation == 0)

else

vh.adduivertexquad(_quad);}}

void update()}}

void lateupdate()

else}}

if (material == currentmaterial)

return;

psystem = null;

initialize();

}}

shader "17zuoye/ui particle addtive"

_color ("tint", color) = (0.5,0.5,0.5,0.5)

_speedu("橫向速度", float) = 0

_speedv("縱向速度", float) = 0

_coloradd("亮度",float) = 2

_stencilcomp ("stencil comparison", float) = 8

_stencil ("stencil id", float) = 0

_stencilop ("stencil operation", float) = 0

_stencilwritemask ("stencil write mask", float) = 255

_stencilreadmask ("stencil read mask", float) = 255

_colormask ("color mask", float) = 15

[toggle(unity_ui_alphaclip)] _useuialphaclip ("use alpha clip", float) = 0

}subshader

stencil

cull off

lighting off

zwrite off

ztest [unity_guiztestmode]

blend srcalpha one

colormask [_colormask]

pass

;struct v2f

;sampler2d _maintex;

float4 _maintex_st;

fixed4 _color;

fixed4 _texturesampleadd;

float4 _cliprect; // 2d mask 剪裁。

float _speedu;

float _speedv;

float _coloradd;

fixed4 frag(v2f in) : sv_target

endcg}}

}

canvas實現流星特效

控制透明度變化函式 function easeinquad curtime,begin,end,duration 用平方根構建的緩慢減速運動 核心函式 x x 2 x function easeoutquad curtime,begin,end,duration function easeinout...

水波特效的實現

增加波源 俗話說 無風不起浪,為了形成水波,我們必須在水池中加入波源,你可以想象成向水中投入石頭,形成的波源的大小和能量與石頭的半徑和你扔石頭的力量都有關係。知道了這些,那麼好,我們只要修改波能資料緩衝區buf,讓它在石頭入水的地點來乙個負的 尖脈衝 即讓buf x,y n。經過實驗,n的範圍在 3...

UIBlurEffect 毛玻璃特效實現

ios 8 後使用 uiblureffect 類和 uivisualeffectview 類新增毛玻璃特效更加便捷,高效。建立imageview iimageview imageview uiimageview alloc initwithimage uiimage imagenamed 5 建立需...