1
0
mirror of https://github.com/Cardidi/dotween-upm-fork.git synced 2025-12-20 09:16:02 +08:00

[bugfix] Better (and faster) solution to previous TweenerCore.Reset allocation issue, which has no problems with IL2CPP

This commit is contained in:
Demigiant 2016-12-08 12:10:46 +01:00
parent 94c2faae8f
commit 5f28d4bba8
64 changed files with 160 additions and 39 deletions

View File

@ -891,6 +891,14 @@
<param name="perc">The percentage (0 to 1) at which to get the point</param>
<param name="convertToConstantPerc">If TRUE constant speed is taken into account, otherwise not</param>
</member>
<member name="T:DG.Tweening.Plugins.Options.IPlugOptions">
<summary>
Base interface for all tween plugins options
</summary>
</member>
<member name="M:DG.Tweening.Plugins.Options.IPlugOptions.Reset">
<summary>Resets the plugin</summary>
</member>
<member name="T:DG.Tweening.Plugins.Vector3ArrayPlugin">
<summary>
This plugin generates some GC allocations at startup

View File

@ -891,6 +891,14 @@
<param name="perc">The percentage (0 to 1) at which to get the point</param>
<param name="convertToConstantPerc">If TRUE constant speed is taken into account, otherwise not</param>
</member>
<member name="T:DG.Tweening.Plugins.Options.IPlugOptions">
<summary>
Base interface for all tween plugins options
</summary>
</member>
<member name="M:DG.Tweening.Plugins.Options.IPlugOptions.Reset">
<summary>Resets the plugin</summary>
</member>
<member name="T:DG.Tweening.Plugins.Vector3ArrayPlugin">
<summary>
This plugin generates some GC allocations at startup

View File

@ -891,6 +891,14 @@
<param name="perc">The percentage (0 to 1) at which to get the point</param>
<param name="convertToConstantPerc">If TRUE constant speed is taken into account, otherwise not</param>
</member>
<member name="T:DG.Tweening.Plugins.Options.IPlugOptions">
<summary>
Base interface for all tween plugins options
</summary>
</member>
<member name="M:DG.Tweening.Plugins.Options.IPlugOptions.Reset">
<summary>Resets the plugin</summary>
</member>
<member name="T:DG.Tweening.Plugins.Vector3ArrayPlugin">
<summary>
This plugin generates some GC allocations at startup

View File

