Introduction and use of the Outline (stroke) component of Unity UGUI
1. What is the Outline component?
The Outline component is a special effect component in Unity UGUI, which is used to add stroke effects to UI elements. By setting the color, width, and blur of the stroke, UI elements can be visually more prominent.
2. How the Outline (stroke) component works
The Outline component achieves the effect of stroke by drawing multiple identical UI elements around the UI element and setting different colors and sizes. The width and blur of the stroke can be controlled by adjusting the parameters.
3. Common properties of Outline (stroke) components
- Effect Color: The color of the stroke.
- Effect Distance: The distance of the stroke can be set to a positive or negative value.
- Use Graphic Alpha: Whether to use the transparency of UI elements as the transparency of strokes.
- Blur: The degree of blur of the stroke.
- Outline: The width of the stroke.
4. Common functions of Outline (stroke) components
- ModifyMesh: Modify the grid of UI elements to draw stroke effects.
5. Complete example code
Example 1: Add a red stroke to the button
using UnityEngine; using ; public class Example1 : MonoBehaviour { public Button button; public Outline outline; void Start() { = ; = new Vector2(2, -2); = true; = 0; = 5; } }
Operation steps:
- Create a button and mount the Example1 script to the button.
- Drag the Outline component of the button onto the outline variable of the Example1 script.
- In the Start function, set the properties of the outline, including effectColor, effectDistance, useGraphicAlpha, blur, and outline.
Notes:
- The color, distance, transparency, blur and width of the stroke can be adjusted as needed.
Example 2: Add a blue stroke to the text
using UnityEngine; using ; public class Example2 : MonoBehaviour { public Text text; public Outline outline; void Start() { = ; = new Vector2(1, -1); = true; = 0; = 3; } }
Operation steps:
- Create a text object and mount the Example2 script onto the text object.
- Drag the Outline component of the text object onto the outline variable of the Example2 script.
- In the Start function, set the properties of the outline, including effectColor, effectDistance, useGraphicAlpha, blur, and outline.
Notes:
- The color, distance, transparency, blur and width of the stroke can be adjusted as needed.
Example 3: Add a green stroke to the image
using UnityEngine; using ; public class Example3 : MonoBehaviour { public Image image; public Outline outline; void Start() { = ; = new Vector2(3, -3); = true; = 0; = 7; } }
Operation steps:
- Create an image object and mount the Example3 script onto the image object.
- Drag the Outline component of the image object onto the outline variable of the Example3 script.
- In the Start function, set the properties of the outline, including effectColor, effectDistance, useGraphicAlpha, blur, and outline.
Notes:
- The color, distance, transparency, blur and width of the stroke can be adjusted as needed.
Example 4: Add a yellow stroke to the slider
using UnityEngine; using ; public class Example4 : MonoBehaviour { public Slider slider; public Outline outline; void Start() { = ; = new Vector2(2, -2); = true; = 0; = 5; } }
Operation steps:
- Create a slider object and mount the Example4 script onto the slider object.
- Drag the Outline component of the slider object onto the outline variable of the Example4 script.
- In the Start function, set the properties of the outline, including effectColor, effectDistance, useGraphicAlpha, blur, and outline.
Notes:
- The color, distance, transparency, blur and width of the stroke can be adjusted as needed.
Example 5: Add a purple stroke to the input box
using UnityEngine; using ; public class Example5 : MonoBehaviour { public InputField inputField; public Outline outline; void Start() { = ; = new Vector2(1, -1); = true; = 0; = 3; } }
Operation steps:
- Create an input box object and mount the Example5 script onto the input box object.
- Drag the Outline component of the input box object onto the outline variable of the Example5 script.
- In the Start function, set the properties of the outline, including effectColor, effectDistance, useGraphicAlpha, blur, and outline.
Notes:
- The color, distance, transparency, blur and width of the stroke can be adjusted as needed.
References
- Unity official documentation:Outline
The above is the detailed content of the introduction and usage examples of the Unity UGUI Outline Stroke Component. For more information about the Unity UGUI Outline Stroke Component, please pay attention to my other related articles!