创建不使用构造函数的播放器对象

时间:2017-10-14 21:42:04

标签: c# unity3d constructor

需要一些专业知识。我目前正以一种创建对象的方式从类中生成一个玩家,然后逐行定义变量,如下所示:

Player p = new Player();
p.Avatar = go;
p.PlayerName = playerName;
p.ConnectionId = cnnId;
p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
players.Add(cnnId, p);

并且玩家类的结构很简单:

public class Player
{
    public string PlayerName;
    public GameObject Avatar;
    public int ConnectionId;
}

这样有用,但我想扩展参数并使用构造函数来创建我的玩家对象,我尝试通过这样创建我的对象:

Player p = new Player(playerName, go, cnnId); p.Avatar.GetComponentInChildren<TextMesh>().text = pName; players.Add(cnnId, p);

然后我像这样创建了我的构造函数:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public struct Player {

    Client client;

    public string PlayerName { get; set; }
    public GameObject Avatar { get; set; }
    public int ConnectionId { get; set; }
    public byte[] Tex { get; set; }
    public string Type { get; set; }
    public string Id { get; set; }
    public int Strength { get; set; }
    public int Hitpoints { get; set; }
    public bool IsAlive { get; set; }

    // Initial method takes base arguments for testing
    public Player(string playerName, GameObject avatar, int connectionID) : this()
    {
        this.PlayerName = playerName;
        this.Avatar = avatar;
        this.ConnectionId = connectionID;

        client.infoDisplayText.GetComponent<Text>().text += playerName + " " + ConnectionId + " " + "\n";
    }

    // Overload method takes all player arguments
    public Player(string playerName, GameObject avatar, int connectionID, byte[] tex, string type, string id, int strength, int hitpoints, bool isAlive) : this()
    {
        this.PlayerName = playerName;
        this.Avatar = avatar;
        this.ConnectionId = connectionID;

        this.Tex = tex;
        this.Type = type;
        this.Id = id;
        this.Strength = strength;
        this.Hitpoints = hitpoints;
        this.IsAlive = isAlive;

        Debug.Log(id + " : " + type + " created with strength " + strength + ", hit points " + hitpoints + ", and a texture the size of " + tex.Length);

        client.infoDisplayText.GetComponent<Text>().text += playerName + " " + id + " : " + type + " created with strength " + strength + ", hit points " + hitpoints + ", and a texture the size of " + tex.Length +" \n";
    }
}

当我尝试使用构造函数方法虽然它不起作用 - 我的播放器名称没有出现,我的播放器移动不再在网络上更新。我需要做些什么改变才能让我的构造函数起作用?

我正用于播放我的播放器的完整功能:

private void SpawnPlayer(string pName, int cnnId)
    {
        GameObject go = Instantiate(playerPrefab) as GameObject;

        // Is this our player?
        if (cnnId == ourClientId)
        {
            // Add mobility
            go.AddComponent<Movement>();    // Add Movement.cs Script

            // Remove Connect Button
            if(GameObject.Find("Canvas").activeInHierarchy == true)
                GameObject.Find("ConnectButton").SetActive(false);

            isStarted = true;
        }

        Player p = new Player(playerName, go, cnnId);
        p.Avatar.GetComponentInChildren<TextMesh>().text = pName;
        players.Add(cnnId, p);
    }

1 个答案:

答案 0 :(得分:1)

  

当我尝试使用构造函数方法时虽然它不起作用 - 我的   玩家名称不会出现,我的玩家动作也不会更新   整个网络了。我需要做些什么改变才能得到我的   构造函数工作?

首先,这对构造函数没有任何意义。请注意,您的工作Playerstruct,无效工作class。这真的不应该有任何区别。

以下是导致新课程未更新的两个可能原因:

1 。问题是在该课程中使用{ get; set; }。 Unity无法序列化/反序列化自动属性。从class中的每个变量中删除它们,它应该可以正常工作。

2 。另外,您没有初始化该类中的client变量。在使用它或对其执行client.infoDisplayText.GetComponent<Text>()之前对其进行初始化。