【问题标题】:Unity With C# script : Class Data Type Passing Value From Class With Data Twice WeirdUnity 与 C# 脚本:类数据类型从具有数据的类传递值两次奇怪
【发布时间】:2016-06-28 09:53:26
【问题描述】:

我得到了一个简单的问题,并且很奇怪将值传递给数据类型类变量。

如下代码:

void AddItem(int ID) {
    for (int i = 0; i < database.items.Count; i++) {
        if(database.items[i].itemID == ID) {
            itemxs = new item (database.items [i].itemName,
                              database.items [i].itemID,
                              database.items [i].itemDesc,
                              database.items [i].harvest,
                              database.items [i].itemTime,
                              database.items [i].stdprice,
                              database.items [i].hightprice,
                              database.items [i].itemStock,
                              database.items [i].Lvlunlock,
                              database.items [i].rawTree,
                              database.items [i].itemType,
                              database.items [i].itemProd,
                              database.items [i].itemIcon);

            Debug.Log ("Item Icon 1 : " + database.items[i].itemIcon); // This appear "Corps/Lemon"
            Debug.Log ("Item Icon 2 : " + itemxs.itemIcon); // This appear "Corps/Lemon/Lemon" **even though I just passing value from code itemxs = new item (database.items [i].itemName,...etc)** it is wierd

            CheckInventoryExist(itemxs);
            break;
        }
    }
}

itemicon 来自基本名称:basename + itemName; database.items[i].itemicon 包含 "Corps/Lemon" 问题是我 debug.log database.items[i].itemicon 它仍然是相同的字符串 "Corps/Lemon", 但是当我输入 "itemxs = new item (database.items [i].itemName,..etc" 然后我 debug.log itemxs.itemicon 它包含 "Corps/Lemon /柠檬”

发生了什么事?

谢谢

丹尼斯

下面是类:

item.cs

using UnityEngine;
using System.Collections;

// Make Class Item
public class item {
    public string itemName;
    public int itemID;
    public string itemDesc;
    public string itemIcon;
    public GameObject itemModel;
    public int itemTime;
    public int hightprice;
    public int stdprice;
    public int itemStock;
    public int harvest;
    public RawTree rawTree;
    public ItemType itemType;
    public ItemProd itemProd;
    public int Lvlunlock;
    private string baseName;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void Awake () {

    }

    public enum ItemType {
        Raw,
        Admirable,
        Valuable
    }

    public enum RawTree {
        BigTree,
        SmallTree,
        Field,
        None
    }

    public enum ItemProd {
        AnimalBarm,
        Mining,
        Corps,
        Dairy,
        JuiceJamMaker,
        Merchant,
        Kitchen,
        Bakery,
        CraftHouse
    }

    public item (string name, int ID, string desc, int harvestx, int time, int stdpricex, int hightpricex, int stock, int Lvlunlockx, RawTree RawTree, ItemType type, ItemProd prod, string folder) {
        itemName = name;
        itemID = ID;
        itemDesc = desc;
        harvest = harvestx;
        itemTime = time;
        stdprice = stdpricex;
        hightprice = hightpricex;
        itemStock = stock;
        Lvlunlock = Lvlunlockx;
        rawTree = RawTree;
        itemType = type;
        itemProd = prod;
        this.baseName = folder + "/";
        itemIcon = this.baseName + itemName;
    }

    public item() {

    }
}

然后是 itemDatabase.cs

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

public class itemDatabase : MonoBehaviour {
    public List<item> items = new List<item>();

    // Use this for initialization
    void Start () {
        // Add Data To Item List With Class Item

        // ------------------------------- Item Corps (Raw - Big Tree) ------------------------------- //
        items.Add (new item ("Lemon", 1, "Honey Lemon", 9, 1020, 75, 158, 0, 1, item.RawTree.BigTree, item.ItemType.Raw, item.ItemProd.Corps, "Corps"));

    }

