Unity With C# script : Class Data Type Passing Val

2019-03-03 05:37发布

问题:

I got a simple Question and weird from passing value to data type class variable.

the code like below :

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 is source from base name : basename + itemName; database.items[i].itemicon contain "Corps/Lemon" The Problem is where i debug.log database.items[i].itemicon it still the same string that is "Corps/Lemon", But when i put "itemxs = new item (database.items [i].itemName,....etc" and then i debug.log itemxs.itemicon it contain "Corps/Lemon/Lemon"

What is going on ?

Thanks

Dennis

Below is the class :

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

    }
}

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

    }
}

When I put this code :

 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 contain : "Lemon";

ItemICon contain: "Corps/" + itemName; You Can check it in itemDatabase.cs file and the constructor is at item.cs.

so the result itemIcon must be : "Corps/Lemon" But

Why itemxs.itemIcon got double itemName ? for example : "Corps/Lemon/Lemon".

回答1:

In item constructor you are doing it:

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

where folder == Corps and this.baseName == "Corps/Lemon/" and itemName == "Lemon" so you have exactly what you should get.



回答2:

In your constructor:

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

Did you get it now??

You may want to assign itemIcon value directly instead of doing all this.

Do this in your constructor :

itemIcon = folder;

Cheers