UnityScript相当于这个C#片段

时间:2011-12-16 00:03:29

标签: c# ios unity3d unityscript prime31

在UnityScript中会是什么样子?

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

玩家是一个列表而非数组。

我正在尝试处理来自Apple GameCenter的事件响应,它返回一个电镀属性列表。这些是错误:

  

“长度不是对象播放器的成员”

     

“Type'Object'(播放器)不支持切片”

使用iOS Game Center,Unity和prime31 GameCenter插件。

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

这将是:

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

答案 2 :(得分:0)

使用.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
    }
}

答案 3 :(得分:0)

虽然C#的列表&lt;&gt;与下面的内容不同,就像这样

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()

答案 4 :(得分:-2)

我相信它是这样的:

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

可能会有误,是抱怨吗?