统一调试日志满浮动

本文关键字:满浮动 日志 调试 | 更新日期: 2023-09-27 18:09:25

我很困惑,这是我的c sharp脚本,创建和记录浮动:

using UnityEngine;
using System.Collections;
public class guicontrol : MonoBehaviour {
    void CalculatePosition(string ObjectName) {
        GameObject theobject = GameObject.Find (ObjectName);
        int selectedwidth = 1366;
        int selectedheight = 768;
        int screenwidth = Screen.width;
        int screenheight = Screen.height;
        float coordinatey = theobject.transform.localPosition.y;
        float coordinatex = theobject.transform.localPosition.x;
        float coordinatez = theobject.transform.localPosition.z;
        Debug.Log ("work at current stats : " + screenwidth + " " + screenheight);
        if (screenwidth != selectedwidth || screenheight != selectedheight) {
            float coordinateytomove = (screenheight / selectedheight) * coordinatey;
            float coordinatextomove = (screenwidth / selectedwidth) * coordinatex;
            Debug.Log (coordinateytomove + " " + coordinatextomove);
        }
    }
}

在计算器中手工计算,coordinatextomove的结果为:

-2.99853587116

,但在调试日志中是:

-3 0

-3是不改变当前坐标的coordinateytomove,但为什么如果coordinatextomove改变了,它的调试记录的是0而不是-2.99853587116 ?如果有之前的帖子回答了这个问题,请原谅我并给我帖子的链接:)。

统一调试日志满浮动

screenheight / selectedheight将在得到结果后给您整数除法,忽略.右侧的任何内容。在除

之前,需要将其中一个强制转换为浮点数
float coordinateytomove = ((float)screenheight / selectedheight) * coordinatey;
float coordinatextomove = ((float)screenwidth / selectedwidth) * coordinatex;

您也可以将变量声明为float,而不需要稍后进行强制转换。

int selectedwidth = 1366;
int selectedheight = 768;
float screenwidth = Screen.width;
float screenheight = Screen.height;