Unity与c#脚本:类数据类型从类数据传递值两次奇怪

本文关键字:两次 脚本 数据类型 数据 Unity | 更新日期: 2023-09-27 17:53:18

我得到了一个简单的问题和奇怪的从传递值到数据类型类变量

以下代码:

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 ???**
}

。baseName是目录图片的来源,例如:

。baseName = "Corps/Lemon"。从"军团"文件夹的"柠檬"文件中找到的。这一点。baseName = folderx + "/";folderx是从itemDatabase: items输入的。添加新商品("柠檬",1,"蜂蜜柠檬",9,1020,75,158,0,1,item. rawtree)。阿瓦•大树就是这样一个例子,item.ItemType。生,item.ItemProd。队,"队"));//"军团"指的是文件夹

//Add Item Method

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";

你知道吗??

丹尼斯

Unity与c#脚本:类数据类型从类数据传递值两次奇怪

你的问题是:
您在Start的第一个代码片段中创建了一个项目。这应该会得到一个正确的项目。

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

带值:

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

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

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

原因是您实际上在AddItem中传递了database.items [i].itemIcon,而不是像在第一个代码片段中那样只传递了"Corps"

这就是你复制失败的地方。我建议大家看一下c# copy constructor