Created by Hiroya Aramaki (Makihiro)
Modiferty is a great solution for making modifications to properties.
In games, there are often situations in which the status of characters, weapons, etc. temporarily change.
Modiferty can be used in the following situations.
- Want to modify the in-game character status temporally.
Download any version from releases.
Releases: https://github.com/mackysoft/Modiferty/releases
Add "MackySoft.Modiferty" namespace into using area.
usingMackySoft.Modiferty;The following code implements a temporary increase the character attack power.
publicclassCharacter:MonoBehaviour{publicinthealth=3;publicModifiableIntattackPower=newModifiableInt(baseValue:1);publicvoidAttack(Charactertarget){target.health-=attackPower.Evaluate();}}publicclassPowerUpItem:MonoBehaviour{publicAdditiveModifierIntadditivePower=newAdditiveModifierInt(1);publicvoidModify(Charactertarget){target.attackPower.Modifiers.Add(additivePower);// Same as below.// target.attackPower.AddModifier(additivePower);}}If you want to modify the value without using ModifiableProperty, use a ModifierList.
ModifierList<int>m_DamageModifiers=newModifierList<int>;// Add something modifiers.m_DamageModifiers.Add(modifier);// Evaluate the damage.intevaluatedDamage=m_DamageModifiers.Evaluate(damage);ModifiableList is also used in the ModifiableProperty implementation.
Basic operator modifiers are provided.
- Additive Modifier
- Subtractive Modifier
- Multiply Modifier
- Division Modifier
A variety of other unique modifiers are also available.
The given value ignored and the specified value returned.
varsetModifier=newSetModifierInt(0);character.attackPower.AddModifier(setModifier);// result is always 0.intresult=character.attackPower.Evaluate();The lambda formula allows you to improvise complex modifiers.
varcreateModifier=newCreateModifier<int>(value =>{intresult;// Do something process...returnresult;});Modiferty supports integration with some external assets.
Install UniRx and define MODIFERTY_UNIRX to enable integration with UniRx.
UniRx: https://github.com/neuecc/UniRx
The integration with UniRx mainly adds the following APIs to allow you to observe the values of Modiferty.
ReactiveModifierList<T>ReactiveModifiableProperty<T>
usingUnityEngine;usingUnityEngine.UI;usingMackySoft.Modiferty;usingUniRx;publicclassCharacter:MonoBehaviour{// Define attackPower as ReactiveModifiableProperty.publicReactiveModifiableIntattackPower=newReactiveModifiableInt(baseValue:1);.....}publicclassCharacterAttackPowerUI:MonoBehaviour{publicCharactercharacter;publicTextattackPowerTsxt;voidAwake(){// You can observe changes BaseValue and Modifiers.character.attackPower.ObserveChanged().Subscribe(property =>{// Apply the attackPower change to the text.attackPowerText.text=property.Evaluate();});}}Hiroya Aramaki is a indie game developer in Japan.
- Blog: https://mackysoft.net/blog
- Twitter: https://twitter.com/makihiro_dev
This library is under the MIT License.