Legacy Documentation: Version 2018.2 (Go to current version)
LanguageEnglish
  • C#

Graphic.color

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public Color color;

Description

Base color of the Graphic.

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.

//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; } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答