我目前做了一個 游戲的背包系統,它由2部分組成
第一部分是PropsControll,它再游戲開始時會把SQL里的玩家的背包的串列先加載出來
void Start ()
{
//初始化
Locator.SetProps(this);
#if UNITY_ANDROID
IconPath = Application.persistentDataPath + "/Resources/UI/Icons/Props/";
#elif UNITY_IOS
IconPath = Application.persistentDataPath + "/Resources/UI/Icons/Props/";
#elif UNITY_STANDALONE_WIN
IconPath = Application.dataPath + "/Resources/UI/Icons/Props/";
#else
IconPath = Application.dataPath + "/Resources/UI/Icons/Props/";
#endif
SQLiteHelper helper = new SQLiteHelper();
dt_PropsList = helper.ReadTable("PropsList");
helper.CloseConnection();
}
//列出背包資料
public void BagList()
{
for (int i = 0; i < dt_PropsList.Rows.Count; i++)
{
Props tempbag = new Props();
tempbag.Name = (string)dt_PropsList.Rows[i]["Name"];
tempbag.Amount = (int)dt_PropsList.Rows[i]["Amount"];
tempbag.Description = (string)dt_PropsList.Rows[i]["Description"];
tempbag.Icon = IconPath + (string)dt_PropsList.Rows[i]["Icon"] + ".png";
_props.Add(tempbag);
}
}
第二部分是綁在背包按鈕上的代碼,它的作業是讀取PropsControll的List,并加進字典里
public PropsMaterial _temp;
public Image _tempIcon;
public Text _tempDescription;
public Dictionary<string, Sprite> _iconDic = new Dictionary<string, Sprite>();
public List<PropsMaterial> _material;
public PropsControll _controll;
public RectTransform _panel;
public UIManager _ui;
public ResearchMusic _research;
private void Awake()
{
_controll = FindObjectOfType<PropsControll>();
_controll.ResearchList();
_ui = FindObjectOfType<UIManager>();
for (int i = 0; i < _controll._props.Count; i++)
{
Sprite temp = FileReadWriteHelper.LoadSprite(_controll._props[i].Icon,100,85);
_iconDic.Add(_controll._props[i].Name, temp);
}

目前的問題是這樣,我第一次進入場景時,背包作業正常
但是如果我切換出場景時,則會出現這個 問題,并且背包顯示不出任何資料了
請問如何解決呢?
uj5u.com熱心網友回復:
1,重復添加問題這里建議加入一個判斷是否已經添加過對應的資料,如果是,那么放棄添加。
ps:可以在添加最后一個背包資料時加入一個[信號值]作為判斷。
2:其實建議不以官方教學的到處掛載組件形式編程,建議建立擁有標準的物體類工程結構,背包就不會被場景所影響。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/18985.html
標籤:Unity3D
上一篇:unity多載
