unity分帧加载脚本

using HeurekaGames;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 分帧加载 fsLoaded
/// 示例
///  Vector2 initPos = Vector2.zero;
///  comp_fsLoaded_test.Init(5, (g,index) =>
///    {
///        g.SetActiveEx(true);
///        g.GetComponent<RectTransform>().anchoredPosition = initPos;
///        initPos += new Vector2(0, 200);
///}, (t) => { LogTool.EditorError($"{t.Count}"); },0.1f);
/// </summary>
public class FrameSegmentLoaded : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }





    public GameObject mPrefab;//需要加载的模板
    private int loadCount = 0;//加载的个数
    private int needloadCount = 0;//需要加载的数量
    private Action<GameObject,int> afterPerCreate;//加载后回调
    private Action<List<GameObject>> overCallBack;//加载完毕回调
    List<GameObject> lists;
    private bool isloadOver;//是否加载完毕

    /// <summary>
    /// 初始化分帧加载
    /// </summary>
    /// <param name="needloadAllCount">需要加载的个数</param>
    /// <param name="cb">加载后回调</param>
    /// <param name="loadWaitTimer">加载间隔 ,默认为帧调用</param>
    public void Init(int needloadAllCount,Action<GameObject,int> cb,Action<List<GameObject>> overcb,float loadWaitTimer = -1)
    {
        afterPerCreate = cb;
        this.loadWaitTimer = loadWaitTimer;
        overCallBack = overcb;

        //第二次调用时如果已经加载完毕
        if (isloadOver)
        {
            loadCount = needloadAllCount > needloadCount? needloadCount: needloadAllCount;
            isloadOver = needloadAllCount > needloadCount ? false:true;
            for (int i = 0; i < loadCount; i++)
            {
                if (i <= (needloadAllCount - 1))
                {
                    lists[i].SetActive(true);
                    afterPerCreate?.Invoke(lists[i],i);
                }
                else
                {
                    lists[i].SetActive(false);
                }
            }
        }
        else
        {
            loadCount = 0;
            lists = new List<GameObject>();
            needloadCount = needloadAllCount;
            isloadOver = false;
        } 
    }




    // Update is called once per frame
    void Update()
    {
        Loaded();
    }

    private float loadWaitTimer = 0;
    private float loadTimer = 0;
    private void Loaded()
    {
        if (isloadOver) return;
        if (needloadCount == 0) return;
        loadTimer += Time.deltaTime;
        if (loadTimer >= loadWaitTimer)
        {
            loadTimer = 0;
            if (loadCount < needloadCount&& mPrefab!=null)
            {
                GameObject go = GameObject.Instantiate(mPrefab, transform);
                go.SetActiveEx(true);
                afterPerCreate?.Invoke(go, loadCount);
                lists.Add(go);
                loadCount++;
            }

            if (loadCount == needloadCount)
            {
                isloadOver = true;
                overCallBack?.Invoke(lists);
            }

        }     
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值