大侠
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

112 lines
3.5 KiB

import { _decorator, Component, ScrollView, Prefab, instantiate, Layout, Node, UITransform, Enum } from "cc";
const { ccclass, property ,menu} = _decorator;
export enum RenderType{
Node,
Prefab,
}
@ccclass
@menu("列表布局/分帧布局")
export default class FrameLayout extends Component{
@property(Layout)
mLayout: Layout = null;
@property({type:Enum(RenderType)})
mRenderType:RenderType = RenderType.Node;
@property({ type: Node, displayName: "渲染预设节点", visible: function () { return this.mRenderType == RenderType.Node } })
m_RendererItem: Node = null;
@property({ type: Prefab, displayName: "渲染预设节点", visible: function () { return this.mRenderType == RenderType.Prefab } })
m_RendererPerfab: Prefab = null;
/**参数1 index 参数2 渲染的物体 */
m_ItemRendererFuntion: (index: number, Item: Node) => void = null;
m_ItemClick: Function;
private _ItemCount: number = 0;
private ChildItemList: Node[] = [];
private curFrame:number = 0;
public MaxFrame:number = 2;
ClearAll(){
this.mLayout.node.removeAllChildren();
this.ChildItemList = [];
this.renderIndex =0;
}
//private RenderCount:number = 0;
renderIndex:number = 0;
getChildItem(index:number){
if(index<this.ChildItemList.length){
return this.ChildItemList[index];
}
return null;
}
get m_ItemCount():number{
return this._ItemCount;
}
set m_ItemCount(value:number){
this._ItemCount = value;
this.SetRendererListItem();
}
getitemByindex(idnex: number) {
if (idnex < this.ChildItemList.length) {
return this.ChildItemList[idnex];
}
return null;
}
private createItem(){
return this.mRenderType == RenderType.Node? instantiate(this.m_RendererItem):instantiate(this.m_RendererPerfab);
}
protected update(dt: number): void {
this.curFrame++;
if (this.curFrame < this.MaxFrame) { return; }
this.curFrame = 0;
if (this.renderIndex < this._ItemCount) {
if (!this.ChildItemList[this.renderIndex]) {
let Item:Node = this.createItem();
Item.active = true;
this.ChildItemList.push(Item);
this.mLayout.node.addChild(Item);
this.mLayout.updateLayout();
this.m_ItemClick && CNodeUtils.addBtnComponent(Item, this.m_ItemClick.bind(this, this.renderIndex, Item))
}
this.ChildItemList[this.renderIndex].active = true;
this.m_ItemRendererFuntion && this.m_ItemRendererFuntion(this.renderIndex, this.ChildItemList[this.renderIndex]);
++this.renderIndex;
}
let TempStarIndex = this._ItemCount;
for (; TempStarIndex < this.ChildItemList.length; TempStarIndex++) {
if (this.ChildItemList[TempStarIndex].active) {
this.ChildItemList[TempStarIndex].active = false;
} else {
break;
}
}
}
SetRendererListItem() {
this.renderIndex = 0;
for (let i = 0; i < this._ItemCount; i++) {
if (i < this.ChildItemList.length) {
if (!this.ChildItemList[i].active) {
this.ChildItemList[i].active = true;
}
this.m_ItemRendererFuntion && this.m_ItemRendererFuntion(i, this.ChildItemList[i]);
++this.renderIndex;
} else {
break;
}
}
}
}