框架上传
This commit is contained in:
109
Assets/00.StaryEvo/Runtime/BindableProperty/BindableProperty.cs
Normal file
109
Assets/00.StaryEvo/Runtime/BindableProperty/BindableProperty.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
|
||||
public interface IBindableProperty<T> : IReadonlyBindableProperty<T>
|
||||
{
|
||||
new T Value { get; set; }
|
||||
void SetValueWithoutEvent(T newValue);
|
||||
}
|
||||
|
||||
public interface IReadonlyBindableProperty<T>
|
||||
{
|
||||
T Value { get; }
|
||||
|
||||
IUnRegister RegisterWithInitValue(Action<T> action);
|
||||
void UnRegister(Action<T> onValueChanged);
|
||||
IUnRegister Register(Action<T> onValueChanged);
|
||||
}
|
||||
|
||||
public class BindableProperty<T> : IBindableProperty<T>
|
||||
{
|
||||
public BindableProperty(T defaultValue = default)
|
||||
{
|
||||
mValue = defaultValue;
|
||||
}
|
||||
|
||||
protected T mValue;
|
||||
|
||||
public T Value
|
||||
{
|
||||
get => GetValue();
|
||||
set
|
||||
{
|
||||
if (value == null && mValue == null) return;
|
||||
if (value != null && value.Equals(mValue)) return;
|
||||
|
||||
SetValue(value);
|
||||
mOnValueChanged?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void SetValue(T newValue)
|
||||
{
|
||||
mValue = newValue;
|
||||
}
|
||||
|
||||
protected virtual T GetValue()
|
||||
{
|
||||
return mValue;
|
||||
}
|
||||
|
||||
public void SetValueWithoutEvent(T newValue)
|
||||
{
|
||||
mValue = newValue;
|
||||
}
|
||||
|
||||
private Action<T> mOnValueChanged = (v) => { };
|
||||
|
||||
public IUnRegister Register(Action<T> onValueChanged)
|
||||
{
|
||||
mOnValueChanged += onValueChanged;
|
||||
return new BindablePropertyUnRegister<T>()
|
||||
{
|
||||
BindableProperty = this,
|
||||
OnValueChanged = onValueChanged
|
||||
};
|
||||
}
|
||||
|
||||
public IUnRegister RegisterWithInitValue(Action<T> onValueChanged)
|
||||
{
|
||||
onValueChanged(mValue);
|
||||
return Register(onValueChanged);
|
||||
}
|
||||
|
||||
public static implicit operator T(BindableProperty<T> property)
|
||||
{
|
||||
return property.Value;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString();
|
||||
}
|
||||
|
||||
public void UnRegister(Action<T> onValueChanged)
|
||||
{
|
||||
mOnValueChanged -= onValueChanged;
|
||||
}
|
||||
}
|
||||
|
||||
public class BindablePropertyUnRegister<T> : IUnRegister
|
||||
{
|
||||
public BindableProperty<T> BindableProperty { get; set; }
|
||||
|
||||
public Action<T> OnValueChanged { get; set; }
|
||||
|
||||
public void UnRegister()
|
||||
{
|
||||
BindableProperty.UnRegister(OnValueChanged);
|
||||
|
||||
BindableProperty = null;
|
||||
OnValueChanged = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57e121dc01fbf6b4f973d5170b1b7050
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user