using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine;
[Serializable] public class GoodsInfo : Item { public string xxx;
public override object Clone() { MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, this); stream.Position = 0; var obj = formatter.Deserialize(stream); return obj; } }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class BagPanel : InventoryPanel { private static BagPanel instance; public static BagPanel I { get { if (instance == null) { instance = new BagPanel(); }
return instance; } }
private BagPanel() { }
public GameObject itemGo; public BagData bagData = new BagData(); public Button btnAdd; public Button btnUse; private void Start() { instance = this; bagData.InitData("ItemData", this, itemGo, 25); bagData.TestInit(); bagData.LoadPanel(); btnAdd.onClick.AddListener(OnAddItem); btnUse.onClick.AddListener(OnUseItem);
for (int i = 0; i < content.childCount; ++i) { content.GetChild(i).gameObject.name = i.ToString(); } }
public void OnAddItem() { bagData.AddItem(9,1); }
public void OnUseItem() { bagData.UseItem(3,1); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PanDrop : MonoBehaviour { public Button btnYes; public Button btnNo; private InventoryItem it; private int pos; void Start() { btnYes.onClick.AddListener(OnBtnYes); btnNo.onClick.AddListener(OnBtnNo); }
public T ObjectDeepCopy(T inM) { Type t = inM.GetType(); T outM = (T)Activator.CreateInstance(t); foreach (PropertyInfo p in t.GetProperties()) { t.GetProperty(p.Name).SetValue(outM, p.GetValue(inM)); } return outM; }
内存拷贝:序列化的类必须有[Serializable]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicstatic T DeepCopy(T obj) { object retval; using (MemoryStream ms = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); //序列化成流 bf.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); //反序列化成对象 retval = bf.Deserialize(ms); ms.Close(); }