版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/iceSony/article/details/83412503
试衣间系统大家不陌生,类似qq飞车的商城,界面效果如下
由于界面左侧存在多个可切换页签,因此代码不适合放在一个.lua脚本中构造,采用主干+多支线的方式
主干代码:
--幻化间
--左侧点击原位置/颜色 现位置/颜色
local color_click = Color(1,1,1,1)
local color_unclick = Color(1, 1, 1, 0.8)
local pos_click = Vector3(22,0,0)
local pos_unclick = Vector3.zero
--存储左侧room信息,包含icon bg text 用于选中颜色位置变化
local roomtran_table = {}
local roomcolor_table = {}
--组件 默认显示个数 每一次切换页签修改的数据
local mGridScroll
local defineItemCount = 2
local showData
--战力脚本
local battle_num
--右侧显示
local choose_title
--sceneEff中玩家位置 其他位置
local player_tran
local other_tran ={} --包含坐骑 宠物 女神节点
local showPoint={} --展示节点
--右侧小圆点
local dot_show
local dot_unshow
--下方的horsebtn
local uphorse
local downhorse
--类型对应扩展表
local FitExtScriptNameEnum=
{
--坐骑幻化
[1] = "HorseFitExt",
--宠物幻化
[2] = "PetFitExt",
--女神幻化
[3] = "GoddessFitExt",
--神兵幻化
[4] = "ShenBingFitExt",
--时装上衣幻化
[5] = "FashionClothFitExt",
--时装武器幻化
[6] = "FashionWeaponFitExt",
--时装副武器幻化
[7] = "FashionAssistantWeaponFitExt",
}
-----模型 模型位置
local objPlayer
local sceneEff
local tranCamera
--扩展脚本路径
local strExPath="LuaLogic/UIWindow/FittingRoom/func_extension/%s.lua"
local function GetExtensionLuaPath(scriptName)
if not scriptName then
return ""
end
local strPath=string.format(strExPath,scriptName)
return strPath
end
--扩展脚本
local extScript
--recid 用于记录每个页签选择的item
local recordSelItemTab={}
local curSelRoomType
--物品描述和名称
local itemname_txt
local itemdes_txt
function Awake()
CS.ActivityButton.Get(l_returnbtn).action=OnClickExit
CS.ActivityButton.Get(l_jsbtn).action=OnClickHuanHua
CS.ActivityButton.Get(l_horsestate).action=OnClickOnhorse
battle_num = l_battle:GetComponent("ImageNum")
choose_title = l_choose_title:GetComponent("Text")
mGridScroll = l_rightcontent.transform.parent:GetComponent("GridScrollUpgrade")
dot_show = CS.SpriteManager.Instance:GetSprite("huanhua_icon_show")
dot_unshow = CS.SpriteManager.Instance:GetSprite("huanhua_item_unshow")
uphorse = CS.SpriteManager.Instance:GetSprite("huanhua_uphorse")
downhorse = CS.SpriteManager.Instance:GetSprite("huanhua_downhorse")
itemname_txt = l_itemname:GetComponent("Text")
itemdes_txt = l_itemdes:GetComponent("Text")
CS.ActivityButton.Get(l_content_parent.transform.parent.gameObject).endDragAction=MoveSqContent
end
function OnShow()
--初始化房间列表
InitRoomList()
InitFitRoom()
SetAllTitleState(false)
if l_content_bg.transform.childCount > 0 then
local tranRoomItem=l_content_bg.transform:GetChild(0)
if Ex_IsNotNil(tranRoomItem) then
local tranTouchBg=tranRoomItem:GetChild(0)
if Ex_IsNotNil(tranTouchBg) then
OnClickRoomItem(tranTouchBg.gameObject)
end
end
end
ShowZhanLi(false)
end
function OnHide()
if extScript and extScript.HandleEvent then
extScript.HandleEvent(false)
end
extScript = nil
if curSelRoomType ~= nil then
roomtran_table[curSelRoomType].name.localPosition = pos_unclick
roomtran_table[curSelRoomType].icon.localPosition = pos_unclick
roomcolor_table[curSelRoomType].name.color = color_unclick
roomcolor_table[curSelRoomType].icon.color = color_unclick
roomcolor_table[curSelRoomType].bg.color = color_unclick
end
curSelRoomType=nil
recordSelItemTab={}
l_rightcontent.transform:EX_ClearChild(0)
l_itemList.transform:EX_HideChild(0)
l_jsbtn:SetActive(false)
l_jstext:SetActive(false)
if Ex_IsNotNil(sceneEff) then
GameObject.DestroyImmediate(sceneEff)
end
SetAllTitleState(true)
local triggerMouse=CS.ActivityMouse.Get(l_touchImg)
if triggerMouse then
if triggerMouse.funcOffset then
triggerMouse.funcOffset=nil
end
if triggerMouse.funcScrollWheel then
triggerMouse.funcScrollWheel=nil
end
if triggerMouse.funcTouch then
triggerMouse.funcTouch=nil
end
end
end
function ShowGhostTitle()
if Ex_IsNotNil(ghostBase) then
ghostBase:SetHeadShowState(true)
end
end
function HideGhostTitle(ghostBase)
if Ex_IsNotNil(ghostBase) then
ghostBase:SetHeadShowState(false)
end
end
function SetAllTitleState(state)
local ghostDic=CS.GhostMgr.Instance:getGhostDic()
if not ghostDic then
return
end
local eid = PlayerMediator.GetPlayerEid() or 0
local selfGhost = CS.GhostMgr.Instance:GetGhostByID(eid)
if state then
if selfGhost then
selfGhost:SetHeadShowState(true)
end
ghostDic:ForeachGhostDic(ShowGhostTitle)
else
if selfGhost then
selfGhost:SetHeadShowState(false)
end
ghostDic:ForeachGhostDic(HideGhostTitle)
end
end
function InitFitRoom()
local sceneEffPrefab=ResourceManager.Instance:LoadBundle("ResFile/UI/Effect/FittingRoomEffScene", true,true)
if not Ex_IsNotNil(sceneEffPrefab) then
LogError("加载幻化特效场景预制体失败检查路径")
return
end
if not Ex_IsNotNil(sceneEff) then
sceneEff = GameObject.Instantiate(sceneEffPrefab)
sceneEff.transform.localScale = Vector3.one
sceneEff.transform.localPosition = Vector3.zero
sceneEff.name="FittingRoomScene"
--各个模型的挂载点
player_tran = sceneEff.transform:Find("model_point/playerModel")
other_tran.horse_tran = sceneEff.transform:Find("model_point/horseModel")
other_tran.pet_tran = sceneEff.transform:Find("model_point/petModel")
other_tran.nvshen_tran = sceneEff.transform:Find("model_point/nvshenModel")
showPoint.player_tran = sceneEff.transform:Find("show_point/playerModel")
showPoint.horse_tran = sceneEff.transform:Find("show_point/horseModel")
showPoint.pet_tran = sceneEff.transform:Find("show_point/petModel")
showPoint.nvshen_tran = sceneEff.transform:Find("show_point/nvshenModel")
end
if not Ex_IsNotNil(sceneEff) then
LogError("?没生成")
return
end
if not sceneEff.activeSelf then
sceneEff:SetActive(true)
end
--sceneEff.transform:Find("Directional_light").gameObject:SetActive(true)
sceneEff:GetComponent("ExhibitionCameraAnimation"):ResetCameraAnimation()
ResourceManager.Instance:StepLoadBundle("ResFile/Model/PlayerContainer", OnPlayerContainerLoaded, nil,true,true)
end
function OnPlayerContainerLoaded(obj, param)
if not Ex_IsNotNil(sceneEff) then
LogError("场景已被删除")
return
end
if not Ex_IsNotNil(obj) then
LogError("玩家预制体加载失败")
return
end
if not Ex_IsNotNil(objPlayer) then
if not Ex_IsNotNil(player_tran) then
return
end
objPlayer = GameObject.Instantiate(obj,player_tran)
objPlayer:GetComponent("BoxCollider").enabled = false
objPlayer.transform.localPosition = Vector3.zero
objPlayer.transform.localRotation = CS.UnityEngine.Quaternion.Euler(Vector3(0, 179, 0))
--objPlayer.transform:SetSiblingIndex(0)
end
if not Ex_IsNotNil(objPlayer) then
LogError("没人?")
return
end
tranCamera=sceneEff.transform:Find("Camera_Effect_2")
if not Ex_IsNotNil(tranCamera) then
LogError("请挂个照相机")
else
--[[ local rideState=HorseMediator.IsOnRide()
if rideState then
tranCamera:GetComponent("FittingCameraControl"):SetTarget(showPoint.horse_tran:Find("showPoint"),false)
else--]]
tranCamera:GetComponent("FittingCameraControl"):SetTarget(showPoint.player_tran:Find("showPoint"),false)
--end
CS.ActivityMouse.Get(l_touchImg).funcOffset=HandleCameraOffset
CS.ActivityMouse.Get(l_touchImg).funcScrollWheel=HandleCameraFieldOfView
end
local job = PlayerManager.PlayerInfo.mclass
local modelId = PlayerManager.getEntityPAttr(EntityProp.attr_player_cloth)
local weapon1 = PlayerManager.getEntityPAttr(EntityProp.attr_player_weapon)
local weapon2 = PlayerManager.getEntityPAttr(EntityProp.attr_player_weapon2)
local wing = PlayerManager.getEntityPAttr(EntityProp.attr_player_wings)
local compOutLook=objPlayer:GetComponent("OutLookChange")
if Ex_IsNotNil(compOutLook) then
compOutLook:initOutLookInLogin(job,modelId, weapon2, weapon1, OnModelLoaded)
--流光
local suitVal=PlayerManager.getEntityPAttr(EntityProp.attr_suit_effect_id)
if suitVal>0 then
local suitData= gdSuitEquipLiuGuang[suitVal]
if suitData then
local sceneid = suitData.sceneid or 0
compOutLook:ChangeMaterial(objPlayer,sceneid)
end
else
compOutLook:UseMaterialForShow()
end
if wing>0 then
compOutLook:changeBody(FashionMediator.GetBodyPart(4),wing,OnWingLoaded)
end
else
LogError("outlook组件没了",objPlayer)
end
end
function OnWingLoaded(goWing)
if Ex_IsNotNil(goWing) then
goWing:EX_SetLayer("UIEffect")
end
end
function HandleCameraOffset(offsetX,offsetY)
if Ex_IsNotNil(tranCamera) then
tranCamera:GetComponent("FittingCameraControl"):RotateCamera(offsetX,offsetY)
end
end
function HandleCameraFieldOfView(x)
if Ex_IsNotNil(tranCamera) then
tranCamera:GetComponent("FittingCameraControl"):ScrollMouse(x)
end
end
function OnModelLoaded(go)
if Ex_IsNotNil(objPlayer) then
objPlayer:EX_SetLayer("UIEffect",true,true)
--是否在马上
local rideState=HorseMediator.IsOnRide()
local horseModel
local curHorseId=HorseMediator.GetCurHorseId()
if curHorseId>0 then
if Ex_IsNotNil(other_tran.horse_tran) then
local horse_tran = other_tran.horse_tran
local scale,pos,rotate = HorseMediator.GetHorseTranInfo(curHorseId)
horse_tran.gameObject:SetActive(true)
local configData={}
configData.update=true
configData.scale=1
configData.pos=Vector3.zero
configData.layer="UIEffect"
configData.rotate=Vector3(0,180,0)
configData.playAnim = false
horseModel = UIShowModel.CreateModel(horse_tran, HorseMediator.GetHorseModelID(curHorseId),configData)
end
end
if rideState then
if Ex_IsNotNil(horseModel) then
local ridepos = horseModel:GetComponent("HorseRidePos").ridepos
objPlayer.transform:SetParent(ridepos)
objPlayer.transform.localPosition = Vector3.zero
objPlayer:GetComponent("OutLookChange").curBody:GetComponent("Animator"):Play("ride")
else
LogError("马加载失败")
end
else
if Ex_IsNotNil(other_tran.horse_tran) then
other_tran.horse_tran.gameObject:SetActive(false)
end
objPlayer:GetComponent("OutLookChange").curBody:GetComponent("Animator"):Play("idle2")
end
SetHorseChangeBtnState(curHorseId>0,rideState)
end
local petId=PetManager.PetInfo[EntityProp.attr_pet_model] or 0
if other_tran and Ex_IsNotNil(other_tran.pet_tran) then
if petId>0 then
PetMediator.LoadPetModelInScene(petId,other_tran.pet_tran,nil)
else
if other_tran.pet_tran.childCount>0 then
local tranPet=other_tran.pet_tran:GetChild(0)
if Ex_IsNotNil(tranPet) then
GameObject.DestroyImmediate(tranPet.gameObject)
end
end
end
else
LogError("宠物节点没了")
end
end
--从excel表中获取列表,左侧列表进行初始化
function InitRoomList()
--已初始化则返回
if l_content_bg.transform.childCount>0 then
return
else
for roomType,roomData in ipairs(gdFitRoom) do
--生成icon bg name 并进行修改
local roomBg = GameObject.Instantiate(l_roomBg,l_content_bg.transform)
local roomIcon = GameObject.Instantiate(l_roomIcon,l_content_icon.transform)
local roomName = GameObject.Instantiate(l_roomName,l_content_text.transform)
if Ex_IsNotNil(roomBg) and Ex_IsNotNil(roomIcon) and Ex_IsNotNil(roomName) then
--将所有按钮置灰
local name_tran = roomName.transform:Find("text")
local icon_tran = roomIcon.transform:Find("image")
local bg_tran = roomBg.transform:Find("image")
local nametext = name_tran:GetComponent("Text")
local iconimg = icon_tran:GetComponent("Image")
local bgimg = bg_tran:GetComponent("Image")
nametext.color = color_unclick
iconimg.color = color_unclick
bgimg.color = color_unclick
local temp = {}
temp.name = name_tran
temp.icon = icon_tran
local temp2 = {}
temp2.name = nametext
temp2.icon = iconimg
temp2.bg = bgimg
table.insert(roomtran_table,roomType,temp)
table.insert(roomcolor_table,roomType,temp2)
nametext.text=roomData.roomName
bg_tran.name = roomType
iconimg.sprite=CS.SpriteManager.Instance:GetSprite(roomData.roomIcon)
iconimg:SetNativeSize()
CS.ActivityButton.Get(bg_tran.gameObject).action=OnClickRoomItem
else
LogError("房间对象获取失败")
end
end
end
end
--列表点击时触发
function OnClickRoomItem(go)
local roomType=tonumber(go.name)
if not roomType then
LogError("fittype对象 类型名未赋值",go)
return
end
--点击同个按钮
if curSelRoomType and roomType==curSelRoomType then
return
end
--之前选中图标左移
ChangeRoomColor(false)
--清除原本右侧选中
ChangeItemSelState(false)
curSelRoomType = roomType
--展示当前页签右侧选中
ChangeItemSelState(true)
--当前选中图标右移
ChangeRoomColor(true)
l_content_parent:GetComponent("RectTransform").anchoredPosition = Vector2.zero
HandleFitRoomData(roomType)
end
--配置右侧数据
function HandleFitRoomData(roomType)
if extScript and extScript.HandleEvent then
extScript.HandleEvent(false)
end
--扩展加载
local scriptName=FitExtScriptNameEnum[roomType]
if not scriptName then
LogError("对应扩展文件名未定义 roomType is",roomType)
scriptName=FitExtScriptNameEnum[1]
end
local strPath=GetExtensionLuaPath(scriptName)
local func=DoExtension(scriptName,strPath)
if not func then
LogError("扩展加载失败",roomType)
return
end
extScript=func()
if EX_HasNotData(extScript) then
LogError("扩展表空")
return
end
--事件注册
if extScript.HandleEvent then
extScript.HandleEvent(true)
end
--初始化展示物品
InitShowItem()
end
--加载展示物品
function InitShowItem()
if not extScript then
LogError("缺少扩展脚本")
return
end
if not extScript.GetShowData then
LogError("缺少获取展示数据的方法")
return
end
showData=extScript.GetShowData()
if not showData then
LogError("展示数据为空")
return
end
--切换页签如果有选中则修改战力
if not curSelRoomType then
LogError("选择的房间号空")
return
end
--当前标签有选中
if recordSelItemTab[curSelRoomType] then
local id = recordSelItemTab[curSelRoomType].recId
if id then
if not EX_HasNotData(showData[id]) then
local battle = showData[id].battle or 0
if battle_num then
battle_num.Value = battle
end
local isLock = showData[id].isLock or false
if l_jsbtn.activeSelf ~= isLock then
l_jsbtn:SetActive(isLock)
end
if l_jstext.activeSelf ~= isLock then
l_jstext:SetActive(isLock)
end
ShowNameAndDesc(showData[id])
ShowSource(showData[id])
end
else
LogError("有记录没id")
end
else--当前标签无选中
if l_jsbtn.activeSelf then
l_jsbtn:SetActive(false)
end
if l_jstext.activeSelf then
l_jstext:SetActive(false)
end
if l_itemname.activeSelf then
l_itemname:SetActive(false)
end
if l_itemdes.activeSelf then
l_itemdes:SetActive(false)
end
ShowZhanLi(false)
l_itemList.transform:EX_HideChild(0)
end
--修改右侧文字
if Ex_IsNotNil(choose_title) then
choose_title.text = LanguageMnager.Get(91007+curSelRoomType)
end
if not EX_HasNotData(showData) then
local mcount=math.ceil(luatablemaxcount(showData)/9)
if mcount>0 then
mGridScroll:SetGridNum(mcount)
if mcount<defineItemCount then
defineItemCount=mcount
else
defineItemCount=2
end
end
--创建右侧九宫格
local count = l_rightcontent.transform.childCount + 1
for i=count,defineItemCount do
local grid = GameObject.Instantiate(l_fittingbag,l_rightcontent.transform).transform
for j=0,8 do
local s_grid = grid:GetChild(j)
CS.ActivityButton.Get(s_grid.gameObject).action=OnClickShowItem
CS.ActivityButton.Get(s_grid.gameObject).endDragAction=MoveSqContent
end
end
--创建右侧小圆点
local dotcount = l_dotcontent.transform.childCount
for i=dotcount,mcount-1 do
GameObject.Instantiate(l_dotimg,l_dotcontent.transform)
end
for i=0,dotcount-1 do
l_dotcontent.transform:GetChild(i).gameObject:SetActive(true)
end
l_dotcontent.transform:EX_HideChild(mcount)
l_rightcontent.transform:EX_ClearChild(mcount)
mGridScroll:InitList()
--小圆点复位
ShowDot()
end
end
--获取途径加载
function InitShowSourceWnd(id)
if (not showData) or (not showData[id]) then
LogError("展示数据为空",id)
if l_itemList.activeSelf then
l_itemList:SetActive(false)
end
return
end
l_itemList:SetActive(true)
local itemData=showData[id]
if itemData then
ShowSource(itemData)
end
end
function ShowNameAndDesc(m_itemData)
if EX_HasNotData(m_itemData) then
return
end
local itemname = m_itemData.itemname
local itemdesc = m_itemData.itemdesc
if itemname and itemdesc then
itemname_txt.text = itemname
itemdes_txt.text = itemdesc
l_itemsource:SetActive(true)
l_itemname:SetActive(true)
l_itemdes:SetActive(true)
else
LogError("名称或描述丢失")
l_itemname:SetActive(false)
l_itemdes:SetActive(false)
end
end
function ShowSource(m_itemData)
if (not m_itemData.source) or (not gdItemSource) then
LogError("没有来源信息")
Ex_HideChild(l_itemList.transform, 0)
return
end
local sourceCount=#(m_itemData.source)
for i = 1,sourceCount do
local index = tonumber(m_itemData.source[i])--一定要转number
local sourceData = gdItemSource[index]
local tranItem = nil
if i <= l_itemList.transform.childCount then
tranItem = l_itemList.transform:GetChild(i - 1)
else
tranItem = (GameObject.Instantiate(l_item)).transform
tranItem:SetParent(l_itemList.transform)
tranItem.localPosition = Vector3.zero
tranItem.localScale = Vector3.one
end
if not tranItem.gameObject.activeSelf then
tranItem.gameObject:SetActive(true)
end
local panelData=sourceData.panelData
if panelData then
local uidata=gdUIData[panelData.panelId]
--跳转界面的情况
if uidata then
local childdata = uidata.UIContainer[panelData.childId] or {panelname=uidata.PanelName,UIIcon=uidata.UIIcon}
if childdata then
tranItem:Find("Name"):GetComponent("Text").text = childdata.panelname
if sourceData.childtitle then
tranItem:Find("ChlidName"):GetComponent("Text").text = sourceData.childtitle
else
tranItem:Find("ChlidName"):GetComponent("Text").text = ""
end
local spIcon = CS.UIDataMgr.LoadSprite(childdata.UIIcon)
if spIcon then
tranItem:Find("IconImage"):GetComponent("Image").sprite = spIcon
local rectIcon = tranItem:Find("IconImage"):GetComponent("RectTransform")
rectIcon.sizeDelta = spIcon.rect.size
end
if tranItem then
tranItem.gameObject:GetComponent("Button").onClick:RemoveAllListeners()
tranItem.gameObject:GetComponent("Button").onClick:AddListener(function() OnClickSource(panelData.panelId,panelData.childId, sourceData.openlv,nil,panelData.dailyEid,panelData.eventId) end)
end
end
end
end
end
Ex_HideChild(l_itemList.transform, sourceCount)
end
--获取途径点击跳转方法
function OnClickSource(openPanel, childpanelIndex, lv,iid,activityId,jumpId)
--判断下功能是否开启
if openPanel==UINameEnum.GuildBossPopPanel then
if GuildMediator.GetCurrGuildId()==0 then
CS.NotifyMgr.Instance:AddNotify(NotifyType.Function,LanguageMnager.Get(84239) ,0)
return
end
end
if activityId~=nil and activityId>0 then
if FuncOpenManager.BaseUIWindowFrameState(UINameEnum.EctypePanelUI,childpanelIndex,true)==0 then
CS.NotifyMgr.Instance:AddNotify(NotifyType.Function,LanguageMnager.Get(84244),0)
return
end
else
if FuncOpenManager.BaseUIWindowFrameState(openPanel,childpanelIndex,true)==0 then
CS.NotifyMgr.Instance:AddNotify(NotifyType.Function,LanguageMnager.Get(84244),0)
return
end
end
CS.SoundManager.Instance:PlaySound(90018)
local m_nCurPlayerLv = PlayerManager.PlayerInfo.level
if m_nCurPlayerLv and lv and m_nCurPlayerLv < lv then
local strFormat = lv .. "级开放!"
CS.NotifyMgr.Instance:AddNotify(NotifyType.Function, strFormat, 0)
return
end
--直接跳副本去。。
if activityId~=nil and activityId>0 then
CS.UIMgr.Instance:PushUIEx(UINameEnum.EctypePanelUI, UIWindowShowMode.HideOther, activityId)
return
end
if openPanel == UINameEnum.TreasureHunterWindowUI or openPanel == "TreasureHunterWindowUI" then
--狩猎跳转
CS.UIMgr.Instance:PushUIEx(openPanel, UIWindowShowMode.DoNothing, childpanelIndex,iid)
elseif jumpId~=nil then
CS.UIMgr.Instance:PushUIEx(openPanel, UIWindowShowMode.DoNothing, childpanelIndex - 1,iid,jumpId)
else
CS.UIMgr.Instance:PushUIEx(openPanel, UIWindowShowMode.DoNothing, childpanelIndex - 1,iid)
end
end
--一键清空
function ResetFit()
if EX_HasNotData(recordSelItemTab) then
return
end
if Ex_IsNotNil(objPlayer) then
objPlayer.transform:SetParent(player_tran)
objPlayer.transform.localPosition=Vector3.zero
objPlayer.transform.localRotation = CS.UnityEngine.Quaternion.Euler(Vector3(0, 179, 0))
--GameObject.Destroy(objPlayer)
end
if other_tran then
if Ex_IsNotNil(other_tran.horse_tran) and other_tran.horse_tran.childCount>0 then
local tranHorse=other_tran.horse_tran:GetChild(0)
if Ex_IsNotNil(tranHorse) then
GameObject.DestroyImmediate(tranHorse.gameObject)
end
end
if Ex_IsNotNil(other_tran.nvshen_tran) then
other_tran.nvshen_tran.gameObject:SetActive(false)
end
if Ex_IsNotNil(player_tran) then
player_tran.gameObject:SetActive(true)
end
end
InitFitRoom()
if curSelRoomType and recordSelItemTab[curSelRoomType] then
local idx=recordSelItemTab[curSelRoomType].recId
local tranItem=GetItemById(idx)
if Ex_IsNotNil(tranItem) then
ItemPrefab.ConfigTip(tranItem,false)
end
end
recordSelItemTab={}
end
--点击物品传递gameobject对象
function OnClickShowItem(go)
local id=tonumber(go.name)
if not id then
LogError("展示物品对象名错误",go)
return
end
local recId
if not curSelRoomType then
LogError("当前选择房间为空")
return
end
if recordSelItemTab[curSelRoomType] then
recId = recordSelItemTab[curSelRoomType].recId
if recId and id == recId then
return
end
--去掉当前页之前选中的
--去掉选中框
local tranItem = GetItemById(recId)
if not Ex_IsNotNil(tranItem) then
LogError("goitem is nil!!!!")
return
end
ItemPrefab.ConfigTip(tranItem,false)
else
recordSelItemTab[curSelRoomType]={}
end
ShowZhanLi(true)
recordSelItemTab[curSelRoomType].recId=id
ItemPrefab.ConfigTip(go.transform,true)
if not showData[id] then
LogError("展示数据空",id)
return
end
local btnState = not showData[id].isLock
if btnState~=l_jsbtn.activeSelf then
l_jsbtn:SetActive(btnState)
l_jstext:SetActive(btnState)
end
if extScript.LoadModel then
if Ex_IsNotNil(player_tran) then
--每一次加载模型player放到对应节点下,防止被删除
if Ex_IsNotNil(other_tran.horse_tran) and Ex_IsNotNil(other_tran.pet_tran) and Ex_IsNotNil(other_tran.nvshen_tran) and Ex_IsNotNil(objPlayer) then
objPlayer.transform:SetParent(player_tran)
extScript.LoadModel(id,player_tran,objPlayer,other_tran,tranCamera,showPoint)
else
LogError("模型挂载点有丢失?")
end
end
else
LogError("扩展模型加载没得")
end
--如果有马 切换为ride,没有就idle2
SetRideState()
--修改战力显示
if not EX_HasNotData(showData[id]) then
local battle = showData[id].battle or 0
if battle_num then
battle_num.Value = battle
end
end
--修改名称与物品描述
if not EX_HasNotData(showData[id]) then
ShowNameAndDesc(showData[id])
end
--获取途径
InitShowSourceWnd(id)
end
function OnClick(eventName)
if eventName=="ResetFit" then
ResetFit()
elseif eventName=="ContentMove" then
local pos = l_content_bg.transform.position
l_content_icon.transform.position = pos
l_content_text.transform.position = pos
elseif eventName=="RightContentMove" then
ShowDot()
end
end
function OnClickHuanHua()
if extScript and extScript.ExcuteFit then
if curSelRoomType then
if recordSelItemTab[curSelRoomType] then
local idx = recordSelItemTab[curSelRoomType].recId or 1
extScript.ExcuteFit(idx,sceneEff,objPlayer)
end
end
else
LogError("缺少幻化方法")
end
end
function OnClickExit()
CS.UIMgr.Instance:PopUIEx(UINameEnum.FittingRoomPanel)
end
--true设为点击
function ChangeRoomColor(flag)
local index = curSelRoomType
if not index then
return
end
if EX_HasNotData(roomtran_table[index]) then
return
end
if flag == true then
roomtran_table[index].name.localPosition = pos_click
roomtran_table[index].icon.localPosition = pos_click
roomcolor_table[index].name.color = color_click
roomcolor_table[index].icon.color = color_click
roomcolor_table[index].bg.color = color_click
else
roomtran_table[index].name.localPosition = pos_unclick
roomtran_table[index].icon.localPosition = pos_unclick
roomcolor_table[index].name.color = color_unclick
roomcolor_table[index].icon.color = color_unclick
roomcolor_table[index].bg.color = color_unclick
end
end
InitGrids=function(gridTransform,wrapIndex,gridRealIndex,gridListIndex)
if EX_HasNotData(showData) then
return
end
local startidx = gridRealIndex*9+1
local endidx = (gridRealIndex +1)*9
if endidx > #showData then
endidx = #showData
end
print(startidx,endidx)
local index
for i=startidx,endidx do
index = i%9
if index == 0 then
index = 9
end
local itemData = showData[i]
local objShowItem = gridTransform:GetChild(index-1)
objShowItem.gameObject.name = i
if recordSelItemTab[curSelRoomType] then
local recId = recordSelItemTab[curSelRoomType].recId or -1
if i ~= recId then
ItemPrefab.ConfigTip(objShowItem,false)
else
ItemPrefab.ConfigTip(objShowItem,true)
local isLock = itemData.isLock or false
l_jsbtn:SetActive(not isLock)
l_jstext:SetActive(not isLock)
if itemData then
ShowSource(itemData)
end
end
end
ItemPrefab.ConfigIcon(objShowItem,itemData.icon)
ItemPrefab.ConfigColor(objShowItem,itemData.color)
ItemPrefab.ConfigLock(objShowItem,itemData.isLock)
objShowItem.gameObject:SetActive(true)
end
gridTransform.transform:EX_HideChild(index)
end
function GetItemById(id)
local tranItem
local index = math.floor((id-1) / 9)
local idx = id%9 - 1
if idx == -1 then
idx = 8
end
if index <0 then
LogError("传入id错误")
end
if index < l_rightcontent.transform.childCount then
tranItem = l_rightcontent.transform:GetChild(index):GetChild(idx)
if not Ex_IsNotNil(tranItem) then
LogError("对象获取失败")
return
end
end
return tranItem
end
--改变当前选中状态
function ChangeItemSelState(flag)
if not recordSelItemTab[curSelRoomType] then
return
end
local idx = recordSelItemTab[curSelRoomType].recId
if not idx then
LogError("暂无历史选中")
return
end
local tranItem = GetItemById(idx)
if Ex_IsNotNil(tranItem) then
ItemPrefab.ConfigTip(tranItem,flag)
end
end
--如果场景里面有马,则ride,反之不管
function SetRideState()
if not Ex_IsNotNil(other_tran.horse_tran) then
LogError("horse_tran为空")
return
end
if not Ex_IsNotNil(objPlayer) then
LogError("objplayer为空")
return
end
local playerAnim=objPlayer:GetComponent("OutLookChange").curBody:GetComponent("Animator")
if not Ex_IsNotNil(playerAnim) then
LogError("没有动画")
return
end
local animHelp=CS.AnimatorHelper(playerAnim)
--如果有马
if other_tran.horse_tran.gameObject.activeSelf and other_tran.horse_tran.childCount>0 then
if not animHelp:IsPlayingClip("ride") then
local backpos = CS.SyntaxParser.FindBone(other_tran.horse_tran, "RidePos")
objPlayer.transform:SetParent(backpos)
objPlayer.transform.localPosition = Vector3.zero
playerAnim:Play("ride")
end
if other_tran.nvshen_tran.gameObject.activeSelf then
other_tran.nvshen_tran.gameObject:SetActive(false)
end
SetHorseChangeBtnState(true,true)
else
if not animHelp:IsPlayingClip("idle2") then
playerAnim:Play("idle2")
objPlayer.transform.localPosition = Vector3.zero
end
local curHorseId=HorseMediator.GetCurHorseId()
SetHorseChangeBtnState(curHorseId>0 or other_tran.horse_tran.childCount>0,false)
end
end
--根据当前显示的右侧九宫格,dot变亮与灰
function ShowDot()
local posx = l_content_parent:GetComponent("RectTransform").anchoredPosition.x*-1 + 50
if posx<0 then
posx = 0
end
posx = math.floor(posx / 300);
local count = l_dotcontent.transform.childCount-1
if posx> count*300 then
posx = count
end
for i=0,count do
l_dotcontent.transform:GetChild(i):GetComponent("Image").sprite = dot_unshow
if i==posx then
l_dotcontent.transform:GetChild(i):GetComponent("Image").sprite = dot_show
end
end
end
function OnClickOnhorse(go)
if not Ex_IsNotNil(other_tran.horse_tran) then
LogError("没有马节点")
return
end
local horseState=other_tran.horse_tran.gameObject.activeSelf
other_tran.horse_tran.gameObject:SetActive(not horseState)
if horseState then
if Ex_IsNotNil(objPlayer) and Ex_IsNotNil(player_tran) then
objPlayer.transform:SetParent(player_tran)
end
if Ex_IsNotNil(player_tran) and (not player_tran.gameObject.activeSelf) then
player_tran.gameObject:SetActive(true)
end
end
SetRideState()
end
--设置上下马按钮
--showState 按钮显示状态
--horseState 骑乘状态
function SetHorseChangeBtnState(showState,horseState)
if l_horsestate.activeSelf ~= showState then
l_horsestate:SetActive(showState)
end
if l_horsestatetext.activeSelf ~= showState then
l_horsestatetext:SetActive(showState)
end
if l_horsestatebg.activeSelf ~= showState then
l_horsestatebg:SetActive(showState)
end
--如果按钮是关闭状态则无需设置图片和文字了
if not showState then
return
end
--骑着就显示下马 没骑就显示上马
if not horseState then
l_horsestatetext:GetComponent("Text").text=LanguageMnager.Get(91015)
l_horsestate:GetComponent("Image").sprite=uphorse
else
l_horsestatetext:GetComponent("Text").text=LanguageMnager.Get(91016)
l_horsestate:GetComponent("Image").sprite=downhorse
end
end
function ShowZhanLi(flag)
if l_zhanli.activeSelf ~= flag then
l_zhanlibg_l:SetActive(flag)
l_zhanlibg_r:SetActive(flag)
l_zhanli:SetActive(flag)
l_battle:SetActive(flag)
end
end
function MoveSqContent()
local rectSqContent=l_content_parent:GetComponent("RectTransform")
local pos=rectSqContent.anchoredPosition
if pos.x>0 or pos.x<-3500 then
return
end
local curShowCardIdx= pos.x/-300
if ((curShowCardIdx*10)%10)>5 then
local endPos=-math.ceil(curShowCardIdx)*300
rectSqContent:EX_DOAnchorPosX(endPos,0.2)
else
local endPos=-math.floor(curShowCardIdx)*300
rectSqContent:EX_DOAnchorPosX(endPos,0.2)
end
end
主干实际也有1100多行,分解下来并不多,主要分为不同数据的获取;不同模型的加载;诸如女神和马的处理,摄像头的移动。右侧界面是在左侧页签切换时候获取并缓存在支线中,以其中的宠物为例
local this={}
local Vector3 = CS.UnityEngine.Vector3
local Quaternion = CS.UnityEngine.Quaternion
--id :第几个物品 p_tran:场景位置 model:模型 o_tran包含pet_tran,horse_tran,nvshen_tran
function this.LoadModel(id,p_tran,model,o_tran,tranCamera,showPoint)
id = tonumber(id)
PetMediator.LoadPetModelInScene(id,o_tran.pet_tran,nil)
--[[ if Ex_IsNotNil(tranCamera) and Ex_IsNotNil(showPoint.pet_tran) then
tranCamera:GetComponent("FittingCameraControl"):SetTarget(showPoint.pet_tran:Find("showPoint"))
end--]]
end
--传回主干数据
local maindata = {}
local tabledata = gdPetModel
--得到按钮显示状态
function this.GetBtnState(id)
return false
end
local function ParseData()
if not EX_HasNotData(maindata) then
return
end
if EX_HasNotData(tabledata) then
LogError("宠物表检查")
return
end
for i=1,#tabledata do
local temp = {}
temp.icon = tabledata[i].headicon
temp.color = tabledata[i].quality
temp.source = tabledata[i].source
temp.isLock = not PetMediator.IsPetOpen(i)
temp.battle = PetMediator.GetPetGradeBattleNum(i)
temp.itemname = "[ "..tabledata[i].name.." ]"
temp.itemdesc = LanguageMnager.Get(tabledata[i].desc)
maindata[#maindata+1] = temp
end
end
--展示数据
--item需要的数据
--icon名
--color品质色
--star星级
--isLock是否已经解锁 或者已经获取到了
--source获取途径
--item一定要按顺序排列
--另外请加上showCount这个字段 用来统计你有几个道具
--返回需要加载的物品数量
function this.GetShowData()
ParseData()
return maindata
end
--事件
function this.HandleEvent(state)
if state then
LuaEvent.RegEvent(gdGame.EventNameEnum.EVENT_MSG_FUNC_DATA_OPERATOR_RESPONSE_LUA,this.HandleMsgFuncData)
else
LuaEvent.RmvEvent(gdGame.EventNameEnum.EVENT_MSG_FUNC_DATA_OPERATOR_RESPONSE_LUA,this.HandleMsgFuncData)
end
end
function this.HandleMsgFuncData(ret)
if ret.funcid==FuncProp.PET_changeModel then
if ret.errcode==Error.Success then
CS.NotifyMgr.Instance:AddNotify(NotifyType.Function,LanguageMnager.Get(84000),0)
else
GetErrorString(ret.errcode,NotifyType.Function)
end
end
end
--幻化
function this.ExcuteFit(id)
id = tonumber(id)
local ret = {}
ret.funcid=FuncProp.PET_changeModel
ret.datax=id
Networks.Push_msg(Network.MsgFuncDataOperatorRequest ,ret)
end
return this
在Parsedata中处理并将数据缓存进table内,每次主干获取数据先判断table是否为空,不为空直接返回。
不同模型的加载用到的是创建多个空的gameobject作为模型位置点,每次创建模型判断节点下是否为空,不为空则清空再创建。
模型的创建在支线中生成,那么在主线中怎么控制模型的隐藏呢?
在主线有个方法叫checkride,当场景中有马则上马,没有就站着,如果有女神就让女神出现,马和人节点影藏。
由于人需要坐到马背上,所以在创建模型的时候需要每次把人物放到人物节点下防止误删。
非常nice的镜头c#代码。摄像头用lateupdate,保证所有物体移动以后它才开始移动。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour {
public Transform Camera;//摄像机
public Transform player;//目标物体
public float scrollspeed = 10;//滑动速度
public float maxview = 20;//放大最大size
Vector3 offset;
Vector3 pos = new Vector3(0, 0, 0);//用于记录第二个点
Vector3 startpos;
float view;
float distance;
void Start()
{
startpos = Camera.position;
view = Camera.GetComponent<Camera>().fieldOfView;
}
void ScrollView(float x)
{
if (x < 0)
{
float posx = Camera.position.x;
float posy = Camera.position.y;
float posz = Camera.position.z;
if (posy + x <= 0)
{
Camera.position = new Vector3(posx, 0, posz);
if (pos == Vector3.zero) pos = Camera.position;
float camview = Camera.GetComponent<Camera>().fieldOfView;
if (camview + x < maxview) return;
else Camera.GetComponent<Camera>().fieldOfView += x;
}
else
{
Camera.position = new Vector3(posx, posy + x, posz);
}
}
else if (x > 0)
{
if (pos == Vector3.zero)
{
float posx = Camera.position.x;
float posy = Camera.position.y;
float posz = Camera.position.z;
if (posy + x >= startpos.y) return;
Camera.position = new Vector3(posx, posy + x, posz);
}else
{
Camera.GetComponent<Camera>().fieldOfView -= x;
float t = Camera.GetComponent<Camera>().fieldOfView;
if (t <= view)
{
Camera.GetComponent<Camera>().fieldOfView = view;
pos = Vector3.zero;
}
}
}
Camera.LookAt(player);
}
// Update is called once per frame
void LateUpdate ()
{
float x = Input.GetAxis("Mouse ScrollWheel");
x *= scrollspeed;
ScrollView(x);
}
}
u3d漫漫征途,加油~