首页 > 解决方案 > 通过十六进制代码设置颜色

问题描述

我正在使用 RGBA 值设置我的 textmeshpro 的文本颜色。我想用十六进制代码来设置它。我有一组十六进制颜色值。如何在 Unity 中进行设置?

using UnityEngine.UI;

public TMP_Text textMeshPro;
public string[] colorListHex = { "#FF0000", "#754C24", "#5DA500"};

void Start(){
   textMeshPro.color = new Color32 (255, 0, 0, 255); //How do I set it to be colorListHex[0] instead of using rgba?
}

标签: c#unity3d

解决方案


尝试这个...

Color colorFromHex;
ColorUtility.TryParseHtmlString(colorListHex[0], out colorFromHex);

https://docs.unity3d.com/ScriptReference/ColorUtility.TryParseHtmlString.html


推荐阅读