//
// Copyright (c) 2017 Anthony Marmont. All rights reserved.
// Licensed for use under the Unity Asset Store EULA. See https://unity3d.com/legal/as_terms for full license information.
//
#if UNITY_EDITOR
#pragma warning disable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
namespace AssetIcons.Editors.Preferences
{
///
/// A base-class for the generic wrapper object that fires events whenever its value changes.
///
public abstract class EventField
{
///
/// Event that is fired whenever the value of this changes.
///
public event Action OnChanged;
///
/// Invokes the event.
///
/// Fired upon calling this method.
protected void InvokeChanged()
{
if (OnChanged != null)
{
OnChanged();
}
}
}
///
/// A generic wrapper object that fires events whenever its value changes.
///
///
///
public class EventField : EventField, ISerializationCallbackReceiver
{
[SerializeField]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private T internalValue;
[SerializeField]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private T beforeSerialization;
///
/// The value that this wrapper currently represents.
///
/// Fired whenever the value of this property change.
public T Value
{
get
{
return internalValue;
}
set
{
if (!EqualityComparer.Default.Equals(internalValue, value))
{
internalValue = value;
InvokeChanged();
}
}
}
///
/// Constructs a new instance of this .
///
public EventField()
{
}
///
/// Constructs a new instance of this with a default value.
///
/// The default value for this .
public EventField(T defaultValue)
{
internalValue = defaultValue;
}
///
/// Implements the Unity interface.
///
public void OnAfterDeserialize()
{
if (!EqualityComparer.Default.Equals(internalValue, beforeSerialization))
{
InvokeChanged();
}
}
///
/// Implements the Unity interface.
///
public void OnBeforeSerialize()
{
beforeSerialization = internalValue;
}
}
}
#pragma warning restore
#endif