从通用字典

时间:2015-11-23 23:53:24

标签: generics dictionary unity3d floating-point

我在Unity(uJS)中使用通用字典来存储字符串[key]和float [value]。但是我似乎无法通过传递字符串键来访问浮点值。

我确信浮点值存在于字典中,因为它们在使用键值对迭代时正确返回,所以我必须尝试访问错误的值;任何人都可以告诉我这个问题可能发生在哪里? 尝试过参考文档中提供的大量方法无济于事。

非常感谢,

赖安

功能:

function findXOrigin(numberOfGroupings : int, groupings : List.<String>)
{
	var floorPlaneCol = floorPlane.GetComponent(BoxCollider);	
	var floorSizeX = floorPlaneCol.size.x;
	Debug.Log("floorSizeX: " + floorSizeX);
	
	var floorSubdivisionSize = floorSizeX / numberOfGroupings;
	Debug.Log("floorSubdivisionSize: " + floorSubdivisionSize);
	
	groupingsOrigin = new Dictionary.<String, float>();
	
	for (var group : String in groupings)
	{	
		var subDivLength = floorSizeX - floorSubdivisionSize;
		floorSizeX -= floorSubdivisionSize; //removes accounted for section from the overall size
		var subDivXOrigin = subDivLength + (floorSubdivisionSize/2);
		groupingsOrigin.Add(group, subDivXOrigin);
	}

正确调试:

for (var group : KeyValuePair.<String, float> in groupingsOrigin)
{
    var groupOrigin = groupingsOrigin.Values;
    Debug.Log("Dictionary entry= " + " Group Name: " +group.Key + " " + " Group X Origin: " + group.Value);

}

调试错误:(也试用了带TryGetValue等)

    for (var group : String in groupingsOrigin.Keys)
{
    var groupOriginX = groupingsOrigin[group];
    //var groupOriginX : float;
    //groupingsOrigin.TryGetValue(group, groupOriginX);
    Debug.Log("Dictionary entry= " + " Group Name: " + group + " " + " Group X Origin: " + groupOriginX.ToString);
}

错误返回:

而不是像第一种方法那样返回Dictionary entry= Group Name: Home Scan Group X Origin: 8.333334,第二种方法返回Dictionary entry= Group Name: Home Scan Group X Origin: CompilerGenerated.__BrowseImpSortPositionObj_findXOrigin$callable1$120_117__ UnityEngine.Debug:Log(Object)

1 个答案:

答案 0 :(得分:0)

只需使用groupOriginX.ToString()代替groupOriginX.ToString。或者只是groupOriginX

如果使用不带括号的ToString,则引用函数ToString,而不是调用它。这就是你得到的 - ToStringCompilerGenerated.__BrowseImpSortPositionObj_findXOrigin$callable1$12...

类型的对象