@ -5,6 +5,7 @@
// This work is subject to the terms at http://dotween.demigiant.com/license.php
using DG.Tweening.Core.Enums;
using DG.Tweening.Plugins.Options;
namespace DG.Tweening.Core
{
@ -22,7 +23,7 @@ namespace DG.Tweening.Core
// Prevents a tween to use a From setup even if passed
internal static TweenerCore<T1, T2, TPlugOptions> NoFrom<T1, T2, TPlugOptions>(this TweenerCore<T1, T2, TPlugOptions> t)
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{
t.isFromAllowed = false;
return t;
@ -30,7 +31,7 @@ namespace DG.Tweening.Core
// Sets the tween as blendable
internal static TweenerCore<T1, T2, TPlugOptions> Blendable<T1, T2, TPlugOptions>(this TweenerCore<T1, T2, TPlugOptions> t)
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{
t.isBlendable = true;
return t;

View File

@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using DG.Tweening.Core.Enums;
using DG.Tweening.Plugins.Options;
using UnityEngine;
namespace DG.Tweening.Core
@ -52,7 +53,7 @@ namespace DG.Tweening.Core
// Returns a new Tweener, from the pool if there's one available,
// otherwise by instantiating a new one
internal static TweenerCore<T1,T2,TPlugOptions> GetTweener<T1,T2,TPlugOptions>()
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{
TweenerCore<T1,T2,TPlugOptions> t;
// Search inside pool

View File

@ -10,6 +10,7 @@ using DG.Tweening.Core.Surrogates;
using System;
using DG.Tweening.Core.Enums;
using DG.Tweening.Plugins.Core;
using DG.Tweening.Plugins.Options;
using UnityEngine;
#pragma warning disable 1591
@ -20,7 +21,7 @@ namespace DG.Tweening.Core
// T1: type of value to tween
// T2: format in which value is stored while tweening
// TPlugOptions: options type
public class TweenerCore<T1,T2,TPlugOptions> : Tweener where TPlugOptions : struct
public class TweenerCore<T1,T2,TPlugOptions> : Tweener where TPlugOptions : struct, IPlugOptions
{
// SETUP DATA ////////////////////////////////////////////////
@ -131,7 +132,8 @@ namespace DG.Tweening.Core
if (tweenPlugin != null) tweenPlugin.Reset(this);
// plugOptions = new TPlugOptions(); // Generates GC because converts to an Activator.CreateInstance
plugOptions = Utils.InstanceCreator<TPlugOptions>.Create(); // Fixes GC allocation using workaround
// plugOptions = Utils.InstanceCreator<TPlugOptions>.Create(); // Fixes GC allocation using workaround (doesn't work with IL2CPP)
plugOptions.Reset(); // Alternate fix that uses IPlugOptions Reset
getter = null;
setter = null;
hasManuallySetStartValue = false;

View File

@ -4,8 +4,6 @@
// License Copyright (c) Daniele Giardini.
// This work is subject to the terms at http://dotween.demigiant.com/license.php
using System;
using System.Linq.Expressions;
using UnityEngine;
namespace DG.Tweening.Core
@ -40,11 +38,12 @@ namespace DG.Tweening.Core
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
// Uses code from BK > http://stackoverflow.com/a/1280832
public class InstanceCreator<T> where T : new()
{
static readonly Expression<Func<T>> _TExpression = () => new T();
static readonly Func<T> _TBuilder = _TExpression.Compile();
public static T Create() { return _TBuilder(); }
}
// (scrapped > doesn't work with IL2CPP)
// public class InstanceCreator<T> where T : new()
// {
// static readonly Expression<Func<T>> _TExpression = () => new T();
// static readonly Func<T> _TBuilder = _TExpression.Compile();
// public static T Create() { return _TBuilder(); }
// }
}
}

View File

@ -32,7 +32,7 @@ namespace DG.Tweening
public class DOTween
{
/// <summary>DOTween's version</summary>
public static readonly string Version = "1.1.510";
public static readonly string Version = "1.1.520";
///////////////////////////////////////////////
// Options ////////////////////////////////////
@ -399,7 +399,7 @@ namespace DG.Tweening
public static TweenerCore<T1, T2, TPlugOptions> To<T1, T2, TPlugOptions>(
ABSTweenPlugin<T1, T2, TPlugOptions> plugin, DOGetter<T1> getter, DOSetter<T1> setter, T2 endValue, float duration
)
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{ return ApplyTo(getter, setter, endValue, duration, plugin); }
/// <summary>Tweens only one axis of a Vector3 to the given value using default plugins.</summary>
@ -957,7 +957,7 @@ namespace DG.Tweening
static TweenerCore<T1, T2, TPlugOptions> ApplyTo<T1, T2, TPlugOptions>(
DOGetter<T1> getter, DOSetter<T1> setter, T2 endValue, float duration, ABSTweenPlugin<T1, T2, TPlugOptions> plugin = null
)
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{
InitCheck();
TweenerCore<T1, T2, TPlugOptions> tweener = TweenManager.GetTweener<T1, T2, TPlugOptions>();

View File

@ -96,6 +96,7 @@
<Compile Include="Plugins\Core\PathCore\ControlPoint.cs" />
<Compile Include="Plugins\DoublePlugin.cs" />
<Compile Include="Plugins\LongPlugin.cs" />
<Compile Include="Plugins\Options\IPlugOptions.cs" />
<Compile Include="Plugins\Options\PathOptions.cs" />
<Compile Include="Plugins\Options\QuaternionOptions.cs" />
<Compile Include="Plugins\Options\UintOptions.cs" />

View File

@ -6,12 +6,13 @@
using DG.Tweening.Core;
using DG.Tweening.Core.Enums;
using DG.Tweening.Plugins.Options;
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Core
{
// Public so it can be extended by custom plugins
public abstract class ABSTweenPlugin<T1,T2,TPlugOptions> : ITweenPlugin where TPlugOptions : struct
public abstract class ABSTweenPlugin<T1,T2,TPlugOptions> : ITweenPlugin where TPlugOptions : struct, IPlugOptions
{
public abstract void Reset(TweenerCore<T1, T2, TPlugOptions> t); // Resets specific TweenerCore stuff, not the plugin itself
public abstract void SetFrom(TweenerCore<T1, T2, TPlugOptions> t, bool isRelative);

View File

@ -30,6 +30,7 @@ using DOColorPlugin = DG.Tweening.Plugins.ColorPlugin;
using System;
using System.Collections.Generic;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using UnityEngine;
namespace DG.Tweening.Plugins.Core
@ -61,7 +62,7 @@ namespace DG.Tweening.Plugins.Core
// ===================================================================================
// INTERNAL METHODS ------------------------------------------------------------------
internal static ABSTweenPlugin<T1,T2,TPlugOptions> GetDefaultPlugin<T1,T2,TPlugOptions>() where TPlugOptions : struct
internal static ABSTweenPlugin<T1,T2,TPlugOptions> GetDefaultPlugin<T1,T2,TPlugOptions>() where TPlugOptions : struct, IPlugOptions
{
Type t1 = typeof(T1);
Type t2 = typeof(T2);
@ -127,7 +128,7 @@ namespace DG.Tweening.Plugins.Core
// Public so it can be used by custom plugins Get method
public static ABSTweenPlugin<T1, T2, TPlugOptions> GetCustomPlugin<TPlugin, T1, T2, TPlugOptions>()
where TPlugin : ITweenPlugin, new()
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{
Type t = typeof(TPlugin);
ITweenPlugin plugin;

View File

@ -7,8 +7,13 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct ColorOptions
public struct ColorOptions : IPlugOptions
{
public bool alphaOnly;
public void Reset()
{
alphaOnly = false;
}
}
}

View File

@ -7,8 +7,13 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct FloatOptions
public struct FloatOptions : IPlugOptions
{
public bool snapping;
public void Reset()
{
snapping = false;
}
}
}

View File

@ -0,0 +1,15 @@
// Author: Daniele Giardini - http://www.demigiant.com
// Created: 2016/12/08 11:50
// License Copyright (c) Daniele Giardini
// This work is subject to the terms at http://dotween.demigiant.com/license.php
namespace DG.Tweening.Plugins.Options
{
/// <summary>
/// Base interface for all tween plugins options
/// </summary>
public interface IPlugOptions
{
/// <summary>Resets the plugin</summary>
void Reset();
}
}

View File

@ -7,5 +7,8 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct NoOptions {}
public struct NoOptions : IPlugOptions
{
public void Reset() {}
}
}

View File

@ -17,7 +17,7 @@ namespace DG.Tweening.Plugins.Options
LookAtPosition
}
public struct PathOptions
public struct PathOptions : IPlugOptions
{
public PathMode mode;
public OrientType orientType;
@ -32,6 +32,23 @@ namespace DG.Tweening.Plugins.Options
public Transform parent; // Only used with OrientType.ToPath and useLocalPosition set as TRUE
internal Quaternion startupRot; // Used to reset orientation when rewinding
internal float startupZRot; // Used to store Z value in case of lock Z, in order to rotate things differently
internal float startupZRot; // Used to store Z value in case of lock Z, in order to rotate things differently
public void Reset()
{
mode = PathMode.Ignore;
orientType = OrientType.None;
lockPositionAxis = lockRotationAxis = AxisConstraint.None;
isClosedPath = false;
lookAtPosition = Vector3.zero;
lookAtTransform = null;
lookAhead = 0;
hasCustomForwardDirection = false;
forward = Quaternion.identity;
useLocalPosition = false;
parent = null;
startupRot = Quaternion.identity;
startupZRot = 0;
}
}
}

View File

@ -9,10 +9,17 @@ using UnityEngine;
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct QuaternionOptions
public struct QuaternionOptions : IPlugOptions
{
internal RotateMode rotateMode;
internal AxisConstraint axisConstraint; // Used by SpecialStartupMode SetLookAt
internal Vector3 up; // Used by SpecialStartupMode SetLookAt
public void Reset()
{
rotateMode = RotateMode.Fast;
axisConstraint = AxisConstraint.None;
up = Vector3.zero;
}
}
}

View File

@ -7,8 +7,13 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct RectOptions
public struct RectOptions : IPlugOptions
{
public bool snapping;
public void Reset()
{
snapping = false;
}
}
}

View File

@ -7,7 +7,7 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct StringOptions
public struct StringOptions : IPlugOptions
{
public bool richTextEnabled;
public ScrambleMode scrambleMode;
@ -15,5 +15,13 @@ namespace DG.Tweening.Plugins.Options
// Stored by StringPlugin
internal int startValueStrippedLength, changeValueStrippedLength; // No-tag lengths of start and change value
public void Reset()
{
richTextEnabled = false;
scrambleMode = ScrambleMode.None;
scrambledChars = null;
startValueStrippedLength = changeValueStrippedLength = 0;
}
}
}

View File

@ -6,8 +6,13 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct UintOptions
public struct UintOptions : IPlugOptions
{
public bool isNegativeChangeValue; // Necessary because uints can't obviously be negative
public void Reset()
{
isNegativeChangeValue = false;
}
}
}

