PGRData/Script/matrix/xui/xuimain/xuichildview/XUiPanelAd.lua

301 lines
No EOL
9 KiB
Lua

local XUiPanelAd = XClass(XLuaBehaviour, "XUiPanelAd")
local MISTAKE_DISTANCE = 5
local DefaultPicScrollInterval = 5
local PITCH_ON = CS.XGame.ClientConfig:GetString("UiMainPitchOn")
local PITCH_OFF = CS.XGame.ClientConfig:GetString("UiMainPitchOff")
local JumpType = {
Web = 1,
Game = 2,
Sign = 3,
}
function XUiPanelAd:Ctor(rootUi, ui)
self.RootUi = rootUi
self.GameObject = ui.gameObject
self.Transform = ui.transform
self:InitAutoScript()
self.ChildPosXs = {} --广告坐标
self.AdPrefab = {} --广告图片
self.PageNum = {} --小白点表
self.CurIndex = 1 --当前为第几图片
self:AddPointerClickListener()
self:UpdateAdList()
XEventManager.AddEventListener(XEventId.EVENT_PLAYER_LEVEL_CHANGE, self.UpdateAdList, self)
end
-- auto
-- Automatic generation of code, forbid to edit
function XUiPanelAd:InitAutoScript()
self:AutoInitUi()
self.SpecialSoundMap = {}
end
function XUiPanelAd:AutoInitUi()
self.Panelpicture = self.Transform:Find("Map/Panelpicture")
self.PanelSwitchover = self.Transform:Find("Map/PanelSwitchover")
self.PanelSw = self.Transform:Find("Map/PanelSwitchover/PanelSw")
self.CenterScroll = self.Transform:GetComponent("XUiCenterScroll")
self:InitCenterScroll()
end
function XUiPanelAd:Update()
if #self.AdList <= 1 then
return
end
if not self.ChildPosXs or not self.AdList then
return
end
if not self.ChildPosXs[self.CurIndex] or not self.AdList[self.CurIndex] then
return
end
local nowPosX = self.Panelpicture.anchoredPosition.x
local targetPosX = -self.ChildPosXs[self.CurIndex]
if self.CurIndex == 1 and math.abs(nowPosX - targetPosX) <= MISTAKE_DISTANCE then
self:ForceMoveToIndex(#self.AdList - 2)
elseif self.CurIndex == #self.AdList and math.abs(nowPosX - targetPosX) <= MISTAKE_DISTANCE then
self:ForceMoveToIndex(1)
end
end
function XUiPanelAd:InitCenterScroll()
self.CenterScroll:RegisterEndDragCallBack(function(index)
local nowIndex = index + 1
if self.CurIndex == nowIndex then
return
end
local interval = DefaultPicScrollInterval
if self.AdList and self.AdList[self.CurIndex] and self.AdList[self.CurIndex].Interval then
interval = self.AdList[self.CurIndex].Interval
end
self:ClearTimer()
self.Timer = XScheduleManager.ScheduleOnce(function()
self.CenterScroll:NextPage()
end, interval * 1000)
self.CurIndex = nowIndex
self:UpdatePage()
end)
end
function XUiPanelAd:ClearTimer()
if self.Timer then
XScheduleManager.UnSchedule(self.Timer)
self.Timer = nil
end
end
function XUiPanelAd:AddPointerClickListener()
self.UiPanelAd = self.GameObject:AddComponent(typeof(CS.XUiWidget))
self.UiPanelAd:AddPointerClickListener(function()
self:OnPointerClick()
end)
self.UiPanelAd:AddEndDragListener(function()
self:OnEndDrag()
end)
self.UiPanelAd:AddDragListener(function()
self:OnDrag()
end)
end
function XUiPanelAd:OnDrag()
self.IsDraging = true
end
--停止拖动
function XUiPanelAd:OnEndDrag()
self.IsDraging = false
end
function XUiPanelAd:ForceMoveToIndex(index)
self.CenterScroll:SetIndex(index)
end
function XUiPanelAd:OnPointerClick()
if self.IsDraging then
return
end
if not self.AdList or not self.AdList[self.CurIndex] then
return
end
local data = self.AdList[self.CurIndex]
local jumpAddr = data.JumpAddr
if not jumpAddr then
return
end
local dict = {}
dict["ui_first_button"] = XGlobalVar.BtnBuriedSpotTypeLevelOne.BtnUiMainPanelAd
dict["role_level"] = XPlayer.GetLevel()
CS.XRecord.Record(dict, "200004", "UiOpen")
if tonumber(data.JumpType) == JumpType.Web then
if jumpAddr and #jumpAddr > 0 then
if string.find(jumpAddr, "eden") ~= nil then -- 伊甸文化跳转
local uid = XUserManager.UserId
local serverId = CS.XHeroBdcAgent.ServerId
if uid and uid ~= "" then
if serverId and serverId ~= "" then
CS.UnityEngine.Application.OpenURL(jumpAddr.."?uid="..uid.."&serverId="..serverId)
else
CS.UnityEngine.Application.OpenURL(jumpAddr.."?uid="..uid)
end
return
end
elseif string.find(jumpAddr, "whiteday") ~= nil then --情人节网页活动
local uid = XLoginManager.GetUserId()
if uid and uid ~= "" then
CS.UnityEngine.Application.OpenURL(jumpAddr.."?uid="..uid)
return
end
elseif string.find(jumpAddr, "natsumatsuri") ~= nil then -- 夏日祭活动
local uid = XLoginManager.GetUserId()
if uid and uid ~= "" then
CS.UnityEngine.Application.OpenURL(jumpAddr.."?code_id="..uid)
return
end
elseif string.find(jumpAddr, "seeed") ~= nil or string.find(jumpAddr, "rooot") ~= nil then -- rooot活动的
XDataCenter.ActivityManager.OpenRoootUrl(jumpAddr)
return
end
CS.UnityEngine.Application.OpenURL(jumpAddr)
end
elseif tonumber(data.JumpType) == JumpType.Game then
XFunctionManager.SkipInterface(tonumber(jumpAddr))
elseif tonumber(data.JumpType) == JumpType.Sign then
if jumpAddr and #jumpAddr > 0 then
local signAddr = XDataCenter.MarketingActivityManager.GetSignUrl(jumpAddr)
CS.UnityEngine.Application.OpenURL(signAddr)
end
end
end
--小球变色
function XUiPanelAd:UpdatePage()
local index = self.CurIndex
if index == #self.AdList then
index = 2
elseif self.CurIndex == 1 then
index = #self.AdList - 1
end
for k, rawImage in pairs(self.PageNum) do
rawImage:SetRawImage(PITCH_OFF)
if k == index then
rawImage:SetRawImage(PITCH_ON)
end
end
end
function XUiPanelAd:UpdateAdList()
self.AdList = {}
self.ChildPosXs = {}
local dataList = XDataCenter.NoticeManager.GetScrollPicList() or {}
if #dataList > 1 then
table.insert(self.AdList, dataList[#dataList])
for _, v in ipairs(dataList) do
table.insert(self.AdList, v)
end
table.insert(self.AdList, self.AdList[2])
else
self.AdList = dataList
end
self:UpdateAdvertising()
self:UpdatePage()
end
function XUiPanelAd:Addadvertising(index)
local advertising = self.Panelpicture:GetChild(0).gameObject
advertising:SetActiveEx(false)
local smallSp = self.PanelSw.gameObject
smallSp:SetActiveEx(false)
local adPrefab = CS.UnityEngine.Object.Instantiate(advertising, self.Panelpicture)
adPrefab.gameObject:SetActiveEx(true)
self.AdPrefab[index] = adPrefab
if index > 1 and index < #self.AdList then
local pageNum = CS.UnityEngine.Object.Instantiate(smallSp, self.PanelSwitchover)
pageNum.gameObject:SetActiveEx(true)
self.PageNum[index] = pageNum:GetComponent("RawImage")
end
self.ChildPosXs[index] = (index - 1) * self.Panelpicture:GetComponent("GridLayoutGroup").cellSize.x
self:LoadWebTexture(index)
end
function XUiPanelAd:LoadWebTexture(index, isBackUp)
local picAddr
if isBackUp then
picAddr = self.AdList[index].PicAddrSlave
else
picAddr = self.AdList[index].PicAddr
end
XDataCenter.NoticeManager.LoadPicFromLocal(picAddr, function(texture)
if XTool.UObjIsNil(self.AdPrefab[index]) then
return
end
self.AdPrefab[index]:GetComponent("RawImage").texture = texture
end)
end
--动态获得广告个数
function XUiPanelAd:UpdateAdvertising()
self:ClearAdvertsing()
if #self.AdList <= 0 then
local advertising = self.Panelpicture:GetChild(0).gameObject
advertising:SetActiveEx(true)
local smallSp = self.PanelSw.gameObject
smallSp:SetActiveEx(false)
return
end
for i = 1, #self.AdList do
self:Addadvertising(i)
end
self.CenterScroll:UpdatePages()
if #self.ChildPosXs > 1 then
self:ForceMoveToIndex(1)
end
self.Panelpicture:SetSizeWithCurrentAnchors(CS.UnityEngine.RectTransform.Axis.Horizontal, #self.ChildPosXs * 467)
end
-- 清空广告节点
function XUiPanelAd:ClearAdvertsing()
for _, v in pairs(self.AdPrefab) do
CS.UnityEngine.Object.DestroyImmediate(v)
end
self.AdPrefab = {}
for _, v in pairs(self.PageNum) do
CS.UnityEngine.Object.Destroy(v.gameObject)
end
self.PageNum = {}
end
function XUiPanelAd:OnDestroy()
XEventManager.RemoveEventListener(XEventId.EVENT_PLAYER_LEVEL_CHANGE, self.UpdateAdList, self)
XEventManager.RemoveEventListener(XEventId.EVENT_MAINUI_ENABLE, self.UpdateAdList, self)
end
return XUiPanelAd