限制输入框输入的字节长度 onValidateInput
function M:SetLimitByteLength(length)
    self.limitLength = length + 1 --不知道为啥 不加1就不正确
    self:RemoveOnValidateInput()

    --self.unity_uitextmeshinput 为Input组件
    self.__onValidateInput = handler(self,self.OnValidateInput)
    self.unity_uitextmeshinput.onValidateInput = self.__onValidateInput
end

--lua去长度为字节长度
function M:OnValidateInput(text,charIndex,c)
     if (#(text .. c) > self.limitLength ) then
        return '\0' --大于的时候返回空字符 此时会输入无效了
     else
        return c
     end
end

function M:RemoveOnValidateInput()
    if self.__onValidateInput ~= nil then
        self.unity_uitextmeshinput.onValidateInput = nil
        self.__onValidateInput = nil
    end
end



首页 我的博客
粤ICP备17103704号