【问题标题】:'Implicit downcast from object to int' warning when returning an Array返回数组时“从对象隐式向下转换为 int”警告
【发布时间】:2015-10-16 03:07:47
【问题描述】:

所以我在 GameMain.js 中有一个函数:

function getTarget( ... ){
   ...
   return [targetX, targetY, objectBehaviour, moveX, moveY];
}

如您所见,我返回一个数组。数组中的变量在函数中声明,以及它们的类型。然后我想在另一个脚本中使用该数组:

var targetInfo = gameMain.getTarget( ... );
target = new Vector2(targetInfo[0], targetInfo[1]);
objectBehaviour = targetInfo[2];
moveX = targetInfo[3];
moveY = targetInfo[4];

这里使用的变量也是之前用类型声明的。 一切正常,但最后 3 行给出了这个警告: "从 'Object' 隐式向下转换为 'int'

我想知道为什么我会收到这个警告,因为一切都被声明为 int 并且没有任何东西应该是 Object。请注意,带有 vector2 的最后 4 行没有给出警告。

【问题讨论】:

  • 为什么警告会困扰您?可能您从函数 getTarget() 返回的 array 被认为是 Object 的数组,因为您将它用作 var targetInfo 并且在将其分配给某些 int 变量时不会对其值进行类型转换,因此它会隐式地对其进行类型转换我猜这不是问题。
  • 正如你所说,这不是一个真正的问题,但我只是觉得它们很烦人。在 unityscript 中,我无法隐藏特定的警告。仍然没有找到警告的解决方案,所以我将忽略它们。
  • 如果你想避免这个警告。只需在分配之前将您的对象类型转换为int。您可以在控制台窗口中将警告设置为禁用
  • 你能给出一个简短的代码示例吗?我搜索了一下,但没有找到如何将对象类型转换为 int... 尝试了 as intint() 但都给出了错误
  • This 可能会有帮助

标签: arrays unity3d unityscript


【解决方案1】:

好的,所以我终于找到了as int[]thanks to Venkats 评论的解决方案。 事实上,我对objectBehaviour 撒了谎以简化问题。这不是int,而是ObjectBehaviour(我创建的一个枚举)。 我制作了两个数组,将它们的类型转换为锯齿状数组,如下所示:

return [[targetX, targetY, moveX, moveY], [objectBehaviour]];

在 GameMain.js 和其他脚本中:

var targetInfo = gameMain.getTarget( ... );
var targetInfo1 = targetInfo[0] as int[];
var targetInfo2 = targetInfo[1] as ObjectBehaviour[];
target = new Vector2(targetInfo1[0], targetInfo1[1]);
moveX = targetInfo1[2];
moveY = targetInfo1[3];
objectBehaviour = targetInfo2[0];

Ty Umair 和 Venkat 获取信息!

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 2020-07-02
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多