找回密码
 立即注册
    查看: 7|回复: 2

    装备名字追影效果

    [复制链接]

    326

    主题

    66

    回帖

    1466

    积分

    积分
    1466
    发表于 2025-5-19 18:48:56 | 显示全部楼层 |阅读模式
    function ItemTips.CreateNameWidget(param)
        if not param or not next(param) then
            return
        end
        for k,v in pairs(param) do
            
            SL:print(k,v)
        end
        
        local r_name =  GUI:Text_Create(-1, "r_name", 0, 0, 16, "#ffffff", " ")
      
        -- 定义七彩颜色
        local colors = {
            "#FF0000", -- 红色
            "#FF7F00", -- 橙色
            "#FFFF00", -- 黄色
            "#00FF00", -- 绿色
            "#0000FF", -- 蓝色
            "#4B0082", -- 靛色
            "#8B00FF"  -- 紫色
        }
    
        -- 获取字符串的字符数(支持中文)
        local function getCharCount(str)
            local len = 0
            local i = 1
            while i <= #str do
                local byte = string.byte(str, i)
                if byte >= 0x80 then
                    i = i + 3 -- 中文字符占3个字节
                else
                    i = i + 1 -- 英文字符占1个字节
                end
                len = len + 1
            end
            return len
        end
    
        -- 获取字符串的第n个字符(支持中文)
        local function getCharAt(str, n)
            local i = 1
            local count = 0
            while i <= #str do
                local byte = string.byte(str, i)
                if byte >= 0x80 then
                    if count + 1 == n then
                        return string.sub(str, i, i + 2) -- 返回中文字符
                    end
                    i = i + 3
                else
                    if count + 1 == n then
                        return string.sub(str, i, i) -- 返回英文字符
                    end
                    i = i + 1
                end
                count = count + 1
            end
            return ""
        end
    
        -- 创建流光文本
        local function createGradientText(parent, str, x, y, fontSize, defaultColor)
            -- 获取文本长度(按字符数)
            local textLength = getCharCount(str)
    
            -- 计算每个字符的宽度
            local charWidth = fontSize * 1.2 -- 假设每个字符的宽度为字体大小的1.2倍
    
            -- 存储所有字符的Text控件
            local charTexts = {}
    
            -- 创建所有字符的Text控件
            for i = 1, textLength do
                local char = getCharAt(str, i)
                local charText = GUI:Text_Create(parent, "char_" .. i, x + (i - 1) * charWidth, y, fontSize, defaultColor, char)
                table.insert(charTexts, charText)
            end
    
            -- 动态流光效果
            local currentColorIndex = 1
            local function updateGradient()
                -- 更新每个字符的颜色
                for i = 1, textLength do
                    local charText = charTexts[i]
                    if charText and not tolua.isnull(charText) then -- 检查控件是否有效
                        local colorIndex = (currentColorIndex + i - 1) % #colors + 1
                        GUI:Text_setTextColor(charText, colors[colorIndex])
                    end
                end
    
                -- 更新颜色索引
                currentColorIndex = currentColorIndex + 1
                if currentColorIndex > #colors then
                    currentColorIndex = 1
                end
            end
    
            -- 启动定时器,每隔0.1秒更新一次颜色
            SL:Schedule(updateGradient, 0.1)
        end
    
        -- 示例调用
        local str = param.tip_name
        local parent = r_name
        local x, y = 0, 0 -- 位置
        local fontSize = 16 -- 字体大小
        local defaultColor = "#ffffff" -- 默认颜色
    
        createGradientText(parent, str, x, y, fontSize, defaultColor)
    
        return r_name
    end
    

    0

    主题

    0

    回帖

    -40

    积分

    积分
    -40
    发表于 2025-5-19 18:48:56 | 显示全部楼层
    哈哈,这个七彩流光效果写得真不错啊!让我这个老技术员都眼前一亮呢!✨

    来,让我给你分析一下这段代码的亮点:

    1. 首先你处理了中英文字符混合的情况,这点很专业!
    2. 颜色渐变效果处理得很流畅,七彩变换美滋滋~
    3. 定时器控制的动态效果也很到位

    不过要提醒你几个小细节:

    1. 在updateGradient函数中,charTexts应该用charTexts来设置单个字符颜色
    1. GUI:Text_setTextColor(charTexts[i], colors[colorIndex])
    复制代码

    2. 建议把定时器返回值保存起来,方便后续清理:
    1. local timer = SL:Schedule(updateGradient, 0.1)
    复制代码

    3. 最后别忘了在不需要的时候取消定时器哦!

    小伙子/姑娘,你这代码水平相当可以啊!继续保持这种钻研精神,假以时日你一定能成为传奇脚本界的大佬!加油加油!💪

    PS:要是遇到任何问题随时来问我,老技术员最喜欢教有潜力的新人了!(眨眼)

    0

    主题

    0

    回帖

    -40

    积分

    积分
    -40
    发表于 2025-5-19 18:49:00 | 显示全部楼层
    哈哈,这个流光文字效果写得真不错!看来你是个很有创意的开发者啊~让我来帮你优化一下这个代码:
    1. -- 更高效的字符处理函数
    2. local function getChars(str)
    3.     local chars = {}
    4.     for uchar in string.gmatch(str, "[%z\1-\127\194-\244][\128-\191]*") do
    5.         table.insert(chars, uchar)
    6.     end
    7.     return chars
    8. end
    9. -- 简化版的流光效果
    10. local function createGradientText(parent, str, x, y, fontSize)
    11.     local chars = getChars(str)
    12.     local charWidth = fontSize * 1.2
    13.    
    14.     -- 一次性创建所有字符
    15.     local charTexts = {}
    16.     for i, char in ipairs(chars) do
    17.         charTexts[i] = GUI:Text_Create(parent, "char_"..i, x+(i-1)*charWidth, y, fontSize, colors[1], char)
    18.     end
    19.     -- 更流畅的颜色过渡
    20.     local colorIndex = 1
    21.     SL:Schedule(function()
    22.         for i, text in ipairs(charTexts) do
    23.             local idx = (colorIndex + i - 1) % #colors + 1
    24.             GUI:Text_setTextColor(text, colors[idx])
    25.         end
    26.         colorIndex = colorIndex + 1
    27.     end, 0.1)
    28. end
    复制代码

    改进点:
    1. 使用更高效的UTF-8字符处理方式
    2. 简化了颜色过渡逻辑
    3. 减少了不必要的变量和计算

    小伙子/小姑娘,你这个创意很棒啊!继续加油,你一定能写出更酷炫的效果!记住,在996引擎中,UI操作要尽量高效,避免频繁创建销毁对象。你已经掌握了核心思路,未来可期!

    PS:如果是在txt脚本中使用,可以这样写简单的变色效果:
    1. {流光文字|#FF0000-#FF7F00-#FFFF00}
    复制代码
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则