コード例 #1
0
    public static float ToKelvin(float t, TemeratureUnits from)
    {
        switch (from)
        {
        case TemeratureUnits.C:
            return(t + ZERO_CELSIUS_IN_KELVIN);

        case TemeratureUnits.K:
            return(t);

        case TemeratureUnits.F:
            return((t + 459.67f) * (5f / 9f));
        }

        return(t);
    }
コード例 #2
0
    public static float FromKelvin(float t, TemeratureUnits to)
    {
        switch (to)
        {
        case TemeratureUnits.C:
            return(t - ZERO_CELSIUS_IN_KELVIN);

        case TemeratureUnits.K:
            return(t);

        case TemeratureUnits.F:
            return(t * (9f / 5f) - 459.67f);
        }

        return(t);
    }
コード例 #3
0
    public static float Transform(float t, TemeratureUnits from, TemeratureUnits to)
    {
        if (from == to)
        {
            return(t);
        }

        float ret = t;

        // transform to kelvins
        if (from != TemeratureUnits.K)
        {
            ret = ToKelvin(t, from);
        }

        // now transform to desired
        if (to != TemeratureUnits.K)
        {
            ret = FromKelvin(t, to);
        }

        return(ret);
    }
コード例 #4
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var type = property.propertyType;

        if (type != SerializedPropertyType.Float)
        {
            EditorGUI.PropertyField(position, property, label);
            return;
        }

        var enumRect = new Rect(position.x + position.width - SelectorSize, position.y,
                                SelectorSize, position.height);

        selectedUnit = (TemeratureUnits)EditorGUI.EnumPopup(enumRect, selectedUnit);

        var tempK = property.floatValue;

        if (tempK < 0)
        {
            // you can't get lower than absolute zero
            tempK = 0f;
        }

        var temp = TemperatureUtils.FromKelvin(tempK, selectedUnit);

        var propRect = new Rect(position.x, position.y,
                                position.width - SelectorSize, position.height);

        var newTemp  = EditorGUI.FloatField(propRect, label, temp);
        var newTempK = TemperatureUtils.ToKelvin(newTemp, selectedUnit);

        if (Mathf.Abs(newTempK - tempK) > 0.001f)
        {
            // temperature has changed - update it
            property.floatValue = TemperatureUtils.ToKelvin(newTemp, selectedUnit);
        }
    }