2023년 3월 30일 목요일

unity (waitUntil) 최적화 [unity coroutine optimization]

 



waitUntil을 추가했음



## 효과

yield new 로 생기는 가비지콜렉션을 줄일수있다.


참고

https://velog.io/@livelyjuseok/C-Unity-%EC%BD%94%EB%A3%A8%ED%8B%B4-Yield-%EC%B5%9C%EC%A0%81%ED%99%94-%ED%95%98%EA%B8%B0IEqualityComparer



## 사용예

```
yield return YieldCache.WaitForEndOfFrame;
        yield return YieldCache.WaitUntilcustom(() => isLoginSuccess);
        yield return YieldCache.WaitUntilcustom(() => FirebaseDataManager.Instance != null);
        yield return YieldCache.WaitUntilcustom(() => DataPersistenceManager.Instance != null);

        //yield return new WaitForEndOfFrame();
        //yield return new WaitUntil(() => isLoginSuccess);
        //yield return new WaitUntil(() => FirebaseDataManager.Instance !=null);
        //yield return new WaitUntil(() => DataPersistenceManager.Instance != null);

```




## 최적화 코드
```csharp 최적화 코드
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
static class YieldCache
{
    class FloatComparer : IEqualityComparer<float>
    {
        bool IEqualityComparer<float>.Equals(float x, float y)
        {
            return x == y;
        }
        int IEqualityComparer<float>.GetHashCode(float obj)
        {
            return obj.GetHashCode();
        }
    }


    class FloatComparerbool : IEqualityComparer<Func<bool>>
    {
        bool IEqualityComparer<Func<bool>>.Equals(Func<bool> x, Func<bool> y)
        {
            return x == y;
        }
        int IEqualityComparer<Func<bool>>.GetHashCode(Func<bool> obj)
        {
            return obj.GetHashCode();
        }
    }

    public static readonly WaitForEndOfFrame WaitForEndOfFrame = new WaitForEndOfFrame();
    public static readonly WaitForFixedUpdate WaitForFixedUpdate = new WaitForFixedUpdate();

    private static readonly Dictionary<float, WaitForSeconds> _timeInterval = new Dictionary<float, WaitForSeconds>(new FloatComparer());
    private static readonly Dictionary<float, WaitForSecondsRealtime> _timeIntervalReal = new Dictionary<float, WaitForSecondsRealtime>(new FloatComparer());
    private static readonly Dictionary<Func<bool>, WaitUntil> _waitUntill = new Dictionary<Func<bool>, WaitUntil>(new FloatComparerbool());

    public static WaitForSeconds WaitForSeconds(float seconds)
    {
        WaitForSeconds wfs;
        if (!_timeInterval.TryGetValue(seconds, out wfs))
            _timeInterval.Add(seconds, wfs = new WaitForSeconds(seconds));
        return wfs;
    }

    public static WaitForSecondsRealtime WaitForSecondsRealTime(float seconds)
    {
        WaitForSecondsRealtime wfsReal;
        if (!_timeIntervalReal.TryGetValue(seconds, out wfsReal))
            _timeIntervalReal.Add(seconds, wfsReal = new WaitForSecondsRealtime(seconds));
        return wfsReal;
    }

    public static WaitUntil WaitUntilcustom(Func<bool> state)
    {
        WaitUntil wfsReal;
        if (!_waitUntill.TryGetValue(state, out wfsReal))
            _waitUntill.Add(state, wfsReal = new WaitUntil(state));
        return wfsReal;
    }

}







git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...