| Readme Languages: | ||
| English | Русский | |
A simple implementation of a buff-system for use in scripts. Its advantage is that you can use any C#-types as the buff value and value-processors.
In addition to the default processors for the buff values, you can create your own implementations.
- Unity 2021.3 or later
Use ONE of two options:
- Open Package Manager from Window > Package Manager.
- Click the "+" button > Add package from git URL.
- Enter the following URL:
https://github.com/Demexis/Unity-Buffs.git
Alternatively, open Packages/manifest.json and add the following to the dependencies block:
{
"dependencies": {
"com.demegraunt.buffs": "https://github.com/Demexis/Unity-Buffs.git"
}
}Download a unity package from the latest release.
1) Create a buff with the desired type and bind an original value:
// This variable can change its value!publicfloatoriginalValue=1f;
...var buff =newBuff<float>(()=>originalValue);2) Add processor(-s) that will sequentially transform the original value:
privatereadonlyGuidprocessorId=Guid.NewGuid();
...buff.Add(processorId,newBuffProcessor<float>(value =>value*4f));3) Calculate the buff value:
varresultValue=buff.Calculate();Debug.Log(resultValue);// prints 44) Replace the processor:
buff.Replace(processorId,newBuffProcessor<float>(value =>value+20f));resultValue=buff.Calculate();Debug.Log(resultValue);// prints 215) Remove the processor:
buff.Remove(processorId);resultValue=buff.Calculate();Debug.Log(resultValue);// prints 1 - the original value1: A sprint mechanic using a buff
usingSystem;usingDemegraunt.Framework;usingUnityEngine;publicsealedclassMovementWithBuff:MonoBehaviour{[field:SerializeField]publicfloatSpeed{get;set;}=1f;[field:SerializeField]publicfloatSprintMultiplier{get;set;}=4f;privateBuff<float>SpeedBuff{get;set;}privatereadonlyGuidsprintProcessorId=Guid.NewGuid();privatevoidAwake(){SpeedBuff=newBuff<float>(()=>Speed);}privatevoidUpdate(){if(Input.GetKeyDown(KeyCode.LeftShift)){ActivateSprint();}elseif(Input.GetKeyUp(KeyCode.LeftShift)){DeactivateSprint();}transform.position+=Vector3.right*(Time.deltaTime*SpeedBuff.Calculate());}publicvoidActivateSprint(){if(SpeedBuff.Contains(sprintProcessorId)){return;}SpeedBuff.Add(sprintProcessorId,newBuffProcessor<float>(originalValue =>originalValue*SprintMultiplier));}publicvoidDeactivateSprint(){SpeedBuff.Remove(sprintProcessorId);}}2: One buff affects the other
publicsealedclassWalkRunBuffs:MonoBehaviour{[field:SerializeField]publicfloatSpeed{get;set;}=1f;[field:SerializeField]publicfloatSprintMultiplier{get;set;}=4f;privateBuff<float>WalkBuff{get;set;}privateBuff<float>RunBuff{get;set;}privatereadonlyGuidslowdownProcessorId=Guid.NewGuid();privatevoidAwake(){WalkBuff=newBuff<float>(()=>Speed);RunBuff=newBuff<float>(()=>WalkBuff.Calculate()*SprintMultiplier);}privatevoidStart(){Slowdown();}privatevoidUpdate(){// always runningtransform.position+=Vector3.right*(Time.deltaTime*RunBuff.Calculate());}publicvoidSlowdown(){WalkBuff.Add(slowdownProcessorId,newFloatBuffMultiplier(0.1f));}}The package contains custom processors such as:
FloatBuffAdder,FloatBuffMultiplier,IntBuffAdder,IntBuffMultiplier. You can define your own types of processors by inheriting fromBuffProcessor<T>.If you cache the created processor instance, you can access the
ProcessCallbackproperty to change the processing logic, instead of creating a completely new instance:
varbuff=newBuff<float>(1f);varprocessor=newBuffProcessor<float>(value =>value+5f);buff.Add(Guid.NewGuid(),processor);Debug.Log(buff.Calculate());// prints 6processor.ProcessCallback= value =>value/2f;Debug.Log(buff.Calculate());// prints 0.5- If you don't want to write separately a field with a parameter (
float,int, ...) and a field with a buff, you can use the types:FloatBuffField,IntBuffField. You can also make an implementation for a new type by inheriting fromBuffField<T>and adding the[Serializable]attribute.