コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Addressable Asset System で PercentComplete が正常な値を返してくれない場合

概要

自分の環境だと 1.9.2 以降、PercentComplete が正常な値を返してくれず
プログレスバーの表示がおかしくなる現象に遭遇したが
PercentComplete 周りの処理のみ 1.8.5 以前のものに戻したら
正常に動作するようになった

ChainOperation.cs

protected override float Progress
{
    get
    {
        float total       = 0f;
        int   numberOfOps = 0;

        if (m_DepOp.IsValid())
        {
            total += m_DepOp.PercentComplete;
            numberOfOps++;
        }

        if (m_WrappedOp.IsValid())
        {
            total += m_WrappedOp.PercentComplete;
            numberOfOps++;
        }

        return total / numberOfOps;
    }
}

GroupOperation.cs

protected override float Progress
{
    get
    {
        List<AsyncOperationHandle> allDependentOperations = new List<AsyncOperationHandle>();
        allDependentOperations.AddRange(Result);

        foreach(var handle in Result)
            handle.GetDependencies(allDependentOperations);

        if (Result.Count < 1)
            return 1f;
        float total = 0;
        for (int i = 0; i < allDependentOperations.Count; i++)
            total += allDependentOperations[i].PercentComplete;
        return total / allDependentOperations.Count;
    }
}

ProviderOperation.cs

protected override float Progress
{
    get
    {
        if (m_GetProgressCallback == null)
            return 0.0f;
        try
        {
            List<AsyncOperationHandle> allDependantOperations = new List<AsyncOperationHandle>() { m_DepOp };

            float total = m_GetProgressCallback();

            if (m_DepOp.IsValid())
            {
                foreach (var handle in m_DepOp.Result)
                    if (handle.IsValid())
                        handle.GetDependencies(allDependantOperations);
            }

            foreach (var handle in allDependantOperations)
                if(handle.IsValid())
                    total += handle.PercentComplete;

            return total / (DependencyCount + 1);
        }
        catch
        {
            return 0.0f;
        }
    }
}