< Summary

Class:DynamicScrollSensitivity
Assembly:UIHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/UIHelpers/DynamicScrollSensitivity.cs
Covered lines:21
Uncovered lines:0
Coverable lines:21
Total lines:61
Line coverage:100% (21 of 21)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DynamicScrollSensitivity()0%110100%
OnEnable()0%220100%
OnDestroy()0%110100%
RecalculateSensitivity()0%110100%
RecalculateCoroutine()0%440100%
KillCurrentCoroutine()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/UIHelpers/DynamicScrollSensitivity.cs

#LineLine coverage
 1using System.Collections;
 2using UnityEngine;
 3using UnityEngine.UI;
 4
 5/// <summary>
 6/// Let to set a dynamic sensitivity value depending on the height of the content container.
 7/// Call to RecalculateSensitivity() function to calculate and apply the new sensitivity value.
 8/// </summary>
 9public class DynamicScrollSensitivity : MonoBehaviour
 10{
 11    [Tooltip("Scroll Rect component that will be modified.")]
 12    public ScrollRect mainScrollRect;
 13    [Tooltip("Min value for the calculated scroll sensitivity.")]
 40614    public float minSensitivity = 5f;
 15    [Tooltip("Max value for the calculated scroll sensitivity.")]
 40616    public float maxSensitivity = 50f;
 17    [Tooltip("Multiplier applied to the sensitivity value (sensitivityMultiplier = 1 for not applying).")]
 40618    public float sensitivityMultiplier = 0.5f;
 19    [Tooltip("True to recalculate each time the game object is enabled.")]
 40620    public bool recalculateOnEnable = true;
 21
 22    private Coroutine recalculateCoroutine = null;
 23
 24    private void OnEnable()
 25    {
 6726        if (recalculateOnEnable)
 6727            RecalculateSensitivity();
 6728    }
 29
 12630    private void OnDestroy() { KillCurrentCoroutine(); }
 31
 32    /// <summary>
 33    /// Recalculate the scroll sensitivity value depending on the current height of the content container.
 34    /// </summary>
 35    public void RecalculateSensitivity()
 36    {
 14237        KillCurrentCoroutine();
 14238        recalculateCoroutine = CoroutineStarter.Start(RecalculateCoroutine());
 14239    }
 40
 41    private IEnumerator RecalculateCoroutine()
 42    {
 43        // We need to wait for a frame for having available the correct height of the contentContainer after the OnEnabl
 14244        yield return null;
 45
 4046        if (mainScrollRect.content != null)
 47        {
 4048            float newSensitivity = (mainScrollRect.content.rect.height * minSensitivity / mainScrollRect.viewport.rect.h
 4049            mainScrollRect.scrollSensitivity = Mathf.Clamp(newSensitivity, minSensitivity, maxSensitivity);
 50        }
 4051    }
 52
 53    private void KillCurrentCoroutine()
 54    {
 20555        if (recalculateCoroutine == null)
 8756            return;
 57
 11858        CoroutineStarter.Stop(recalculateCoroutine);
 11859        recalculateCoroutine = null;
 11860    }
 61}