View File

@ -7,11 +7,18 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct Vector3ArrayOptions
public struct Vector3ArrayOptions : IPlugOptions
{
public AxisConstraint axisConstraint;
public bool snapping;
internal float[] durations; // Duration of each segment
public void Reset()
{
axisConstraint = AxisConstraint.None;
snapping = false;
durations = null;
}
}
}

View File

@ -7,9 +7,15 @@
#pragma warning disable 1591
namespace DG.Tweening.Plugins.Options
{
public struct VectorOptions
public struct VectorOptions : IPlugOptions
{
public AxisConstraint axisConstraint;
public bool snapping;
public void Reset()
{
axisConstraint = AxisConstraint.None;
snapping = false;
}
}
}

View File

@ -69,7 +69,7 @@ namespace DG.Tweening
internal static bool Setup<T1, T2, TPlugOptions>(
TweenerCore<T1, T2, TPlugOptions> t, DOGetter<T1> getter, DOSetter<T1> setter, T2 endValue, float duration, ABSTweenPlugin<T1, T2, TPlugOptions> plugin = null
)
where TPlugOptions : struct
where TPlugOptions : struct, IPlugOptions
{
if (plugin != null) t.tweenPlugin = plugin;
else {
@ -99,7 +99,7 @@ namespace DG.Tweening
// CALLED BY TweenerCore
// Returns the elapsed time minus delay in case of success,
// -1 if there are missing references and the tween needs to be killed
internal static float DoUpdateDelay<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t, float elapsed) where TPlugOptions : struct
internal static float DoUpdateDelay<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t, float elapsed) where TPlugOptions : struct, IPlugOptions
{
float tweenDelay = t.delay;
if (elapsed > tweenDelay) {
@ -116,7 +116,7 @@ namespace DG.Tweening
// (unless it's a FROM tween, in which case it will be called BEFORE any eventual delay).
// Returns TRUE in case of success,
// FALSE if there are missing references and the tween needs to be killed
internal static bool DoStartup<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t) where TPlugOptions : struct
internal static bool DoStartup<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t) where TPlugOptions : struct, IPlugOptions
{
t.startupDone = true;
@ -152,7 +152,7 @@ namespace DG.Tweening
// CALLED BY TweenerCore
internal static Tweener DoChangeStartValue<T1, T2, TPlugOptions>(
TweenerCore<T1, T2, TPlugOptions> t, T2 newStartValue, float newDuration
) where TPlugOptions : struct
) where TPlugOptions : struct, IPlugOptions
{
t.hasManuallySetStartValue = true;
t.startValue = newStartValue;
@ -178,7 +178,7 @@ namespace DG.Tweening
// CALLED BY TweenerCore
internal static Tweener DoChangeEndValue<T1, T2, TPlugOptions>(
TweenerCore<T1, T2, TPlugOptions> t, T2 newEndValue, float newDuration, bool snapStartValue
) where TPlugOptions : struct
) where TPlugOptions : struct, IPlugOptions
{
t.endValue = newEndValue;
t.isRelative = false;
@ -215,7 +215,7 @@ namespace DG.Tweening
internal static Tweener DoChangeValues<T1, T2, TPlugOptions>(
TweenerCore<T1, T2, TPlugOptions> t, T2 newStartValue, T2 newEndValue, float newDuration
) where TPlugOptions : struct
) where TPlugOptions : struct, IPlugOptions
{
t.hasManuallySetStartValue = true;
t.isRelative = t.isFrom = false;
@ -243,7 +243,7 @@ namespace DG.Tweening
// Commands shared by DOStartup/ChangeStart/End/Values if the tween has already started up
// and thus some settings needs to be reapplied.
// Returns TRUE in case of SUCCESS, FALSE if there were managed errors
static bool DOStartupSpecials<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t) where TPlugOptions : struct
static bool DOStartupSpecials<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t) where TPlugOptions : struct, IPlugOptions
{
try {
switch (t.specialStartupMode) {
@ -262,11 +262,11 @@ namespace DG.Tweening
}
return true;
} catch {
// Erro in SpecialPluginUtils (usually due to target being destroyed)
// Error in SpecialPluginUtils (usually due to target being destroyed)
return false;
}
}
static void DOStartupDurationBased<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t) where TPlugOptions : struct
static void DOStartupDurationBased<T1, T2, TPlugOptions>(TweenerCore<T1, T2, TPlugOptions> t) where TPlugOptions : struct, IPlugOptions
{
if (t.isSpeedBased) t.duration = t.tweenPlugin.GetSpeedBasedDuration(t.plugOptions, t.duration, t.changeValue);
t.fullDuration = t.loops > -1 ? t.duration * t.loops : Mathf.Infinity;

View File

@ -891,6 +891,14 @@
<param name="perc">The percentage (0 to 1) at which to get the point</param>
<param name="convertToConstantPerc">If TRUE constant speed is taken into account, otherwise not</param>
</member>
<member name="T:DG.Tweening.Plugins.Options.IPlugOptions">
<summary>
Base interface for all tween plugins options
</summary>
</member>
<member name="M:DG.Tweening.Plugins.Options.IPlugOptions.Reset">
<summary>Resets the plugin</summary>
</member>
<member name="T:DG.Tweening.Plugins.Vector3ArrayPlugin">
<summary>
This plugin generates some GC allocations at startup

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.