Question by
ShortQuestion · Sep 06, 2018 at 11:48 AM ·
c#uitextcanvasrecttransform
Updated with UI text element not correct (same frame)
I have some code to set a custom with for a text element and then the height gets adjusted automatically like so:
Title.SetTextContainerSize("autoH", 330);
public void SetTextContainerSize(string fit, float dimension = 0)
{
if (UIElement.GetComponent<ContentSizeFitter>() == null)
UISizeFitterComponent = UIElement.AddComponent(typeof(ContentSizeFitter)) as ContentSizeFitter;
if (fit == "autoWH")
{
UISizeFitterComponent.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
UISizeFitterComponent.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
base.SetDimensions( RectOptions.sizeDelta);
}
else if (fit == "autoW")
{
UISizeFitterComponent.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
base.SetDimensions( new Vector2(RectOptions.rect.width, dimension));
}
else if (fit == "autoH")
{ //here is a problem..
UISizeFitterComponent.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
Canvas.ForceUpdateCanvases();
Debug.Log(new Vector2(dimension, RectOptions.rect.height));
base.SetDimensions( new Vector2(dimension, RectOptions.rect.height));
}
}
Edit Added the set dimensions function
public void SetDimensions(Vector2 dimensions)
{
RectOptions.sizeDelta = dimensions;
Dimensions = dimensions; //this just sets the value of a property.
}
but when i call the function to update the text i get the following result from the Debug.Log()
it says the width = 330 and the height is 2077. In this case the width is correct but the height is not even close.
See here a screenshot of the actual element and its correct height.
what am i doing wrong when updating the actual height of the element?
If there is an completely other way to do it i am also open for complete rewrites of the current function if nessesary :)
IF something is unclear let me know so i can clarify!
capture.png
(9.7 kB)
Comment