ScrollRect滑动优化(四)_Unity—Item图片轮播效果

 直接看效果好了,我只弄了两张图片。支持左右滑动。

ScrollRect滑动优化(四)_Unity—Item图片轮播效果_第1张图片

如果有更好实现的方式,麻烦请告知,网上看无限滑动的例子很多都是动态增减item,我这个是改变每个item的位置来实现无限滑动。

此为Lua代码


local ImageCarousel = class("ImageCarousel")

function ImageCarousel:ctor()

end

function ImageCarousel:Awake()

    self.contents = self.transform:Find("Viewport/Contents")
    self.Btn_Live = self.contents.transform:Find("Btn_ads_Live");
    self.Btn_lottery = self.contents.transform:Find("Btn_ads_lottery");

    self.isLeft = true
    self.isAuto = true
    self.isCheck = false
end

function ImageCarousel:Start(eventData)

    local callback = Delegete:New(typeof(CS.UnityEngine.GameObject), Handler(self, self.BtnClick))
    self.gameObject:SetBtnOnClickListener(callback)

    self.ScrRect = self.gameObject:GetComponent(typeof(CS.UnityEngine.UI.ScrollRect))

    self.Posy = self.Btn_Live.transform.localPosition.y
    self.Posz = self.Btn_Live.transform.localPosition.z

    --添加图形块,图块偏移依据于此
    self.objList = { } 
    self.objList[#self.objList + 1] = self.Btn_Live
    self.objList[#self.objList + 1] = self.Btn_lottery

    self.imageWidth = self.Btn_lottery.transform.localPosition.x - self.Btn_Live.transform.localPosition.x
    self.LeftTargePos = 0
    self.RightTargePos = self.imageWidth

    self.ScrContent = self.ScrRect.content
    self.LeftScrContTargePosX = 0
    self.RightScrContTargePosX = - self.imageWidth
end

function ImageCarousel:OnDisable()

end

function ImageCarousel:OnDestroy()
    
end

function ImageCarousel:Update()
    self:StartExecute()
end

function ImageCarousel:BtnClick(btn)

    if (btn.name == "Btn_test1") then
        Log.info("test1")
    elseif (btn.name == "Btn_test2") then
        Log.info("test2")
    end
end

function ImageCarousel:OnBeginDrag(eventData)
    TimerManager.CancleInvoke("OnPauseMove")
    self.MovePos = CS.UnityEngine.Input.mousePosition.x
    self.isAuto = false

end

function ImageCarousel:OnEndDrag(eventData)

    self.isAuto = true
end

function ImageCarousel:OnDrag(eventData)

    if self.MovePos ~= CS.UnityEngine.Input.mousePosition.x then

        local value = self.MovePos - CS.UnityEngine.Input.mousePosition.x
        --Log.Info("move: " .. self.MovePos .. ", mouse: " .. CS.UnityEngine.Input.mousePosition.x .. ", value" .. value)
        if value >= 0 then
            self.isLeft = true
        else
            self.isLeft = false
        end

        self.MovePos = CS.UnityEngine.Input.mousePosition.x
    end
end

function ImageCarousel:OnPointerDown(eventData)

end

function ImageCarousel:OnPointerUp(eventData)

end

function ImageCarousel:OnPointerClick(eventData)
    
end

function ImageCarousel:CheckSlipper(gob, isLeft)

    self.isCheck = true
    if isLeft then
        --图块跳转
        gob.transform.localPosition =  Vector3(self.LeftTargePos + #self.objList * self.imageWidth, self.Posy, self.Posz) 

        --偏移计算
        self.LeftTargePos = self.LeftTargePos + self.imageWidth
        self.LeftScrContTargePosX = self.LeftScrContTargePosX - self.imageWidth

        --偏移计算
        self.RightTargePos = self.RightTargePos + self.imageWidth
        self.RightScrContTargePosX = self.RightScrContTargePosX - self.imageWidth

        --位置校准
        self.ScrContent.transform.localPosition = Vector3(self.LeftScrContTargePosX, self.Posy, self.Posz)
    else
        
        gob.transform.localPosition =  Vector3(self.RightTargePos - #self.objList * self.imageWidth, self.Posy, self.Posz)

        self.RightTargePos = self.RightTargePos - self.imageWidth
        self.RightScrContTargePosX = self.RightScrContTargePosX + self.imageWidth

        self.LeftTargePos = self.LeftTargePos - self.imageWidth
        self.LeftScrContTargePosX = self.LeftScrContTargePosX + self.imageWidth

        self.ScrContent.transform.localPosition = Vector3(self.RightScrContTargePosX, self.Posy, self.Posz)
    end

    if self.isAuto then
        self.isAuto = false
        --计时器,等待5s后运行
        TimerManager.Invoke("OnPauseMove", 5,function()
            self.isAuto = true
        end )
    end
    self.isCheck = false
end

function ImageCarousel:StartExecute()

    if self.isCheck then
        return
    end
    for i = 1, #self.objList do
        if self.isLeft then
            if self.isAuto then
                self.ScrContent.transform.localPosition = Vector3(self.ScrContent.transform.localPosition.x - 10, self.Posy, self.Posz)
            end

--            Log.Info(self.ScrContent.transform.localPosition.x,"X")
--            Log.Info(self.LeftScrContTargePosX - self.imageWidth,"left")

--            Log.Info(self.LeftTargePos,"TargetPos")
            if self.ScrContent.transform.localPosition.x <= self.LeftScrContTargePosX - self.imageWidth and self.objList[i].transform.localPosition.x == self.LeftTargePos then   
                self:CheckSlipper(self.objList[i], self.isLeft)
                break
            end
        else
            if self.isAuto then
                self.ScrContent.transform.localPosition = Vector3(self.ScrContent.transform.localPosition.x + 10, self.Posy, self.Posz)
            end

--            Log.Info(self.ScrContent.transform.localPosition.x,"X")
--            Log.Info(self.RightScrContTargePosX + self.imageWidth,"Right")

--            Log.Info(self.RightTargePos,"TargetPos")
            if self.ScrContent.transform.localPosition.x >= self.RightScrContTargePosX + self.imageWidth and self.objList[i].transform.localPosition.x == self.RightTargePos then
                self:CheckSlipper(self.objList[i], self.isLeft)
                break
            end
        end
    end

end

return ImageCarousel

 

在结尾附上Demo的链接: https://download.csdn.net/download/le_sam/10887047

你可能感兴趣的:(Unity)