diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExample.unity b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExample.unity index be74781..9d1842a 100644 Binary files a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExample.unity and b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExample.unity differ diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExampleBrain.cs b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExampleBrain.cs index fed5cd6..8fbe005 100644 --- a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExampleBrain.cs +++ b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomPluginExampleBrain.cs @@ -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; + } } \ No newline at end of file diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRange.cs b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRange.cs new file mode 100644 index 0000000..e1962be --- /dev/null +++ b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRange.cs @@ -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); + } +} \ No newline at end of file diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRange.cs.meta b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRange.cs.meta new file mode 100644 index 0000000..9ecc930 --- /dev/null +++ b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRange.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8c33f7a63008fad4eac4198a5417ad8c +timeCreated: 1444381777 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRangePlugin.cs b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRangePlugin.cs new file mode 100644 index 0000000..b03be7e --- /dev/null +++ b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRangePlugin.cs @@ -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; + +/// +/// 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 +/// +public class CustomRangePlugin : ABSTweenPlugin +{ + // Leave this empty + public override void Reset(TweenerCore t) {} + + // Sets the values in case of a From tween + public override void SetFrom(TweenerCore 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 t, CustomRange value) + { + return value; + } + + // Determines the correct endValue in case this is a relative tween + public override void SetRelativeEndValue(TweenerCore t) + { + t.endValue = t.startValue + t.changeValue; + } + + // Sets the overall change value of the tween + public override void SetChangeValue(TweenerCore 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 getter, DOSetter 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); + } +} \ No newline at end of file diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRangePlugin.cs.meta b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRangePlugin.cs.meta new file mode 100644 index 0000000..29bff30 --- /dev/null +++ b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/CustomRangePlugin.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: b3ffdccaa6b21544baad840b4b779d25 +timeCreated: 1444382093 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped).meta b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped).meta new file mode 100644 index 0000000..4f7ba02 --- /dev/null +++ b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped).meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 0e19028f4214f3241b7878d71060677c +folderAsset: yes +timeCreated: 1444384496 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/PlugCustomPlugin.cs b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped)/PlugCustomPlugin.cs similarity index 100% rename from UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/PlugCustomPlugin.cs rename to UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped)/PlugCustomPlugin.cs diff --git a/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/PlugCustomPlugin.cs.meta b/UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped)/PlugCustomPlugin.cs.meta similarity index 100% rename from UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/PlugCustomPlugin.cs.meta rename to UnityTests.Unity5/Assets/_Tests/CustomPlugin Example/old (scrapped)/PlugCustomPlugin.cs.meta