iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎮

Fixing the 'time parameter' Error in the 'UI Gradient' Unity Asset

に公開

https://assetstore.unity.com/packages/tools/gui/ui-gradient-133992

In the standard asset UI Gradient sold on the Asset Store, under certain conditions, the following error occurs:

Evaluate: time parameter is not a valid number

I have investigated the conditions under which this occurs and how to avoid it.

Conditions of Occurrence

  • The width or height of the RectTransform where UIGradient is applied is 0.
  • The rotation angle of the gradient matches the direction where the RectTransform size is 0.
  • GradientType is set to ComplexLinear.

This error occurs when all the above conditions are met.
Now, let's look at the code.

UIGradient.cs
#if UNITY_2018_1_OR_NEWER
    Vector2 normalizedPosition = ((Vector2)vert.position - rectTransform.rect.min) / (rectTransform.rect.max - rectTransform.rect.min);
#else

First, at line 230 of UIGradient.cs, if the width of the RectTransform is 0, for example:

rectTransform.rect.max - rectTransform.rect.min

the x component becomes 0. Division by zero occurs during the Vector2 division, and NaN is assigned to normalizedPosition.x.

UIGradient.cs
normalizedPosition = RotateNormalizedPosition(normalizedPosition, this.angle);

Next, at line 236, normalizedPosition is rotated to match the direction of the gradient.
At this point, if the gradient angle is set to 90 degrees, for instance, the NaN in normalizedPosition moves to the y component.

UIGradient.cs
else if (gradientType == UIGradientType.ComplexLinear)
{
    gradientColor = linearGradient.Evaluate(normalizedPosition.y);
}

At line 250, linearGradient.Evaluate is called on normalizedPosition.y. Since normalizedPosition.y has become NaN due to the previous rotation, attempting to call Evaluate results in the following error:

Evaluate: time parameter is not a valid number

This is the mechanism behind how the error is triggered.

Solutions

There seem to be two ways to handle this: through operational workarounds or by modifying the code.

Operational Workarounds

If you cannot change the code for some reason, it seems you can cover it operationally by keeping the following points in mind:

  • Ensure that the width or height of the RectTransform does not become 0.
  • If you are using Stretch (the blue arrows) in the RectTransform, pay attention to the parent's size.
  • If you are scaling it via animation, etc., be careful to prevent the size from reaching 0.

Modifying the Code

Check for NaN on x and y before RotateNormalizedPosition at line 236, and assign 0f if the value is NaN.

UIGradient.cs
#if UNITY_2018_1_OR_NEWER
    Vector2 normalizedPosition = ((Vector2)vert.position - rectTransform.rect.min) / (rectTransform.rect.max - rectTransform.rect.min);
#else
    Vector2 size = rectTransform.rect.max - rectTransform.rect.min;
    Vector2 normalizedPosition = Vector2.Scale((Vector2)vert.position - rectTransform.rect.min, new Vector2(1f / size.x, 1f / size.y));
#endif

+   normalizedPosition.x = float.IsNaN(normalizedPosition.x) ? 0f : normalizedPosition.x;
+   normalizedPosition.y = float.IsNaN(normalizedPosition.y) ? 0f : normalizedPosition.y;
    normalizedPosition = RotateNormalizedPosition(normalizedPosition, this.angle);

That's all.
While it would normally be more appropriate to check the values before division by zero occurs, I chose this approach because keeping the modifications to a minimum makes it easier to re-apply the fix if the asset is updated in the future.
If you want to be more thorough, you would likely need to cache the rectTransform values and validate them before declaring normalizedPosition, or create the normalizedPosition using those cached values.

I hope this helps anyone struggling with the same error.

Discussion