Unity在Hex与Color之间的颜色转换

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

public enum ColorType {
    RRGGBB,
    RRGGBBAA
}

public class ColorUtil {

    private const string hexRegex = "^#?(?:[0-9a-fA-F]{3,4}){1,2}$";


    public static string ColorToHex (Color32 color, ColorType colorType) {

        long num = 0;
        string hexStr = "";

        if (colorType == ColorType.RRGGBB) {
            num = 0xFFFFFF & (ColorRGBAToInt (color) >> 8);
            hexStr = "#" + num.ToString("X6");
        } else {
            num = 0xFFFFFFFF & (ColorRGBAToInt (color));
            hexStr = "#" + num.ToString("X8");
        }

        return hexStr;
    }

    public static bool HexToColor (string hex, out Color32 color, ColorType colorType) {
        // Check if this is a valid hex string (# is optional)
        if (System.Text.RegularExpressions.Regex.IsMatch (hex, hexRegex)) {
            int startIndex = hex.StartsWith ("#") ? 1 : 0;

            color = Color.black;
            if (colorType == ColorType.RRGGBBAA) //#RRGGBBAA
            {
                color = new Color32 (byte.Parse (hex.Substring (startIndex, 2), NumberStyles.AllowHexSpecifier),
                    byte.Parse (hex.Substring (startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
                    byte.Parse (hex.Substring (startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
                    byte.Parse (hex.Substring (startIndex + 6, 2), NumberStyles.AllowHexSpecifier));
            } else if (colorType == ColorType.RRGGBB) //#RRGGBB
            {
                color = new Color32 (byte.Parse (hex.Substring (startIndex, 2), NumberStyles.AllowHexSpecifier),
                    byte.Parse (hex.Substring (startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
                    byte.Parse (hex.Substring (startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
                    255);
            } 
            return true;
        } else {
            color = new Color32 ();
            return false;
        }
    }

    static public int ColorRGBAToInt (Color c) {
        int retVal = 0;
        retVal |= Mathf.RoundToInt (c.r * 255f) << 24;
        retVal |= Mathf.RoundToInt (c.g * 255f) << 16;
        retVal |= Mathf.RoundToInt (c.b * 255f) << 8;
        retVal |= Mathf.RoundToInt (c.a * 255f);

        return retVal;
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中的颜色转换器可以通过编写自定义的着色器来实现。在引用\[1\]中的代码中,可以看到一个自定义的表面着色器,其中包含一个属性变量_DiffuseColor,用于设置漫反射颜色。在surf函数中,将_DiffuseColor.rgb赋值给o.Albedo,将_DiffuseColor.a赋值给o.Alpha,从而实现了颜色转换。 另外,在Unity中还可以使用Gizmos来显示颜色。在引用\[2\]中的代码中,通过在OnDrawGizmosSelected函数中绘制一个黄色的立方体来显示颜色。可以根据需要调整颜色和形状。 此外,Unity还提供了MenuItem标识,可以为编辑器添加新的菜单,并在点击后执行特定的逻辑。在引用\[3\]的示例代码中,使用MenuItem标识创建了一个菜单命令,用于在选中的游戏对象上创建子对象。可以根据需要修改菜单的名称和快捷方式。 综上所述,Unity中的颜色转换器可以通过自定义着色器、使用Gizmos显示和使用MenuItem标识来实现。 #### 引用[.reference_title] - *1* [【Unity3D】表面着色器](https://blog.csdn.net/m0_37602827/article/details/126882367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Unity编辑器扩展](https://blog.csdn.net/iningwei/article/details/125664854)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值