Introduction and use of Shadow (shadow) components of Unity UGUI
1. What is the Shadow component?
The Shadow component is a special effect component in Unity UGUI for adding shadow effects on UI elements. By adjusting the properties of the shadow color, offset, blur, etc., the UI elements can look more three-dimensional and layered.
2. How the Shadow component works
The Shadow component achieves the shadow effect by drawing a shadow image below the UI element that is the same shape but slightly enlarged as the UI element. The color, offset, and blur of the shadow image can be adjusted by properties.
3. Common properties of Shadow (shadow) component
- Effect Color: The color of the shadow.
- Effect Distance: The offset distance of the shadow.
- Use Graphic Alpha: Whether to use the transparency of UI elements as the transparency of the shadow.
- Blur: The degree of blur of the shadow.
- Blur Distance: Blur distance of shadows.
4. Common functions of Shadow (shadow) component
- ModifyMesh: Modify the grid of UI elements to draw shadow images.
5. Complete example code
Example 1: Adding a shadow effect
using UnityEngine; using ; public class AddShadowExample : MonoBehaviour { public Text text; public Shadow shadow; void Start() { shadow = <Shadow>(); = true; } }
Operation steps:
- Create a Text object and mount the script to the object.
- Drag the Text object to the script
text
in variable. - In the Start function, get the Shadow component on the Text object and place it
enabled
The property is set to true.
Things to note:
- Before using Shadow components, you need to make sure that there is already a Graphic component (such as Text, Image, etc.) on the UI element.
Example 2: Adjust the shade color
using UnityEngine; using ; public class ChangeShadowColorExample : MonoBehaviour { public Text text; public Shadow shadow; void Start() { shadow = <Shadow>(); = true; = ; } }
Operation steps:
- Create a Text object and mount the script to the object.
- Drag the Text object to the script
text
in variable. - In the Start function, get the Shadow component on the Text object and place it
enabled
The property is set to true. - Put Shadow component's
effectColor
The property is set to red.
Things to note:
-
effectColor
The property accepts a value of Color type, and the color of the shadow can be adjusted by setting different RGB values.
Example 3: Adjust shadow offset
using UnityEngine; using ; public class ChangeShadowOffsetExample : MonoBehaviour { public Text text; public Shadow shadow; void Start() { shadow = <Shadow>(); = true; = new Vector2(5, -5); } }
Operation steps:
- Create a Text object and mount the script to the object.
- Drag the Text object to the script
text
in variable. - In the Start function, get the Shadow component on the Text object and place it
enabled
The property is set to true. - Put Shadow component's
effectDistance
The property is set to Vector2(5, -5).
Things to note:
-
effectDistance
The property accepts values of type Vector2, and the shadow offset can be adjusted by setting different x and y values.
Example 4: Adjust the degree of shadow blur
using UnityEngine; using ; public class ChangeShadowBlurExample : MonoBehaviour { public Text text; public Shadow shadow; void Start() { shadow = <Shadow>(); = true; = 2; } }
Operation steps:
- Create a Text object and mount the script to the object.
- Drag the Text object to the script
text
in variable. - In the Start function, get the Shadow component on the Text object and place it
enabled
The property is set to true. - Put Shadow component's
blur
The property is set to 2.
Things to note:
-
blur
The property accepts values of float type, and the degree of blurring of the shadow can be adjusted by setting different values.
Example 5: Use Graphic Alpha as shadow transparency
using UnityEngine; using ; public class UseGraphicAlphaExample : MonoBehaviour { public Text text; public Shadow shadow; void Start() { shadow = <Shadow>(); = true; = true; } }
Operation steps:
- Create a Text object and mount the script to the object.
- Drag the Text object to the script
text
in variable. - In the Start function, get the Shadow component on the Text object and place it
enabled
The property is set to true. - Put Shadow component's
useGraphicAlpha
The property is set to true.
Things to note:
- when
useGraphicAlpha
When the property is set to true, the transparency of the shadow will be consistent with the transparency of the UI element.
References
- Unity official documentation:Shadow
The above is the detailed content of the introduction and usage examples of the Unity UGUI Shadow Shadow Component. For more information about the Unity UGUI Shadow Component, please follow my other related articles!