UnityScript相当于这个c#代码片段

本文关键字:代码 片段 相当于 UnityScript | 更新日期: 2023-09-27 18:18:13

这在UnityScript中会是什么样子?

void playerDataLoaded( List< GameCenterPlayer > players )
{
    foreach( GameCenterPlayer p in players )
    // do something with p
}

Players是一个列表而不是数组。

我试图处理一个事件响应从苹果GameCenter返回一个列表的播放器属性。以下是错误:

length不是对象player"

的成员

类型"对象"(播放器)不支持切片

使用iOS GameCenter, Unity和prime31 GameCenter插件

UnityScript相当于这个c#代码片段

function removePlayersDataLoaded(players){
  for(var i = 0; i < players.length; i++){
    var p = players[i];
    // do something with p
  }
}

应该是这样的:

var remotePlayersDataLoaded = function(players) {
  for (var i = 0; i < players.length; i++) {
    var player = players[i];
  }
};

使用.forEach为列表中的每个成员调用迭代器

function removePlayersDataLoaded(players) {
    players.forEach(doSomethingWithPlayer);
    function doSomethingWithPlayer(p) {
        // code
    }
}

或者在数组

上使用for循环
function removePlayersDataLoaded(players) {
    for (var i = 0, len = players.length; i < len; i++) {
        var p = players[i];
        // code
    }
}

虽然c#的List<>与下面的不完全相同,但像这样的

function Player(playerName, gameCenterPlayer)
{
   this.playerName = playerName;
   this.GameCenterPlayer = gameCenterPlayer; // i'm going to assume this has been calculated elsewhere, and all we need now is a boolean.
}
function playerDataLoaded(players) {
  this.players = players
  this.getCenterPlayer = function() {
var i = 0
  while (i < players.length) {
  if (players[i].GameCenterPlayer) {
console.log(players[i].playerName + " is the center player!");
}
i++;
}
}
}
var player1 = new Player("Ted", 0);
var player2 = new Player("Jane", 1);
var player3 = new Player("Doug", 0);
Players = new playerDataLoaded([player1, player2, player3]);
Players.getCenterPlayer()

我想大概是这样的:

function remotePlayersDataLoaded(players) {
for (p in players)
  {
  // do your thing with p
  } 
}

可能是错误的,但是,它抱怨吗?