In the UI post-design, we may want to do some special effects on the UI, this article we will learn how to achieve the effect of fading in Unity3d,
First, let's look at the practice of the Unity New UI, which is Ugui.
Observation we will find that each Unity4.6 UI can display a control will have a Canvasrender object, canvasrender what role, we
Look at the official explanation: the canvas Renderer component renders a graphical UI object contained within a Canvas. Simple
The renderer component on the canvas renders a graphical user-interface object that is contained within a canvas, and then takes a closer look at the Canvasrenderer class.
We can see that there are two ways to Setalpha, setcolor, and obviously we can modify the transparency alpha and color to achieve fade out, and I
You can also find that button,text,image and other controls are integrated from the Unity.UI.Graphic
As shown below:
1 Public class text:maskablegraphic, Ilayoutelement 2 3 Public Abstract class maskablegraphic:graphic, Imaskable 4 5 Public class Image:maskablegraphic, Icanvasraycastfilter, Iserializationcallbackreceiver, ilayoutelement
Then we'll read the graphic code, and we'll find there are two ways
1 Public void Crossfadealpha (floatfloatbool ignoretimescale); 2 Public void float BOOL bool usealpha);
Open source source code is as follows:
1 Public voidCrossfadealpha (floatAlphafloatDurationBOOLIgnoretimescale)2 {3Crossfadecolor (Createcolorfromalpha (Alpha), duration, Ignoretimescale,true,false);4 }5 6 Public voidCrossfadecolor (Color Targetcolor,floatDurationBOOLIgnoretimescale,BOOLUsealpha)7 {8Crossfadecolor (Targetcolor, Duration, Ignoretimescale, Usealpha,true);9 }Ten One Private voidCrossfadecolor (Color Targetcolor,floatDurationBOOLIgnoretimescale,BOOLUsealpha,BOOLUsergb) A { - if(Canvasrenderer = =NULL|| (!usergb &&!)usealpha)) - return; the -Color CurrentColor =Canvasrenderer.getcolor (); - if(Currentcolor.equals (targetcolor)) - return; + -Colortween.colortweenmode mode = (Usergb && usealpha?)ColorTween.ColorTweenMode.All:(Usergb?)ColorTween.ColorTweenMode.RGB:ColorTween.ColorTweenMode.Alpha)); + A varColortween =NewColortween {duration = Duration, StartColor = Canvasrenderer.getcolor (), Targetcolor =Targetcolor}; at Colortween.addonchangedcallback (canvasrenderer.setcolor); -Colortween.ignoretimescale =Ignoretimescale; -Colortween.tweenmode =mode; - M_colortweenrunner.starttween (colortween); -}
So we can use Crossfadecolor or crossfadealpha to realize the fade-out.
Here is the client calling code
1 {2 3component[] Comps = Gameobject.find ("/canvas"). Getcomponentsinchildren<component>();4 foreach(Component Cinchcomps)5 {6 if(c isGraphic)7 {8(c asGraphic). Crossfadealpha (0,1,true);9 }Ten } One}
The effect is that all the elements under the canvas will fade out in 1 seconds!
---The original: Unity4.6 UI implements the fade-out (FadeIn FadeOut) Effect---
Here are some of the hints that I've created in the actual project that I've made with the co-process.
1 Internalcoroutine Noticecor;2 Internal voidShownotice ()3 {4 Hidenotice ();5Noticecor =Startcoroutine (Noticeshowscale ());6 }7 8 Internal voidHidenotice ()9 {Ten if(NULL!=Noticecor) One { A Stopcoroutine (Noticecor); -NoticeItme.gameObject.SetActive (false); - } the } - - IEnumerator Noticeshowscale () - { +NoticeItem.gameObject.SetActive (true); - floatTargeta =1; +Noticeitem.getcomponent<canvasrenderer> (). Setalpha (0); A floatTimer =0; at while(timer<6f) - { -Noticeitem.getcomponent<graphic> (). Crossfadealpha (Targeta,1,true); - yield return NewWaitforseconds (1f); -Timer + =1f; -Targeta = Targeta = =0?1:0; in } -NoticeItem.gameObject.SetActive (false); toNoticecor =NULL; +}
Ugui do not use Dotween to create fade-out effects in
Unity3d