cube1.name = string.Format("Terrain_{0}_{1}",
(int)Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x,
(int)Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y);
试图将其投放到int
,但它并没有太大变化。
每个多维数据集的名称仍然是例如:Terrain_6.5_5.5
。但我希望名称为Terrain_6_5
。
tilePositionInLocalSpace
为vector3
类型且x
均为float
类型。
答案 0 :(得分:16)
你刚才有一个优先问题。你有:
(int) x + y
相当于
((int) x) + y
...然后会将强制转换的结果提升回浮点数以执行浮点加法。只需将转换应用于整个结果:
(int) (x + y)
当然,x
和y
是原始代码中相当长的表达式。为了便于阅读,我将提取两个值来分隔局部变量,因此您需要:
int sumX = (int) (Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x);
int sumY = (int) (Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y);
cube1.name = string.Format("Terrain_{0}_{1}", sumX, sumY);
或者更好的是:
var position = Terrain.activeTerrain.transform.position;
int sumX = (int) (position.x + tilePositionInLocalSpace.x);
int sumY = (int) (position.z + tilePositionInLocalSpace.y);
cube1.name = string.Format("Terrain_{0}_{1}", sumX, sumY);
答案 1 :(得分:2)
您需要将int
转换为int
,因为现在您要添加float
float
,当然这会导致cube1.name = string.Format("Terrain_{0}_{1}", (int)(Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x), (int)(Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y))
。尝试:
<a ng-repeat="friend in chat.friendlist" ng-click="loadChat('{{friend.friend_username}}')" data-toggle="modal" data-target="#chat" data-dismiss="modal" class="list-group-item p-x-md">
<i class="fa fa-circle text-success text-xs m-r-xs"></i>
<span>{{friend.friend_username}} - {{friend.gamename}}</span>
</a>
答案 2 :(得分:1)
只需添加添加结果:
(int) (x + y);
然后是string.format()...
答案 3 :(得分:1)
我只需指定格式化的位数。可以通过输入格式来完成。
像这样:
// here | |
cube1.name = string.Format("Terrain_{0:0}_{1:0}",
Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x,
Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y);
答案 4 :(得分:-1)
您需要明确转换它:
cube1.name = string.Format("Terrain_{0}_{1}",
Convert.ToInt32(Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x),
Convert.ToInt32(Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y));