Namespace: | Sirenix.Utilities.Editor |
Assembly: | Sirenix.Utilities.Editor |
public static class UnitNumberUtility
public static void AddCustomUnit(string name, string[] symbols, UnitCategory unitCategory, decimal multiplier)
System.String | name | The name of the unit. Duplicate names are not allowed. |
System.String[] | symbols | Symbols used for the unit. First value in the array will be used as the primary symbol. Atleast 1 value required. Duplicate symbols are not allowed within the same category. |
UnitCategory | unitCategory | The category of the unit. Units can only be converted to another of the same category. Custom categories are allowed. |
System.Decimal | multiplier | The multiplier to convert the unit from the base value. For example, meters are the base unit of the distance category, therefore centimeters have a multipler of 100. |
public static void AddCustomUnit(string name, string[] symbols, UnitCategory unitCategory, Func<decimal, decimal> convertToBase, Func<decimal, decimal> convertFromBase)
System.String | name | The name of the unit. Duplicate names are not allowed. |
System.String[] | symbols | Symbols used for the unit. First value in the array will be used as the primary symbol. Atleast 1 value required. Duplicate symbols are not allowed within the same category. |
UnitCategory | unitCategory | The category of the unit. Units can only be converted to another of the same category. Custom categories are allowed. |
System.Func<System.Decimal, System.Decimal> | convertToBase | Method for converting a given value of the custom unit to the base unit. For example, for centimeter, use: |
System.Func<System.Decimal, System.Decimal> | convertFromBase | Method for converting a given value of the base unit to the custom unit. For example, for centimeter, use: |
public static void AddCustomUnit(string name, string[] symbols, string unitCategory, decimal multiplier)
System.String | name | The name of the unit. Duplicate names are not allowed. |
System.String[] | symbols | Symbols used for the unit. First value in the array will be used as the primary symbol. Atleast 1 value required. Duplicate symbols are not allowed within the same category. |
System.String | unitCategory | The category of the unit. Units can only be converted to another of the same category. Custom categories are allowed. |
System.Decimal | multiplier | The multiplier to convert the unit from the base value. For example, meters are the base unit of the distance category, therefore centimeters have a multipler of 100. |
public static void AddCustomUnit(string name, string[] symbols, string unitCategory, Func<decimal, decimal> convertToBase, Func<decimal, decimal> convertFromBase)
System.String | name | The name of the unit. Duplicate names are not allowed. |
System.String[] | symbols | Symbols used for the unit. First value in the array will be used as the primary symbol. Atleast 1 value required. Duplicate symbols are not allowed within the same category. |
System.String | unitCategory | The category of the unit. Units can only be converted to another of the same category. Custom categories are allowed. |
System.Func<System.Decimal, System.Decimal> | convertToBase | Method for converting a given value of the custom unit to the base unit. For example, for centimeter, use: |
System.Func<System.Decimal, System.Decimal> | convertFromBase | Method for converting a given value of the base unit to the custom unit. For example, for centimeter, use: |
a
and b
units.
a
and b
units.
public static decimal ConvertUnitFromTo(decimal value, Units from, Units to)
System.Decimal | value | The value to convert. Should be in the |
Units | from | The unit to convert the value from. |
Units | to | To unit to convert the value to. Must be the same category as |
System.Decimal | The |
System.Exception | Throws when either 'from' or 'to' units are invalid, or when the units are of different categories. |
public static decimal ConvertUnitFromTo(decimal value, UnitInfo fromUnitInfo, UnitInfo toUnitInfo)
System.Decimal | value | The value to convert. Should be in the |
UnitInfo | fromUnitInfo | The unit to convert the value from. |
UnitInfo | toUnitInfo | To unit to convert the value to. Must be the same category as |
System.Decimal | The |
System.Exception | Throws when either 'fromUnitInfo' or 'toUnitInfo' units are invalid, or when the units are of different categories. |
public static IEnumerable<UnitInfo> GetAllUnitInfos()
System.Collections.Generic.IEnumerable<UnitInfo> | Enumerable of both built-in and custom units. |
public static UnitInfo GetUnitInfoByName(string unitName)
System.String | unitName | The name of the unit. |
UnitInfo | UnitInfo for the name. |
System.Exception | Throws when no unit with the given name is found. |
public static UnitInfo MatchUnitInfoBySymbol(string symbol, string unitCategory)
System.String | symbol | The symbol to find a unit for. |
System.String | unitCategory | The category to look for units within. |
UnitInfo | The UnitInfo that best matches the given symbol. |
System.Exception | Throws when no match was found. |
public static bool TryConvertUnitFromTo(decimal value, Units from, Units to, out decimal converted)
System.Decimal | value | The value to convert. Should be in the |
Units | from | The unit to convert the value from. |
Units | to | To unit to convert the value to. Must be the same category as |
System.Decimal | converted | The |
System.Boolean |
|
public static bool TryConvertUnitFromTo(decimal value, UnitInfo fromUnitInfo, UnitInfo toUnitInfo, out decimal converted)
System.Decimal | value | The value to convert. Should be in the |
UnitInfo | fromUnitInfo | The unit to convert the value from. |
UnitInfo | toUnitInfo | To unit to convert the value to. Must be the same category as |
System.Decimal | converted | The |
System.Boolean |
|
System.ArgumentNullException | Throws if either fromUnitInfo or toUnitInfo is null. |
public static bool TryGetUnitInfoByName(string unitName, out UnitInfo unitInfo)
System.String | unitName | The name of the unit. |
UnitInfo | unitInfo | The UnitInfo matching the given name. |
System.Boolean |
|
public static bool TryMatchUnitInfoBySymbol(string symbol, string unitCategory, out UnitInfo unitInfo)
System.String | symbol | The symbol to find a unit for. |
System.String | unitCategory | The category to look for units within. |
UnitInfo | unitInfo | The UnitInfo that best matches the given symbol. |
System.Boolean |
|