【问题标题】:Unity With C# script : Class Data Type Passing Value From Class Data Twice WeirdUnity 与 C# 脚本:类数据类型从类数据传递值两次奇怪
【发布时间】:2016-10-31 15:04:09
【问题描述】:

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

下面的代码:

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

}

}

构造函数:

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;

    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 folderx) {
    itemName = name;
    itemID = ID;
    itemDesc = desc;
    harvest = harvestx;
    itemTime = time;
    stdprice = stdpricex;
    hightprice = hightpricex;
    itemStock = stock;
    Lvlunlock = Lvlunlockx;
    rawTree = RawTree;
    itemType = type;
    itemProd = prod;
    **// line : 60 
    // itemName contain : "Lemon" // folderx contain : "Corps"**
    this.baseName = folderx + "/"; **// basename is directory source picture**
    itemIcon = this.baseName + itemName;
    Debug.Log ("item name : " + itemName); **// result : "Lemon"**
    Debug.Log ("item icon : " + itemIcon); **// result : "Corps/Lemon/Lemon" Why this appear "Corps/Lemon/Lemon" not "Corps/Lemon" ???? this is a mistake right ??**
    Debug.Log ("this.basename : " + this.baseName); **// result : "Corps/Lemon" why this appear "Corps/Lemon" not "corps/" ???? this is also a mistake right ???**
}

this.baseName 是示例目录图片的来源:

this.baseName = "军团/柠檬"。所以它来自文件夹“军团”文件“柠檬”。 this.baseName = 文件夹x + "/"; folderx 是从 itemDatabase 输入的: items.Add (new item ("Lemon", 1, "Honey Lemon", 9, 1020, 75, 158, 0, 1, item.RawTree.BigTree, item.ItemType.Raw, item. ItemProd.Corps,“军团”)); // “军团”指的是文件夹x

//添加项目方法

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);
                // Line 80
                Debug.Log ("Item Icon 1 : " + database.items[i].itemIcon); // result "Corps/Lemon"
                Debug.Log ("Item Icon 2 : " + itemxs.itemIcon); // result "Corps/Lemon/Lemon";

                CheckInventoryExist(itemxs);
                break;
            }
        }
    }

问题从第 60 行和第 80 行(在 Add item 方法中)开始,您可以看到我只是从

传递值
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);

当我调试时这个值很奇怪。

  // Line 80
Debug.Log ("Item Icon 1 : " + database.items[i].itemIcon); // result "Corps/Lemon"
            Debug.Log ("Item Icon 2 : " + itemxs.itemIcon); // result "Corps/Lemon/Lemon";

有什么想法吗??

丹尼斯

【问题讨论】:

  • 没有足够的信息来检查。需要更多代码。为什么不在修改文件夹 x 和 baseName 的地方添加代码?
  • 够了吗?请检查它..
  • 什么是baseName?数据类型请提供更多详细信息
  • 我已对代码进行了一些更改以获取更多详细信息。请检查..谢谢
  • 它给出了正确的值。通过调试值进行检查。我相信你正在改变其他地方。我刚刚检查了你的代码imgur.com/zcdak9v

标签: c# class unity3d parameter-passing


【解决方案1】:

你的问题是这样的:
您在Start 的第一个sn-p 中创建一个项目。这应该导致正确的项目。

现在,对于您的itemxs,您可以使用现有项目的值创建一个项目。

有值:

Item1:
    itemName == "Lemon"
    baseName == "Corps/"
    itemIcon == "Corps/Lemon"

当你在 itemxs 的构造函数中输入 this 时,它看起来像这样(你传递给构造函数的值):

Item(string name == "Lemon", ..., string folderx == "Corps/Lemon")

原因是您实际上在AddItem 中传递了database.items [i].itemIcon,而不仅仅是像在第一个sn-p 中那样传递"Corps"

这就是您的复制失败的地方。我实际上建议您查看c# copy constructor

【讨论】:

  • yahh..我明白了..你知道如何解决它吗?
  • 更改构造函数的内容,或者如我上一句所述,看看复制构造函数。如果您有多个位置要从数据库中复制项目,这也将为您节省大量输入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多