【发布时间】: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