Search Unity

Question What is the proper code to replace element.style.unityBackgroundScaleMode

Discussion in 'UI Toolkit' started by jiungerich, Mar 22, 2023.

  1. jiungerich

    jiungerich

    Joined:
    Jul 26, 2019
    Posts:
    18
    I get warning when using element.style.unityBackgroundScaleMode

    IStyle.unityBackgroundScaleMode' is obsolete: 'unityBackgroundScaleMode is deprecated. Use background-* properties instead.'

    but I cannot find references to background-* properties in the manual. There is no link from the obsolete style to something new just that is obsolete. I want to set the Scale Mode of a Visual Element Background to Scale-to-fit.

    Previously I had
    Code (CSharp):
    1. imageButton.style.unityBackgroundScaleMode = new StyleEnum<ScaleMode>(ScaleMode.ScaleToFit);
     
    WidmerNoel likes this.
  2. ncerone_unity

    ncerone_unity

    Unity Technologies

    Joined:
    Jan 13, 2022
    Posts:
    36
    Hi,

    The way to acheive the ScaleToFit using the background-* properties is:

    imageButton.style.backgroundPositionX = new BackgroundPosition(BackgroundPositionKeyword.Center);
    imageButton.style.backgroundPositionY = new BackgroundPosition(BackgroundPositionKeyword.Center);
    imageButton.style.backgroundRepeat = new BackgroundRepeat(Repeat.NoRepeat, Repeat.NoRepeat);
    imageButton.style.backgroundSize = new BackgroundSize(BackgroundSizeType.Contain);
     
  3. jiungerich

    jiungerich

    Joined:
    Jul 26, 2019
    Posts:
    18
    Wow - no wonder I could not find it. I would have never thought I would need to replace 1 line of code with 4 to do the same thing.
     
  4. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    982
    Welcome to the wonderful world of Web development, now for desktop! :)

    Next thing you know people will start using terrible implementations of javascript that require multiple preprocessors and transpilers to run servers and desktop apps... ... ...
     
  5. jasursadikov

    jasursadikov

    Joined:
    Feb 25, 2020
    Posts:
    17
    @ncerone_unity what would be a replacement for the whole thing? The function was marked as obsolete but the alternative is not provided