Introduced in Better UI 2.1


The static sizer methods enable you to use your own math to calculate size changes.

You can specify up to five methods.


Part 1: Code


To create such a method, first create a class with a static method inside with a specific declaration:




using UnityEngine;


namespace MyNamespace

{

    public static class MyClass

    {

        [Preserve] // <- makes sure the method is not stripped away

        public static float MyStaticMethod(Component caller,

                                Vector2 optimizedResolution, Vector2 actualResolution,

                                float optimizedDpi, float actualDpi)

        {

            return 1; // return the calculated value instead

        }

    }

}




Parameters:

  • caller: The better element which is calling the method for a resize. You can use it to find out what component it is or if there are other components of interest attached to the object.
  • optimizedResolution: The resolution which is used as a reference for calculation (usually coming from a size configuration).
  • actualResolution: The actual resolution of the screen at the moment.
  • optimizedDpi: The dpi value which is used as a reference for calculation (usually coming from a size configuration).
  • actualDpi: The actual dpi of the current screen.

Return value:

The returned value is like a scale factor. a value of 0 resizes to zero, a value of 1 keeps the optimized size, a value of 2 doubles the optimized size, etc.


Part 2: Register


To make the method available, open the "Static Sizer Method" section of the Resolution Monitor and enter the data in the fields of one of the method sections.

  • Assembly Name: By default this is "Assembly-CSharp". If you put your class in another assembly (via assembly definition file), you should change the name accordingly.
  • Type Name: The namespace and class name of the class containing the method. Put a dot between namespace and class name.
  • Method Name: The name of the method which contains your custom calculation.



Don't forget to press the "Save" button afterwards.


Part 3: Use

You can use it then in any size modifier. Simply expand the "Size Modification" section and change the size modification to "Static Method X" (where X is the number of the method you specified).



Troubleshooting

If you see errors in the console you have to fix something. Possible problems:

  • Did you enter something wrong in Part 2?
    • Check the assembly name in your code editor (it is the name of the project in the solution).
    • make sure the type name contains the namespace (if any) and the class name (and possible sub-classes) all concatinated with a dot like:
      • namespace example: MyNamespace.MyClass
      • no namespace example: MyClass
      • namespace and sub-class example: MyNamespace.MyClass.MySubClass
    • Did you misspelled the method name?
  • Is the method declaration correct?
    • Does it start with public static float?
    • Are all five parameters present with correct types in the right order?
  • If it works in Unity but not in a build:
    • make sure the class is not in an Editor folder / Editor assembly and the method is outside of a #if UNITY_EDITOR section.
    • If you have stripping enabled in your build settings, make sure you have marked your static methods with Preserve Attributes.