Class Graphic
Base class for all UI components that should be derived from when creating new Graphic types.
Implements
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[DisallowMultipleComponent]
[RequireComponent(typeof(RectTransform))]
[ExecuteAlways]
public abstract class Graphic : UIBehaviour, ICanvasElement
Constructors
Graphic()
Protected default constructor. Use UnityEngine.GameObject.AddComponent<T>() to add a Graphic to a GameObject.
Declaration
protected Graphic()
Fields
m_CachedMesh
Cached mesh used by the legacy mesh generation path.
Declaration
[Obsolete("m_CachedMesh is obsolete now that legacy mesh generation is no longer supported.", true)]
[NonSerialized]
protected Mesh m_CachedMesh
Field Value
| Type | Description |
|---|---|
| Mesh |
m_CachedUvs
Cached UV array used by the legacy mesh generation path.
Declaration
[Obsolete("m_CachedUvs is obsolete now that legacy mesh generation is no longer supported.", true)]
[NonSerialized]
protected Vector2[] m_CachedUvs
Field Value
| Type | Description |
|---|---|
| Vector2[] |
m_Material
Serialized backing field for the custom material assigned to this graphic.
Declaration
[FormerlySerializedAs("m_Mat")]
[SerializeField]
protected Material m_Material
Field Value
| Type | Description |
|---|---|
| Material |
m_OnDirtyLayoutCallback
Callback invoked when the layout of this graphic is marked dirty.
Declaration
[NonSerialized]
protected UnityAction m_OnDirtyLayoutCallback
Field Value
| Type | Description |
|---|---|
| UnityAction |
m_OnDirtyMaterialCallback
Callback invoked when the material of this graphic is marked dirty.
Declaration
[NonSerialized]
protected UnityAction m_OnDirtyMaterialCallback
Field Value
| Type | Description |
|---|---|
| UnityAction |
m_OnDirtyVertsCallback
Callback invoked when the vertex data of this graphic is marked dirty.
Declaration
[NonSerialized]
protected UnityAction m_OnDirtyVertsCallback
Field Value
| Type | Description |
|---|---|
| UnityAction |
m_SkipLayoutUpdate
Internal flag to avoid layout updates during bulk operations.
Declaration
[NonSerialized]
protected bool m_SkipLayoutUpdate
Field Value
| Type | Description |
|---|---|
| bool |
m_SkipMaterialUpdate
Internal flag to avoid material updates during bulk operations.
Declaration
[NonSerialized]
protected bool m_SkipMaterialUpdate
Field Value
| Type | Description |
|---|---|
| bool |
s_DefaultUI
Default material used for UI graphics when no material is assigned.
Declaration
protected static Material s_DefaultUI
Field Value
| Type | Description |
|---|---|
| Material |
s_Mesh
Shared working mesh used during mesh generation to avoid per-frame allocations.
Declaration
protected static Mesh s_Mesh
Field Value
| Type | Description |
|---|---|
| Mesh |
s_WhiteTexture
Cached reference to a plain white texture used as the default UI texture.
Declaration
protected static Texture2D s_WhiteTexture
Field Value
| Type | Description |
|---|---|
| Texture2D |
Properties
canvas
A reference to the Canvas this Graphic is rendering to.
Declaration
public Canvas canvas { get; }
Property Value
| Type | Description |
|---|---|
| Canvas |
Remarks
In the situation where the Graphic is used in a hierarchy with multiple Canvases, the Canvas closest to the root will be used.
canvasRenderer
A reference to the CanvasRenderer populated by this Graphic.
Declaration
public CanvasRenderer canvasRenderer { get; }
Property Value
| Type | Description |
|---|---|
| CanvasRenderer |
color
Base color of the Graphic.
Declaration
public virtual Color color { get; set; }
Property Value
| Type | Description |
|---|---|
| Color |
Remarks
The builtin UI Components use this as their vertex color. Use this to fetch or change the Color of visual UI elements, such as an Image.
Examples
//Place this script on a GameObject with a Graphic component attached e.g. a visual UI element (Image).
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
Graphic m_Graphic;
Color m_MyColor;
void Start()
{
//Fetch the Graphic from the GameObject
m_Graphic = GetComponent<Graphic>();
//Create a new Color that starts as red
m_MyColor = Color.red;
//Change the Graphic Color to the new Color
m_Graphic.color = m_MyColor;
}
// Update is called once per frame
void Update()
{
//When the mouse button is clicked, change the Graphic Color
if (Input.GetKey(KeyCode.Mouse0))
{
//Change the Color over time between blue and red while the mouse button is pressed
m_MyColor = Color.Lerp(Color.red, Color.blue, Mathf.PingPong(Time.time, 1));
}
//Change the Graphic Color to the new Color
m_Graphic.color = m_MyColor;
}
}
defaultGraphicMaterial
Default material used to draw UI elements if no explicit material was specified.
Declaration
public static Material defaultGraphicMaterial { get; }
Property Value
| Type | Description |
|---|---|
| Material |
defaultMaterial
Returns the default material for the graphic.
Declaration
public virtual Material defaultMaterial { get; }
Property Value
| Type | Description |
|---|---|
| Material |
depth
Absolute depth of the graphic, used by rendering and events -- lowest to highest.
Declaration
public int depth { get; }
Property Value
| Type | Description |
|---|---|
| int |
Examples
The depth is relative to the first root canvas.
Canvas Graphic - 1 Graphic - 2 Nested Canvas Graphic - 3 Graphic - 4 Graphic - 5
This value is used to determine draw and event ordering.
mainTexture
The graphic's texture. (Read Only).
Declaration
public virtual Texture mainTexture { get; }
Property Value
| Type | Description |
|---|---|
| Texture |
Remarks
This is the Texture that gets passed to the CanvasRenderer, Material and then Shader _MainTex.
When implementing your own Graphic you can override this to control which texture goes through the UI Rendering pipeline.
Bear in mind that Unity tries to batch UI elements together to improve performance, so its ideal to work with atlas to reduce the number of draw calls.
material
The Material set by the user
Declaration
public virtual Material material { get; set; }
Property Value
| Type | Description |
|---|---|
| Material |
materialForRendering
The material that will be sent for Rendering (Read only).
Declaration
public virtual Material materialForRendering { get; }
Property Value
| Type | Description |
|---|---|
| Material |
Remarks
This is the material that actually gets sent to the CanvasRenderer. By default it's the same as [[Graphic.material]]. When extending Graphic you can override this to send a different material to the CanvasRenderer than the one set by Graphic.material. This is useful if you want to modify the user set material in a non destructive manner.
raycastPadding
Padding to be applied to the masking X = Left Y = Bottom Z = Right W = Top
Declaration
public Vector4 raycastPadding { get; set; }
Property Value
| Type | Description |
|---|---|
| Vector4 |
raycastTarget
Should this graphic be considered a target for raycasting?
Declaration
public virtual bool raycastTarget { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
rectTransform
The RectTransform component used by the Graphic. Cached for speed.
Declaration
public RectTransform rectTransform { get; }
Property Value
| Type | Description |
|---|---|
| RectTransform |
useLegacyMeshGeneration
Obsolete. The legacy mesh generation is no longer supported.
Declaration
[Obsolete("useLegacyMeshGeneration is deprecated now that the legacy mesh generation is no longer supported.", true)]
protected bool useLegacyMeshGeneration { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
workerMesh
Shared static mesh reused across all graphics during vertex generation.
Declaration
protected static Mesh workerMesh { get; }
Property Value
| Type | Description |
|---|---|
| Mesh |
Methods
CrossFadeAlpha(float, float, bool)
Tweens the alpha of the CanvasRenderer color associated with this Graphic.
Declaration
public virtual void CrossFadeAlpha(float alpha, float duration, bool ignoreTimeScale)
Parameters
| Type | Name | Description |
|---|---|---|
| float | alpha | Target alpha. |
| float | duration | Duration of the tween in seconds. |
| bool | ignoreTimeScale | Should ignore [[Time.scale]]? |
CrossFadeColor(Color, float, bool, bool)
Tweens the CanvasRenderer color associated with this Graphic.
Declaration
public virtual void CrossFadeColor(Color targetColor, float duration, bool ignoreTimeScale, bool useAlpha)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | targetColor | Target color. |
| float | duration | Tween duration. |
| bool | ignoreTimeScale | Should ignore Time.scale? |
| bool | useAlpha | Should also Tween the alpha channel? |
CrossFadeColor(Color, float, bool, bool, bool)
Tweens the CanvasRenderer color associated with this Graphic.
Declaration
public virtual void CrossFadeColor(Color targetColor, float duration, bool ignoreTimeScale, bool useAlpha, bool useRGB)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | targetColor | Target color. |
| float | duration | Tween duration. |
| bool | ignoreTimeScale | Should ignore Time.scale? |
| bool | useAlpha | Should also Tween the alpha channel? |
| bool | useRGB | Should the color or the alpha be used to tween |
GetPixelAdjustedRect()
Returns a pixel perfect Rect closest to the Graphic RectTransform.
Declaration
public Rect GetPixelAdjustedRect()
Returns
| Type | Description |
|---|---|
| Rect | A Pixel perfect Rect. |
Remarks
Note: This is only accurate if the Graphic root Canvas is in Screen Space.
GraphicUpdateComplete()
Called by the canvas update system after graphic updates are complete. No-op for base Graphic.
Declaration
public virtual void GraphicUpdateComplete()
LayoutComplete()
Called by the canvas update system after layout is complete. No-op for base Graphic.
Declaration
public virtual void LayoutComplete()
OnBeforeTransformParentChanged()
Called before the parent transform changes. Unregisters from the current canvas hierarchy.
Declaration
protected override void OnBeforeTransformParentChanged()
Overrides
OnCanvasHierarchyChanged()
Called when the Canvas hierarchy changes. Updates the cached parent canvas reference.
Declaration
protected override void OnCanvasHierarchyChanged()
Overrides
OnCullingChanged()
This method must be called when CanvasRenderer.cull is modified.
Declaration
public virtual void OnCullingChanged()
Remarks
This can be used to perform operations that were previously skipped because the Graphic was culled.
OnDestroy()
Called when the component is destroyed. Releases mesh resources and unregisters from the registries.
Declaration
protected override void OnDestroy()
Overrides
OnDidApplyAnimationProperties()
Called when animation properties are applied. Marks the graphic dirty for a full rebuild.
Declaration
protected override void OnDidApplyAnimationProperties()
Overrides
OnDisable()
Clear references.
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Mark the Graphic and the canvas as having been changed.
Declaration
protected override void OnEnable()
Overrides
OnPopulateMesh(VertexHelper)
Callback function when a UI element needs to generate vertices. Fills the vertex buffer data.
Declaration
protected virtual void OnPopulateMesh(VertexHelper vh)
Parameters
| Type | Name | Description |
|---|---|---|
| VertexHelper | vh | VertexHelper utility. |
Remarks
Used by Text, UI.Image, and RawImage for example to generate vertices specific to their use case.
OnRebuildRequested()
Editor-only callback that is issued by Unity if a rebuild of the Graphic is required. Currently sent when an asset is reimported.
Declaration
public virtual void OnRebuildRequested()
OnRectTransformDimensionsChange()
Called when the RectTransform dimensions change. Sets layout and vertices dirty.
Declaration
protected override void OnRectTransformDimensionsChange()
Overrides
OnTransformParentChanged()
Called when the parent changes. Re-registers with the new canvas hierarchy and marks dirty.
Declaration
protected override void OnTransformParentChanged()
Overrides
OnValidate()
Declaration
protected override void OnValidate()
Overrides
PixelAdjustPoint(Vector2)
Adjusts the given pixel to be pixel perfect.
Declaration
public Vector2 PixelAdjustPoint(Vector2 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point | Local space point. |
Returns
| Type | Description |
|---|---|
| Vector2 | Pixel perfect adjusted point. |
Remarks
Note: This is only accurate if the Graphic root Canvas is in Screen Space.
Raycast(Vector2, Camera)
When a GraphicRaycaster is raycasting into the scene it does two things. First it filters the elements using their RectTransform rect. Then it uses this Raycast function to determine the elements hit by the raycast.
Declaration
public virtual bool Raycast(Vector2 sp, Camera eventCamera)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | sp | Screen point being tested |
| Camera | eventCamera | Camera that is being used for the testing. |
Returns
| Type | Description |
|---|---|
| bool | True if the provided point is a valid location for GraphicRaycaster raycasts. |
Raycast(Vector2, Camera, bool)
When a GraphicRaycaster raycasts into the scene, it first filters the elements based on their RectTransform rect, then uses this Raycast function to determine which elements are hit.
Declaration
protected bool Raycast(Vector2 sp, Camera eventCamera, bool ignoreMasks)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | sp | Screen point being tested. |
| Camera | eventCamera | Camera used for testing. |
| bool | ignoreMasks | If true, masks are ignored and do not prevent raycasts. |
Returns
| Type | Description |
|---|---|
| bool | True if the provided point is a valid location for GraphicRaycaster raycasts. |
Rebuild(CanvasUpdate)
Rebuilds the graphic geometry and its material on the PreRender cycle.
Declaration
public virtual void Rebuild(CanvasUpdate update)
Parameters
| Type | Name | Description |
|---|---|---|
| CanvasUpdate | update | The current step of the rendering CanvasUpdate cycle. |
Remarks
See CanvasUpdateRegistry for more details on the canvas update cycle.
RegisterDirtyLayoutCallback(UnityAction)
Add a listener to receive notification when the graphics layout is dirtied.
Declaration
public void RegisterDirtyLayoutCallback(UnityAction action)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAction | action | The method to call when invoked. |
RegisterDirtyMaterialCallback(UnityAction)
Add a listener to receive notification when the graphics material is dirtied.
Declaration
public void RegisterDirtyMaterialCallback(UnityAction action)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAction | action | The method to call when invoked. |
RegisterDirtyVerticesCallback(UnityAction)
Add a listener to receive notification when the graphics vertices are dirtied.
Declaration
public void RegisterDirtyVerticesCallback(UnityAction action)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAction | action | The method to call when invoked. |
Reset()
Declaration
protected override void Reset()
Overrides
SetAllDirty()
Set all properties of the Graphic dirty and needing rebuilt. Dirties Layout, Vertices, and Materials.
Declaration
public virtual void SetAllDirty()
SetLayoutDirty()
Mark the layout as dirty and needing rebuilt.
Declaration
public virtual void SetLayoutDirty()
Remarks
Send a OnDirtyLayoutCallback notification if any elements are registered. See RegisterDirtyLayoutCallback
SetMaterialDirty()
Mark the material as dirty and needing rebuilt.
Declaration
public virtual void SetMaterialDirty()
Remarks
Send a OnDirtyMaterialCallback notification if any elements are registered. See RegisterDirtyMaterialCallback
SetNativeSize()
Make the Graphic have the native size of its content.
Declaration
public virtual void SetNativeSize()
SetRaycastDirty()
Marks the raycast cache dirty so it will be recalculated on the next query.
Declaration
public void SetRaycastDirty()
SetVerticesDirty()
Mark the vertices as dirty and needing rebuilt.
Declaration
public virtual void SetVerticesDirty()
Remarks
Send a OnDirtyVertsCallback notification if any elements are registered. See RegisterDirtyVerticesCallback
UnregisterDirtyLayoutCallback(UnityAction)
Remove a listener from receiving notifications when the graphics layout are dirtied
Declaration
public void UnregisterDirtyLayoutCallback(UnityAction action)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAction | action | The method to call when invoked. |
UnregisterDirtyMaterialCallback(UnityAction)
Remove a listener from receiving notifications when the graphics material are dirtied
Declaration
public void UnregisterDirtyMaterialCallback(UnityAction action)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAction | action | The method to call when invoked. |
UnregisterDirtyVerticesCallback(UnityAction)
Remove a listener from receiving notifications when the graphics vertices are dirtied
Declaration
public void UnregisterDirtyVerticesCallback(UnityAction action)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAction | action | The method to call when invoked. |
UpdateGeometry()
Call to update the geometry of the Graphic onto the CanvasRenderer.
Declaration
protected virtual void UpdateGeometry()
UpdateMaterial()
Call to update the Material of the graphic onto the CanvasRenderer.
Declaration
protected virtual void UpdateMaterial()