Пример #1
0
    public void TestCaseColorToHexString()
    {
        Color32 rgb  = new Color32(134, 234, 123, 252);
        string  str  = ColorPresets.ColorToHexString(rgb);
        Color32 rgb2 = ColorPresets.HexStringToColor(str);

        Assert.AreEqual(rgb, rgb2);
    }
Пример #2
0
    public void GenerateColorPreset(string _colorPath, string _codePath)
    {
        if (string.IsNullOrEmpty(_colorPath) || string.IsNullOrEmpty(_codePath))
        {
            return;
        }

        Debug.Log("_colorPath = " + _colorPath);
        Debug.Log("_codePath = " + _codePath);

        // 获取颜色样式
        System.Object[] instanceArray               = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(_colorPath);
        System.Type     typeColorPresetLibrary      = System.Type.GetType("UnityEditor.ColorPresetLibrary,UnityEditor");
        System.Reflection.MethodInfo _CountFunc     = typeColorPresetLibrary.GetMethod("Count");
        System.Reflection.MethodInfo _GetNameFunc   = typeColorPresetLibrary.GetMethod("GetName");
        System.Reflection.MethodInfo _GetPresetFunc = typeColorPresetLibrary.GetMethod("GetPreset");

        string genCode = "";

        System.Object colorLibIntance = instanceArray[0];
        int           count           = (int)_CountFunc.Invoke(colorLibIntance, null);

        for (int i = 0; i < count; ++i)
        {
            string name = (string)_GetNameFunc.Invoke(colorLibIntance, new System.Object[1] {
                i
            });
            Color col = (Color)_GetPresetFunc.Invoke(colorLibIntance, new System.Object[1] {
                i
            });
            string hexStr = ColorPresets.ColorToHexString(col);
            genCode += string.Format(FORMAT_COLORITEM, ColorPresets.COLOR_PREFIX, name, hexStr);
        }

        // 插入到目标代码颜色码区域
        string content    = System.IO.File.ReadAllText(_codePath);
        int    beginIndex = content.IndexOf(BEGIN_GENERATE_CODE);
        int    endIndex   = content.IndexOf(END_GENERATE_CODE);

        string upContent   = content.Substring(0, beginIndex + BEGIN_GENERATE_CODE.Length);
        string backContent = content.Substring(endIndex);

        string newStr = upContent + "\n" + genCode + backContent;

        Debug.Log(newStr);
        File.WriteAllText(_codePath, newStr);
    }