示例#1
0
    /// <summary>
    /// 加载资源。
    /// </summary>
    /// <param name="path"></param>
    /// <param name="loaded"></param>
    private void LoadAssets(string path, Queue <Action> q, Action <Resource, MonoBehaviour, Action> loadResource, Action <Object> loaded, Action <Resource> progress = null)
    {
        if (String.IsNullOrEmpty(path))
        {
            Debug.LogError("LoadAssets null path.");
            if (loaded != null)
            {
                loaded(null);
            }
            return;
        }
        var resource = ResourceManager.GetResource(path);

        ResourceManager.AddReferenceCount(resource);
        if (resource.Object != null)//尝试Asset不为空直接返回,逻辑上与下面代码逻辑重复,确认方案后再看如何优化(其实不优化也可以啦)
        {
            Debug.LogWarning("Load loaded asset: " + path);
            if (loaded != null)
            {
                loaded(resource.Object);
            }
            return;
        }

        if (resource.IsDone)//尝试将已经加载完成的提前,以下的代码为重复代码,如果此功能有效再考虑封装
        {
            if (loaded != null)
            {
                if (resource.Object == null)
                {
                    if (!resource.RelativePath.EndsWith(".unity"))
                    {
                        Debug.LogError(string.Concat("Can not load: ", resource.RelativePath));
                    }
                    loaded(null);
                }
                else
                {
                    loaded(resource.Object);
                }
            }
            return;
        }
        if (m_isLoading[q])
        {
            q.Enqueue(() => WaitAndDo(resource, q, loadResource, loaded, progress));
        }
        else
        {
            m_isLoading[q] = true;
            //Debug.Log("isLoading true");
            WaitAndDo(resource, q, loadResource, loaded, progress);
        }
    }