返回介绍

ColorUtility.TryParseHtmlString 尝试解析Html字符串

发布于 2019-12-18 15:37:34 字数 5090 浏览 1006 评论 0 收藏 0

JavaScript => public static function TryParseHtmlString(htmlString: string, out color: Color): bool;
C# => public static bool TryParseHtmlString(string htmlString, out Color color);

Parameters 参数

htmlStringCase insensitive html string to be converted into a color.
要转换为颜色的html字符串,不区分大小写。
colorThe converted color.
已转换的颜色。

Returns 返回值

bool True if the string was successfully converted else false.

如果字符串成功转换返回true,否则返回false。

Description 描述

Attempts to convert a html color string.

尝试转换一个html颜色字符串。

Strings that begin with '#' will be parsed as hexadecimal in the following way:

#开头的字符串作为十六进制解析,像下面这样:

#RGB (becomes RRGGBB)
#RRGGBB
#RGBA (becomes RRGGBBAA)
#RRGGBBAA

When not specified alpha will default to FF.

如果不指定alpha通道,那么默认为FF。

Strings that do not begin with '#' will be parsed as literal colors, with the following supported:

不是#开头的字符串解析为文本颜色,支持的颜色如下:

red, cyan, blue, darkblue, lightblue, purple, yellow, lime, fuchsia, white, silver, grey, black, orange, brown, maroon, green, olive, navy, teal, aqua, magenta..

The following example creates a custom PropertyDrawer that allows the user to input html colors. This property drawer can be shown in the inspector when a color property has the attribute ColorHtmlProperty.

下面的例子是,创建一个自定义的PropertyDrawer,允许用户输入html颜色。当一个颜色属性具有ColorHtmlProperty属性时,那么该颜色属性将显示在检视面板。

这是自定义的颜色属性

JavaScript:

#pragma strict
// This is not an editor script.
public class ColorHtmlPropertyAttribute extends PropertyAttribute {
}

C#:

// This is not an editor script.
using UnityEngine;
 
public class ColorHtmlPropertyAttribute : PropertyAttribute
{
}

JavaScript:

#pragma strict
// This is an editor script and should be placed in an 'Editor' directory.
@CustomPropertyDrawer(ColorHtmlPropertyAttribute)
public class ColorHtmlPropertyDrawer extends PropertyDrawer {
	public override function OnGUI(position: Rect, property: SerializedProperty, label: GUIContent) {
		var htmlField: Rect = new Rect(position.x, position.y, position.width - 100, position.height);
		var colorField: Rect = new Rect(position.x + htmlField.width, position.y, position.width - htmlField.width, position.height);
		var htmlValue: String = EditorGUI.TextField(htmlField, label, "#" + ColorUtility.ToHtmlColorStringRGBA(property.colorValue));
		var newCol: Color;
		if (ColorUtility.TryParseHtmlString(htmlValue, newCol))
			property.colorValue = newCol;
		property.colorValue = EditorGUI.ColorField(colorField, property.colorValue);
	}
}

C#:

// This is an editor script and should be placed in an 'Editor' directory.
using UnityEngine;
using UnityEditor;
 
[CustomPropertyDrawer(typeof(ColorHtmlPropertyAttribute))]
public class ColorHtmlPropertyDrawer : PropertyDrawer
{
	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
	{        
		Rect htmlField = new Rect(position.x, position.y, position.width - 100, position.height);
		Rect colorField = new Rect(position.x + htmlField.width, position.y, position.width - htmlField.width, position.height);
 
		string htmlValue = EditorGUI.TextField(htmlField, label, "#" + ColorUtility.ToHtmlColorStringRGBA(property.colorValue));
 
		Color newCol;
		if (ColorUtility.TryParseHtmlString(htmlValue, out newCol))
			property.colorValue = newCol;
 
		property.colorValue = EditorGUI.ColorField(colorField, property.colorValue);
	}
}

JavaScript:

#pragma strict
// This shows how we would use the PropertyDrawer.
public class Example extends MonoBehaviour {
	@ColorHtmlProperty
	public var htmlColor: Color = Color.green;
	public var standardColor: Color = Color.green;
}

C#:

// This shows how we would use the PropertyDrawer.
using UnityEngine;
 
public class Example : MonoBehaviour
{
	[ColorHtmlProperty]
	public Color htmlColor = Color.green;
 
	public Color standardColor = Color.green;
}

ColorUtility

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文