Tolua 实现打字机

第一次在CSDN上发表博客 看了很久的CSDN 想写一篇文章,证明自己在不断进步,一个人学习代码真的有点无聊 很容易颓废 或者懈怠

local ToolManager = require(‘Toolmanager.toolstring’)–这是自己封装的一个对工具脚本 主要是对字符串的一些处理因为c#写多了会 习惯了一些操作 所以特意封装了一个脚本 切割方法 在最下面
local this = {}
local Time = Time
this.dataTime = 0 --打字机段落初始时间
this.word = {} --打字机的段落表
this.UnityText = nil – 打字机要输出的文本
this.events = nil --update 事件返回值
this.speed = 1 --打字机速度
this.func = nil --打字机完成以后的回调方法
this.deltaTime = 0 – 每次段落的一帧
this.section = 1 --当前打字的段落
this.recordText = “” --记录已经打印的文本
this.length = 0 --记录文本总长度
this.SumdeltaTime = 0 --记录打字机运行总时间
this.pauseTime = 0–每个段落停顿的时间
this.is_Pause = false --是否需要停顿
this.longWord = 0–长文本增加的打字速度
–打字机

–注册打字机
function AddListenerTypeWriter(text,word,speed,func,pauseTime,longWod)
if text ~=nil then
this.UnityText = text
this.word = ToolManager.Split(word,’\n’)
this.speed = speed
this.func = func
this.deltaTime = 0
this.section = 1
this.recordText = “”
this.length = #word
this.pauseTime = pauseTime
this.longWord = longWod
end
this.events = UpdateBeat:Add(this.Update,this)
end

–移除打字机
function RemoveListenerTypeWriter()
if this.events ~=nil then
UpdateBeat:RemoveListener(this.events)
this.UnityText = nil
this.events = nil
this.word = {}
this.deltaTime = 0
this.speed =1
this.recordText = “”
this.section = 1
this.length = 0
this.SumdeltaTime = 0
this.pauseTime = 0
this.is_Pause = false
this.longWord = 0
end
end

–打字机刷新函数 用于验证字符停顿 段落停顿 回调函数的执行
function this.Update()
–判断是否需要停顿
if this.is_Pause then
this.deltaTime = this.deltaTime + Time.deltaTime
if this.deltaTime >= this.pauseTime then
this.is_Pause = false
end
return
end
if #this.word[this.section] >10 then
this.deltaTime = this.deltaTime + Time.deltaTime * (this.speed+this.longWord)
else
this.deltaTime = this.deltaTime + Time.deltaTime * this.speed
end
if this.deltaTime >= #this.word[this.section] then
this.SumdeltaTime = this.SumdeltaTime + this.deltaTime
this.UnityText.text = this.recordText…string.sub(this.word[this.section],0,this.deltaTime)
this.deltaTime = 0
this.section = this.section + 1
this.recordText = this.UnityText.text…’\n’
–计算是否需要结束打字机
if this.section > #this.word then
if this.func ~= nil then
this.func()
this.func = nil
end
end
this.is_Pause = true
return
end
this.UnityText.text = this.recordText …string.sub(this.word[this.section],0,this.deltaTime)
end

–ToolManager 脚本里面的切割方法 传递文本 传递指定的字符 用于切割
–切割字符串
function ToolString.Split(wold, as)
local endIndex = 0
local startIndex = 0
local split = {}
local index = 1
local str
–自定义切割
if as == ‘t’ then
str = ‘\t’
elseif as == ‘r’ then
str = ‘\r’
elseif as == ‘n’ then
str = ‘\n’
else
str=as
end

while true do
    endIndex = string.find(wold, str, startIndex)
    if endIndex == nil then
        split[index] = string.sub(wold, startIndex, #wold)
        return split
    end
    split[index] = string.sub(wold, startIndex, endIndex - 1)
    startIndex = endIndex + 1
    index = index + 1
end
return split

end

–第一次写博客 也不知道怎么写 直接上了源代码 变量的作用也都备注了 我还在坚持 初心未变

猜你喜欢

转载自blog.csdn.net/weixin_42303855/article/details/88583492