< Summary

Class:BaseVariableAsset[T]
Assembly:ScriptableObjects
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ScriptableObject/Variables/BaseVariableAsset.cs
Covered lines:10
Uncovered lines:7
Coverable lines:17
Total lines:72
Line coverage:58.8% (10 of 17)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Set(...)0%330100%
Get()0%2100%
implicit operator T(...)0%2100%
Equals(...)0%110100%
RaiseOnChange()0%6200%
OnEnable()0%110100%
CleanUp()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ScriptableObject/Variables/BaseVariableAsset.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5public abstract class BaseVariableAsset : ScriptableObject
 6{
 7#if UNITY_EDITOR
 8    protected abstract void RaiseOnChange();
 9
 10    [UnityEditor.CustomEditor(typeof(BaseVariableAsset), true)]
 11    public class BaseVariableAssetEditor : UnityEditor.Editor
 12    {
 13        public override void OnInspectorGUI()
 14        {
 15            DrawDefaultInspector();
 16            if (Application.isPlaying && GUILayout.Button("Raise OnChange"))
 17            {
 18                ((BaseVariableAsset)target).RaiseOnChange();
 19            }
 20        }
 21    }
 22#endif
 23}
 24
 25public class BaseVariableAsset<T> : BaseVariableAsset, IEquatable<T>
 26{
 27    public delegate void Change(T current, T previous);
 28
 29    public event Change OnChange;
 30
 31    [SerializeField] protected T value;
 32
 33    public void Set(T newValue)
 34    {
 15536335        if (Equals(newValue))
 14075736            return;
 37
 1460638        var previous = value;
 1460639        value = newValue;
 1460640        OnChange?.Invoke(value, previous);
 628241    }
 42
 043    public T Get() { return value; }
 44
 045    public static implicit operator T(BaseVariableAsset<T> value) => value.value;
 46
 47    public virtual bool Equals(T other)
 48    {
 49        //NOTE(Brian): According to benchmarks I made, this statement costs about twice than == operator for structs.
 50        //             However, its way more costly for primitives (tested only against float).
 51
 52        //             Left here only for fallback purposes. Optimally this method should be always overriden.
 22953        return EqualityComparer<T>.Default.Equals(value, other);
 54    }
 55
 56#if UNITY_EDITOR
 057    protected sealed override void RaiseOnChange() => OnChange?.Invoke(value, value);
 58
 59    private void OnEnable()
 60    {
 961        Application.quitting -= CleanUp;
 962        Application.quitting += CleanUp;
 963    }
 64
 65    private void CleanUp()
 66    {
 067        Application.quitting -= CleanUp;
 068        if (UnityEditor.AssetDatabase.Contains(this)) //It could happen that the base variable has been created in runti
 069            Resources.UnloadAsset(this);
 070    }
 71#endif
 72}