├── .gitattributes ├── .github └── FUNDING.yml ├── CanvasController.cs ├── Preview.gif ├── changelog.txt ├── license.md └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: QFSW 2 | patreon: QFSW 3 | custom: paypal.me/QFSW 4 | -------------------------------------------------------------------------------- /CanvasController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | #if UNITY_EDITOR 6 | using UnityEditor; 7 | #endif 8 | 9 | #if UNITY_EDITOR 10 | /// Inspector for the CanvasController component. 11 | [CustomEditor(typeof(CanvasController))] 12 | [ExecuteInEditMode] 13 | public class CanvasControllerInspector : Editor 14 | { 15 | //Fetches or creates the CanvasGroup on enable 16 | private void OnEnable() 17 | { 18 | CanvasController Controller = (CanvasController)target; 19 | Controller.SetCanvasGroup(); 20 | } 21 | } 22 | #endif 23 | 24 | /// Component that provides fade in/out functionality for a canvas group. 25 | [DisallowMultipleComponent] 26 | [RequireComponent(typeof(CanvasGroup))] 27 | public class CanvasController : MonoBehaviour 28 | { 29 | //Private Variables 30 | /// Default speed for fade in and fade out. 31 | [Tooltip("Default speed for fade in and fade out.")] 32 | public float DefaultSpeed = 0.4f; 33 | 34 | /// Automatically fade on activating the GameObject. 35 | [Tooltip("Automatically fade on activating the GameObject.")] 36 | public bool AutoFade = true; 37 | 38 | /// Blocks raycasts whilst fading. 39 | [Tooltip("Blocks raycasts whilst fading.")] 40 | public bool BlockRaycasts = true; 41 | 42 | //Private Variables 43 | /// The CanvasGroup to fade in and out. 44 | private CanvasGroup Elements; 45 | 46 | /// Whether or not the CanvasController is initialised. 47 | private bool Initialised = false; 48 | 49 | //Initialises 50 | private void Awake() { Initialise(); } 51 | 52 | /// Initialises the CanvasController. 53 | public void Initialise() { Initialise(false); } 54 | 55 | /// Sets the CanvasGroup of this CanvasController 56 | public void SetCanvasGroup() 57 | { 58 | if (Elements == null) { Elements = GetComponent(); } 59 | if (Elements == null) { Elements = gameObject.AddComponent(); } 60 | } 61 | 62 | /// Initialises the CanvasController. 63 | /// Re-initialises even if already initialised. 64 | public void Initialise(bool Override) 65 | { 66 | if (!Initialised || Override) 67 | { 68 | SetCanvasGroup(); 69 | Initialised = true; 70 | } 71 | } 72 | 73 | /// Fades out. 74 | public void FadeOut() { FadeOut(DefaultSpeed); } 75 | /// Fades out. 76 | /// Duration of the fade. 77 | public void FadeOut(float Duration) 78 | { 79 | if (gameObject.activeInHierarchy) 80 | { 81 | //Stops any other fading 82 | StopAllCoroutines(); 83 | 84 | //Begins Fade 85 | StartCoroutine(FadeOutComponents(Duration)); 86 | } 87 | } 88 | /// Fades out. 89 | /// Duration of the fade. 90 | IEnumerator FadeOutComponents(float Duration) 91 | { 92 | //Blocks raycasting 93 | bool WasRaycaster = false; 94 | if (BlockRaycasts) 95 | { 96 | WasRaycaster = Elements.blocksRaycasts; 97 | Elements.blocksRaycasts = false; 98 | } 99 | 100 | //Fades CanvasGroup 101 | Elements.alpha = 1f; 102 | while (Elements.alpha > 0) 103 | { 104 | if (Elements.gameObject.activeInHierarchy) { Elements.alpha -= Time.unscaledDeltaTime / Duration; } 105 | yield return null; 106 | } 107 | Elements.alpha = 1f; 108 | 109 | //Finishes fade 110 | if (BlockRaycasts && WasRaycaster) { Elements.blocksRaycasts = true; } 111 | gameObject.SetActive(false); 112 | } 113 | 114 | /// Fades in. 115 | public void FadeIn() { FadeIn(DefaultSpeed); } 116 | /// Fades in. 117 | /// Duration of the fade. 118 | public void FadeIn(float Length) 119 | { 120 | if (gameObject.activeInHierarchy) 121 | { 122 | //Stops any other fading 123 | if (!Initialised) { Initialise(); } 124 | StopAllCoroutines(); 125 | 126 | //Begins Fade 127 | StartCoroutine(FadeInComponents(Length)); 128 | } 129 | } 130 | /// Fades in. 131 | /// Duration of the fade. 132 | IEnumerator FadeInComponents(float Duration) 133 | { 134 | //Blocks raycasting 135 | bool WasRaycaster = false; 136 | if (BlockRaycasts) 137 | { 138 | WasRaycaster = Elements.blocksRaycasts; 139 | Elements.blocksRaycasts = false; 140 | } 141 | 142 | //Fades CanvasGroup 143 | Elements.alpha = 0f; 144 | while (Elements.alpha < 1f) 145 | { 146 | if (Elements.gameObject.activeInHierarchy) { Elements.alpha += Time.unscaledDeltaTime / Duration; } 147 | yield return null; 148 | } 149 | Elements.alpha = 1f; 150 | 151 | //Finishes fade 152 | if (BlockRaycasts && WasRaycaster) { Elements.blocksRaycasts = true; } 153 | } 154 | 155 | //Auto fade in 156 | void OnEnable() { if (AutoFade) { FadeIn(); } } 157 | } 158 | -------------------------------------------------------------------------------- /Preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QFSW/Unity-UiFaderPro/d496ca5e4dc74b8111c84113f35ff252b6e78656/Preview.gif -------------------------------------------------------------------------------- /changelog.txt: -------------------------------------------------------------------------------- 1 | V1.1.0 2 | Re-written to use CanvasGroups for improved performance 3 | 4 | V1.0.0 5 | Initial release -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 QFSW 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # UI Fader Pro V1.0.0 2 | 3 | UiFaderPro is designed to make fading your UI as simple as possible. By attaching a single component to your Canvas or root GameObject, all elements of the tree will automatically be faded in when activated, and automatically faded out when using CanvasControllerInstance.FadeOut(). It is highly versatile and easy to use, with little to no effort required to add to an existing scene. 4 | 5 | ![](https://github.com/QFSW/Unity-UiFaderPro/blob/master/Preview.gif) 6 | 7 | # How to Use It 8 | To use it, simply add the script to each Canvas or root GameObject of the GUI you would like to add fading too (an object with a CanvasController may still have CanvasControllers in the children object for individual control, but it is not necessary if you only want to fade in or out the entire thing). If AutoFade is ticked, then it will automatically fade in when activated. To use the cross fading, replace your SetActive calls with FadeIn and FadeOut. 9 | --------------------------------------------------------------------------------