mirror of
https://github.com/Cardidi/dotween-upm-fork.git
synced 2026-02-04 06:14:55 +08:00
Created custom plugin example in UnityTests.Unity5 > Assets > _Tests > CustomPlugin Example
This commit is contained in:
parent
49e6a0bb55
commit
a49d2a3f22
Binary file not shown.
@ -1,23 +1,27 @@
|
||||
using DG.Tweening;
|
||||
using DG.Tweening.Plugins;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class CustomPluginExampleBrain : BrainBase
|
||||
public class CustomPluginExampleBrain : MonoBehaviour
|
||||
{
|
||||
public Transform target;
|
||||
public Text txtCustomRange ; // Used to show the custom range tween results
|
||||
|
||||
CustomRange customRange = new CustomRange(0, 10);
|
||||
|
||||
// Store the plugin so you won't have to instantiate it every time you use it
|
||||
// (you can pass the same plugin instance to each tween, since they just do calculations and don't store data)
|
||||
static CustomRangePlugin customRangePlugin = new CustomRangePlugin();
|
||||
|
||||
void Start()
|
||||
{
|
||||
// DOTween.To(()=>target.position, x=> target.position = x, new Vector3(4, 4, 0), 1.5f)
|
||||
// DOTween.To(new PlugCustomPlugin(()=>target.position, x=> target.position = x, 4), 1.5f)
|
||||
// .SetDelay(2).SetRelative().SetLoops(5, LoopType.Yoyo).SetAutoKill(false)
|
||||
// .OnStart(()=> Debug.Log("Start"))
|
||||
// .OnStepComplete(()=> Debug.Log("Step Complete"))
|
||||
// .OnComplete(()=> Debug.Log("Complete"));
|
||||
// The difference with the regular generic way is simply
|
||||
// that you have to pass the plugin to use as an additional first parameter
|
||||
DOTween.To(customRangePlugin, () => customRange, x => customRange = x, new CustomRange(20, 100), 4);
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
if (GUILayout.Button("Flip")) DOTween.FlipAll();
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
txtCustomRange.text = customRange.min + "\n" + customRange.max;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
public struct CustomRange
|
||||
{
|
||||
public float min, max;
|
||||
|
||||
public CustomRange(float min, float max)
|
||||
{
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public static CustomRange operator +(CustomRange c1, CustomRange c2)
|
||||
{
|
||||
return new CustomRange(c1.min + c2.min, c1.max + c2.max);
|
||||
}
|
||||
|
||||
public static CustomRange operator -(CustomRange c1, CustomRange c2)
|
||||
{
|
||||
return new CustomRange(c1.min - c2.min, c1.max - c2.max);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c33f7a63008fad4eac4198a5417ad8c
|
||||
timeCreated: 1444381777
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,65 @@
|
||||
using DG.Tweening;
|
||||
using DG.Tweening.Core;
|
||||
using DG.Tweening.Core.Easing;
|
||||
using DG.Tweening.Core.Enums;
|
||||
using DG.Tweening.Plugins.Core;
|
||||
using DG.Tweening.Plugins.Options;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Custom DOTween plugin example.
|
||||
/// This one tweens a CustomRange value, but you can also create plugins just to do weird stuff, other than to tween custom objects
|
||||
/// </summary>
|
||||
public class CustomRangePlugin : ABSTweenPlugin<CustomRange, CustomRange, NoOptions>
|
||||
{
|
||||
// Leave this empty
|
||||
public override void Reset(TweenerCore<CustomRange, CustomRange, NoOptions> t) {}
|
||||
|
||||
// Sets the values in case of a From tween
|
||||
public override void SetFrom(TweenerCore<CustomRange, CustomRange, NoOptions> t, bool isRelative)
|
||||
{
|
||||
CustomRange prevEndVal = t.endValue;
|
||||
t.endValue = t.getter();
|
||||
t.startValue = isRelative ? t.endValue + prevEndVal : prevEndVal;
|
||||
t.setter(t.startValue);
|
||||
}
|
||||
|
||||
// Used by special plugins, just let it return the given value
|
||||
public override CustomRange ConvertToStartValue(TweenerCore<CustomRange, CustomRange, NoOptions> t, CustomRange value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
// Determines the correct endValue in case this is a relative tween
|
||||
public override void SetRelativeEndValue(TweenerCore<CustomRange, CustomRange, NoOptions> t)
|
||||
{
|
||||
t.endValue = t.startValue + t.changeValue;
|
||||
}
|
||||
|
||||
// Sets the overall change value of the tween
|
||||
public override void SetChangeValue(TweenerCore<CustomRange, CustomRange, NoOptions> t)
|
||||
{
|
||||
t.changeValue = t.endValue - t.startValue;
|
||||
}
|
||||
|
||||
// Converts a regular duration to a speed-based duration
|
||||
public override float GetSpeedBasedDuration(NoOptions options, float unitsXSecond, CustomRange changeValue)
|
||||
{
|
||||
// Not implemented in this case (but you could implement your own logic to convert duration to units x second)
|
||||
return unitsXSecond;
|
||||
}
|
||||
|
||||
// Calculates the value based on the given time and ease
|
||||
public override void EvaluateAndApply(NoOptions options, Tween t, bool isRelative, DOGetter<CustomRange> getter, DOSetter<CustomRange> setter, float elapsed, CustomRange startValue, CustomRange changeValue, float duration, bool usingInversePosition, UpdateNotice updateNotice)
|
||||
{
|
||||
CustomRange res = getter();
|
||||
float easeVal = EaseManager.Evaluate(t, elapsed, duration, t.easeOvershootOrAmplitude, t.easePeriod);
|
||||
|
||||
// Here I use startValue directly because CustomRange a struct, so it won't reference the original.
|
||||
// If CustomRange was a class, I should create a new one to pass to the setter
|
||||
startValue.min += changeValue.min * easeVal;
|
||||
startValue.max += changeValue.max * easeVal;
|
||||
setter(startValue);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3ffdccaa6b21544baad840b4b779d25
|
||||
timeCreated: 1444382093
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e19028f4214f3241b7878d71060677c
|
||||
folderAsset: yes
|
||||
timeCreated: 1444384496
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user