/// <summary> /// 反序列化部件 /// </summary> /// <param name="part"></param> /// <param name="weapon"></param> /// <returns></returns> static MonoPart DeserializeParts(PreserializedPart part, PreserializedWeapon weapon) { if (part.containedParts == null) { return(null); } var portCount = part.containedParts.Length; var prefab = WAPrefabStore.GetPartPrefab(part.prefabID); var mPart = GameObject.Instantiate(prefab.gameObject).GetComponent <MonoPart>(); var ports = mPart.Ports.ToArray(); for (int i = 0; i < ports.Length; i++) { if (part.containedParts[i] < 0) { continue; } var index = part.containedParts[i]; // TODO: Remove Workspace dependence? Workspace.Workspace.InstallPart((MonoPort)ports[i], DeserializeParts(weapon.containedParts[index], weapon)); } return(mPart); }
/// <summary> /// 预序列化武器 /// </summary> /// <param name="weapon"></param> /// <returns></returns> public static PreserializedWeapon Preserializate(IWeapon weapon) { PreserializedWeapon sweapon = new PreserializedWeapon(); List <PreserializedPart> sparts = new List <PreserializedPart>(); PreserializeParts(weapon.RootPart, sparts); sweapon.containedParts = sparts.ToArray(); return(sweapon); }
/// <summary> /// 从预序列化格式中反序列化出武器 /// </summary> /// <param name="weapon"></param> /// <returns></returns> public static MonoWeapon DeserializeWeapon(PreserializedWeapon weapon) { MonoWeapon mWeapon = new GameObject("New Weapon").AddComponent <BasicWeapon>(); mWeapon.RootPart = DeserializeParts(weapon.containedParts[0], weapon); ((MonoPart)mWeapon.RootPart).transform.SetParent(mWeapon.transform); ((MonoPart)mWeapon.RootPart).transform.localPosition = Vector3.zero; mWeapon.CompileWeaponAttribute(); mWeapon.RootPart.UpdateBelongingWeapon(); return(mWeapon); }