2016-06-29 2 views
0

데이터 형식 클래스 변수에 값을 전달하는 간단한 질문과 이상한 점이 있습니다. 아래Unity with C# 스크립트 : 클래스 데이터 형식 클래스 데이터 전달 값이 두 번 이상한

코드 :

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

} 

}

생성자 :

,369 :

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 = folderx + "/"; folderx는 itemDatabase : items.Add (새 항목 ("Lemon", 1, "Honey Lemon", 9, 1020, 75, 158, 0, 1, item.RawTree.BigTree, item.ItemType.Raw, item)에서 입력됩니다. ItemProd.Corps, "Corps")); 당신이 난 그냥

에서 값을 전달 볼 수 // "군단"

가 // 라인 (60)과 (항목 추가 방법에서) 라인 80에서 항목 방법

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

문제의 시작을 추가 folderx 참조

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

모든 아이디어 ??

데니스

+0

확인할 정보가 충분하지 않습니다. 더 많은 코드가 필요합니다. 왜 폴더 x와 baseName을 수정할 지 코드를 추가하지 않으시겠습니까? –

+0

충분하니? 그것을 확인하십시오. –

+0

baseName은 무엇입니까? 데이터 타입은 좀 더 자세한 내용을 넣어주세요 –

답변

0

귀하의 문제는 이것이다 :
당신은 Start에 첫 조각에서 항목을 만들 수 있습니다. 이렇게하면 올바른 항목이 생성됩니다.

이제 itemxs의 경우 기존 항목의 값을 사용하여 항목을 만듭니다. 값

는 :

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

당신이 itemxs의 생성자이 먹이 때 (값이 생성자에 전달)이 다음과 같이 표시됩니다

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

이유는 당신이다 실제로 database.items [i].itemIconAddItem에 전달하면 첫 번째 코드에서와 마찬가지로 "Corps" 일뿐 아니라

복사가 실패한 곳입니다. 실제로는 c# copy constructor을 찾아 볼 것을 권합니다.

+0

yahh..i 그것을 얻었습니다. 어떻게 수정해야하는지 알고 있습니까? –

+0

생성자의 내용을 변경하거나 마지막 문장에서 설명한대로 복사 생성자를 살펴보십시오. 이렇게하면 데이터베이스에서 항목을 복사하려는 여러 위치가있는 경우 입력 내용이 상당히 줄어 듭니다. –

+0

괜찮습니까 .. 고맙습니다. –

관련 문제