模型加载及实现点击拖拽


function t_p_room_panel:onReadyOpen(nextStep)
    local path = ResourceLodUtil.getScenePath(kScenePath)    
    self:loadGameObject(path, function (prefab)
        -- 加载完成回调
        self._sceneRoot = prefab
        local rootTrans = self._sceneRoot.transform
        self:addCtrlGameObject(rootTrans:Find("light/Directional Light_scene (2)").gameObject)
        self:addCtrlGameObject(rootTrans:Find("cam").gameObject)
        self._worldCamera = rootTrans:Find("cam/main_cam"):GetComponent(typeof(CS_Unity.Camera))

        self:checkForNext(nextStep)  -- 因为这几个资源都是异步加载,所以要等到全部ready才执行后面的操作
    end)
    -- 组队讨伐场景相关:
    -- scene(ground/background/) 
    -- light(Directional light)
    -- record profile
    -- previewT4M(nothing
    -- char (point 1 2 3)
    -- cam(main_cam vcam_zhuhouzhengba)
    -- TeamPVEGround(player/ Plane 1 2 3、
            -- heroItem1(ui event listener/ self drag target/ pointer click handler))
            -- guangquan01 guangquan02 粒子特效
            -- show_mayunlu_timeline 【马云禄模型!!
        -- ground(box collider
        -- e_zdtfp_line_01(Clone)  拖拽箭头特效
    -- team_pve_room_player_itemPool

    -- ground prefab
    self:loadGameObject(kSceneTouchPath, function (prefab)
        self._sceneTouchRoot = prefab
        local rootTrans = self._sceneTouchRoot.transform
        
        for i = 1, self._kFullPerson, 1 do
            local planePath = string.format("player/Plane%s" , i)
            local go = rootTrans:Find(planePath).gameObject
            self._pointGoList[#self._pointGoList + 1] = {go, go.transform, nil ,nil}
        end

        -- 光圈特效1
        self.wallet:loadPrefabAsync(kEffectPath, function (_, handle)
            local effectPrefab = self.wallet:getAsset(handle)
            for i = 1, self._kFullPerson, 1 do
                local go = CS.UnityEngine.Object.Instantiate(effectPrefab)  -- 实例化
                go.transform:SetParent(self._pointGoList[i][2],false)    -- 设置父节点
                CSharpImport.ObjectHandler.SetObjectLocalPosition(go, 0, 0, 0)  -- 设置local position
                go:SetActive(false) -- 默认隐藏
                self._pointGoList[i][3] = go   -- 记录实例
            end

            self._isEffectReady = true

            self:checkForNext(nextStep)
        end)
        
        -- 光圈特效2
        self.wallet:loadPrefabAsync(kEffectSelectPath, function (_, handle)
            local effectPrefab = self.wallet:getAsset(handle)
            for i = 1, self._kFullPerson, 1 do
                local go = CS.UnityEngine.Object.Instantiate(effectPrefab)
                go.transform:SetParent(self._pointGoList[i][2],false)
                CSharpImport.ObjectHandler.SetObjectLocalPosition(go, 0, 0, 0)
                go:SetActive(false)
                self._pointGoList[i][4] = go
            end
        
            self._isEffectSelectReady = true
            self:checkForNext(nextStep)
        end)
    end)
end

-- 场景、模型都加载完成后,绑定拖拽、点击监听
-- 与每个模型同级,搞了一个heroItem,仅有boxCllider(碰撞体 碰撞范围)、selfDragTarget/PointerClickHandler/UIEventListener脚本/组件
-- TeamPVEGround(self._sceneTouchRoot)挂了一个InteractiveModelHelper脚本,里面存了3个gameObj(plane) 3个Clicks(PointerClickHandler) 3个Drags(selfDragTarget)
-- TeamPVEModelCore类:1.持有modeScene类,传递click、drag回调方法;2.加载箭头,刷新箭头
-- modelScene类:管理drag、click集合,self._dragEvents/self._clickEvents,key为选中的gameobjName,value为对应的click drag事件类
--  ModelClickEvent(字段:name/clickEvent(即PointerClickHandler实例)  方法:self._clickEvent:OnPointerClickCallback) self._clickEvent:ClearDragCallBack() release释放
--  ModelDragEvent(字段:name/_dragEvent(即selfDragTarget实例)  方法:    self._dragEvent:SetDragCallback(self) 以及各种拖拽回调方法     self._dragEvent:CleanLuaDelegates() self._dragEvents = nil
function t_p_room_panel:checkForNext(nextStep)
    -- body
    if self._sceneRoot and self._sceneTouchRoot and self._isEffectReady and self._isEffectSelectReady then  -- 场景加载ok、特效加载OK
        CSharpImport.ObjectHandler.SetObjectLocalPosition(self._sceneRoot.transform, 100, 0, 0)  -- 设置坐标
        self._sceneTouchRoot.transform:SetParent(self._sceneRoot.transform,false)  -- 设置ground父节点
        CSharpImport.ObjectHandler.SetObjectLocalPosition(self._sceneTouchRoot.transform, 0, 0, 0)  -- 设置ground坐标

        self._modelCore = TeamPVEModelCore:new()
        self._modelCore:bindModel(self._sceneRoot,self._sceneTouchRoot)
        self._modelCore:bindListener(
            nil,
            function (name,hitName)
                self:onDraging(self:getModelCoreIndex(name),self:getModelCoreIndex(hitName))
            end,
            function (name,hitName)
                self:onDragEnd(self:getModelCoreIndex(name),self:getModelCoreIndex(hitName))
            end,
            function (name)                
                self:onClickItem(self:getModelCoreIndex(name))
            end)
        self._modelCore:setCanHandleListener(function (name)
            -- body
            local index = self:getModelCoreIndex(name)
            return self:isRoomMaster() and self._param.players[team_pve_help:getTagByPosIndex(index)] ~= nil
        end)
        for i = 1, self._kFullPerson, 1 do
            self._modelCoreMapping[string.format("heroItem%s",i)] = i
        end

        if nextStep ~= nil then
            nextStep()
        end
    end
end

你可能感兴趣的:(模型加载及实现点击拖拽)