< 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.")]
 19214    public float minSensitivity = 5f;
 15    [Tooltip("Max value for the calculated scroll sensitivity.")]
 19216    public float maxSensitivity = 50f;
 17    [Tooltip("Multiplier applied to the sensitivity value (sensitivityMultiplier = 1 for not applying).")]
 19218    public float sensitivityMultiplier = 0.5f;
 19    [Tooltip("True to recalculate each time the game object is enabled.")]
 19220    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    {
 7337        KillCurrentCoroutine();
 7338        recalculateCoroutine = CoroutineStarter.Start(RecalculateCoroutine());
 7339    }
 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
 7344        yield return null;
 45
 1646        if (mainScrollRect.content != null)
 47        {
 1648            float newSensitivity = (mainScrollRect.content.rect.height * minSensitivity / mainScrollRect.viewport.rect.h
 1649            mainScrollRect.scrollSensitivity = Mathf.Clamp(newSensitivity, minSensitivity, maxSensitivity);
 50        }
 1651    }
 52
 53    private void KillCurrentCoroutine()
 54    {
 13655        if (recalculateCoroutine == null)
 6356            return;
 57
 7358        CoroutineStarter.Stop(recalculateCoroutine);
 7359        recalculateCoroutine = null;
 7360    }
 61}