    // Update is called once per frame
    void Update () {

    }
}

当我输入这段代码时:

 itemxs = new item (database.items [i].itemName, // This contain "Lemon"
                                      database.items [i].itemID,
                                      database.items [i].itemDesc,
                                      database.items [i].harvest,
                                      database.items [i].itemTime,
                                      database.items [i].stdprice,
                                      database.items [i].hightprice,
                                      database.items [i].itemStock,
                                      database.items [i].Lvlunlock,
                                      database.items [i].rawTree,
                                      database.items [i].itemType,
                                      database.items [i].itemProd,
                                      database.items [i].itemIcon);

itemName 包含:“柠檬”;

ItemICon 包含:"Corps/" + itemName;您可以在 itemDatabase.cs 文件中检查它,构造函数位于 item.cs。

所以结果 itemIcon 必须是:"Corps/Lemon" 但是

为什么 itemxs.itemIcon 有双 itemName ?例如:“军团/柠檬/柠檬”。

【问题讨论】:

  • 你能说出这个神奇的构造函数代码中发生了什么吗?
  • 我已经用类更新了代码。你能检查一下吗?
  • 作为旁注:从您的 item 类中删除 StartAwakeUpdate。它不是从MonoBehaviour 继承的,因此不能拥有它们(或者至少它们没有做同样的事情)。此外,类名应始终以大写字母开头。

标签: c# class unity3d variables


【解决方案1】:

item 构造函数中,你正在这样做:

this.baseName = folder + "/";
itemIcon = this.baseName + itemName;

folder == Corpsthis.baseName == "Corps/Lemon/"itemName == "Lemon" 是你应该得到的。

【讨论】:

  • 在 itemDatabase : items.Add (new item ("Lemon", 1, "Honey Lemon", 9, 1020, 75, 158, 0, 1, item.RawTree.BigTree, item.ItemType .Raw, item.ItemProd.Corps, "Corps")); .......参数的结尾我只输入了“Corps”。所以 this.baseName = folder = "Corps/" 没有柠檬。我尝试删除 itemName;但 itemIcon 仅包含“Corps/”。有什么问题?
【解决方案2】:

在你的构造函数中:

 this.baseName = folder + "/";  // if folder = "Corps/Lemon" from database.items[i].itemIcon
 itemIcon = this.baseName + itemName; // it will update itemIcon to "Corps/Lemon" + "/" + "Lemon"

现在收到了吗?

您可能希望直接分配 itemIcon 值而不是执行所有这些操作。

在您的构造函数中执行此操作:

itemIcon = folder;

干杯

【讨论】:

  • 在 itemDatabase : items.Add (new item ("Lemon", 1, "Honey Lemon", 9, 1020, 75, 158, 0, 1, item.RawTree.BigTree, item.ItemType .Raw, item.ItemProd.Corps, "Corps")); .......参数的结尾我只输入了“Corps”。所以 this.baseName = folder = "Corps/" 没有柠檬。我尝试删除 itemName;但 itemIcon 仅包含“Corps/”。有什么问题?
  • 来自您的原始代码:database.items [i].itemIcon // This Contain "Corps/Lemon"); 您传递的是 itemIcon 而不是字符串“Corps”
  • 你能看看上面的这一行吗:Debug.Log (database.items[i].itemIcon); // 虽然 Debug 这仍然包含 "Corps/Lemon" Debug.Log (itemxs.itemIcon); // 当 Debug 这变成包含“Corps/Lemon/Lemon”。
  • 如果您将itemIcon 作为您的文件夹名称,这正是您所期望的。这是准确的行为,因为代码本身没有大脑。这肯定是一个逻辑错误。
  • @DennisLiu 如果您无法理解基本的编程逻辑,则无需对我的答案投反对票。阅读其他人的代码和回答问题需要时间,所以至少感谢我的帮助:)
猜你喜欢
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
相关资源
最近更新 更多