Unity3D UGUI实现翻书特效
本文实例为大家分享了Unity3DUGUI翻书展示的具体代码,供大家参考,具体内容如下
参考大佬的,链接找不到了,找到了再加在这。
下边是Shader代码:
//UpgradeNOTE:replaced'mul(UNITY_MATRIX_MVP,*)'with'UnityObjectToClipPos(*)' Shader"Personal/PageTurning"{ Properties { _Color("Color",Color)=(1,1,1,1) _MainTex("MainTex",2D)="White"{} _SecTex("SecTex",2D)="White"{} _Angle("Angle",Range(0,180))=0 _Warp("Warp",Range(0,10))=0 _WarpPos("WarpPos",Range(0,1))=0 _Downward("Downward",Range(0,1))=0 } SubShader { pass { CullBack CGPROGRAM #pragmavertexvert #pragmafragmentfrag #include"UnityCG.cginc" structv2f { float4pos:POSITION; float2uv:TEXCOORD0; }; fixed4_Color; float_Angle; float_Warp; float_Downward; float_WarpPos; sampler2D_MainTex; float4_MainTex_ST; v2fvert(appdata_basev) { v2fo; v.vertex+=float4(5,0,0,0); floats; floatc; sincos(radians(-_Angle),s,c); float4x4rotate={ c,s,0,0, -s,c,0,0, 0,0,1,0, 0,0,0,1}; floatrangeF=saturate(1-abs(90-_Angle)/90); v.vertex.y+=-_Warp*sin(v.vertex.x*0.4-_WarpPos*v.vertex.x)*rangeF; v.vertex.x-=rangeF*v.vertex.x*_Downward; v.vertex=mul(rotate,v.vertex); v.vertex+=float4(-5,0,0,0); o.pos=UnityObjectToClipPos(v.vertex); o.uv=TRANSFORM_TEX(v.texcoord,_MainTex); returno; } fixed4frag(v2fi):COLOR { fixed4color=tex2D(_MainTex,-i.uv); return_Color*color; } ENDCG } pass { CullFront CGPROGRAM #pragmavertexvert #pragmafragmentfrag #include"UnityCG.cginc" structv2f { float4pos:POSITION; float2uv:TEXCOORD0; }; fixed4_Color; float_Angle; float_Warp; float_Downward; float_WarpPos; sampler2D_SecTex; float4_MainTex_ST; v2fvert(appdata_basev) { v2fo; v.vertex+=float4(5,0,0,0); floats; floatc; sincos(radians(-_Angle),s,c); float4x4rotate={ c,s,0,0, -s,c,0,0, 0,0,1,0, 0,0,0,1}; floatrangeF=saturate(1-abs(90-_Angle)/90); v.vertex.y+=-_Warp*sin(v.vertex.x*0.4-_WarpPos*v.vertex.x)*rangeF; v.vertex.x-=rangeF*v.vertex.x*_Downward; v.vertex=mul(rotate,v.vertex); v.vertex+=float4(-5,0,0,0); o.pos=UnityObjectToClipPos(v.vertex); o.uv=TRANSFORM_TEX(v.texcoord,_MainTex); returno; } fixed4frag(v2fi):COLOR { float2uv=i.uv; uv.x=-uv.x; fixed4color=tex2D(_SecTex,-uv); return_Color*color; } ENDCG } } }
下面是UI代码:
usingSystem.Collections; usingUnityEngine; usingUnityEngine.UI; publicclassFanShuUI:UIBase { privateGameObjectPlane; privateMaterialm_Material; privateCoroutineeffect; privateImageLeftPage; privateImageRightPage; privatevoidAwake() { InitUI(); } publicoverridevoidInitUI() { Plane=GetGameObject("Plane"); LeftPage=GetComp("LeftPage"); RightPage=GetComp ("RightPage"); Plane.SetActive(false); m_Material=Plane.GetComponent ().material; } publicvoidPlayPageTurnEffect(boolisLeft=true) { if(!gameObject.activeSelf) { return; } if(effect!=null) { StopCoroutine(effect); } effect=StartCoroutine(FanShuEffect(0.5f,isLeft)); } publicvoidShowRightImage(stringright) { RightPage.gameObject.SetActive(true); RightPage.sprite=ResourcesMgr.Instance.LoadObj (right); } publicvoidShowLeftImage(stringleft) { LeftPage.gameObject.SetActive(true); LeftPage.sprite=ResourcesMgr.Instance.LoadObj (left); } privateIEnumeratorFanShuEffect(floattime,boolisLeft) { LeftPage.gameObject.SetActive(false); RightPage.gameObject.SetActive(false); Plane.SetActive(true); intangle=(int)(180*0.1f); for(inti=0;i<10;i++) { if(isLeft) { m_Material.SetFloat("_Angle",angle*i); } else { m_Material.SetFloat("_Angle",180-angle*i); } yieldreturnnewWaitForSeconds(time*0.1f); } if(isLeft) { m_Material.SetFloat("_Angle",180); } else { m_Material.SetFloat("_Angle",0); } Plane.SetActive(false); OnEffectOver(); } privatevoidOnEffectOver() { //--callback } }
左右两页纸可以在翻书结束动态加载图片。
下边是Plane的面板信息:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。