간단한 저장...
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public class XmlManager : MonoBehaviour
{
/// <summary>
/// 생활용품 리스트
/// </summary>
public List<GameObject> gameObjects;
string _filePath;
public Transform ContentsParent;
public GameObject Prefab;
private void Awake()
{
_filePath = Application.dataPath + "/Resources/XML/LifeItems.XML";
}
public void SaveInfo()
{
try
{
///파일이 있는지 없는지 확인하는 로직은 만들어야함 일단은 진행하도록함if()
XmlDocument document = new XmlDocument();
XmlElement FList = document.CreateElement("LifeItems");
foreach (var obj in gameObjects)
{
XmlElement objElement = document.CreateElement("item: " + obj.name);
objElement.SetAttribute("Type", "생활용품");
objElement.SetAttribute("이름", obj.name);
objElement.SetAttribute("유통기한", obj.transform.position.ToString());
FList.AppendChild(objElement);
}
document.Save(_filePath);
}
catch (System.Exception e)
{
Debug.Log("Error:" + e.ToString());
}
}
public void LoadInfo()
{
try
{
XmlDocument document = new XmlDocument();
document.Load(_filePath);
XmlElement LifeItemList = document["LifeItems"];
GameObject targetObj;
foreach (XmlElement item in LifeItemList.ChildNodes)
{
GameObject lifeItem = Instantiate(Prefab, ContentsParent);
lifeItem.name = item.GetAttribute("이름");
}
}
catch (System.Exception e)
{
Debug.Log("Error:" + e.ToString());
}
}
}
###################################
//파일정보 있는지 확인하여 새로운파일 만드는것
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public class XmlManager : MonoBehaviour
{
/// <summary>
/// 생활용품 리스트
/// </summary>
public List<LifeItem> m_LifeItems;
string _filePath;
public LifeItemTableViewController m_LifeItemTableViewController;
public Transform ContentsParent;
public GameObject Prefab;
private void Awake()
{
m_LifeItems = new List<LifeItem>();
_filePath = Application.dataPath + "/Resources/XML/LifeItems.XML";
Debug.Log("_filePath"+ _filePath);
m_LifeItemTableViewController = FindObjectOfType<LifeItemTableViewController>();
Prefab = FindObjectOfType<LifeItemTableViewCell>().gameObject;
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
AddLifeItem();
}
if (Input.GetKeyDown("2"))
{
LoadInfo();
}
}
public void AddLifeItem()
{
LifeItem item = new LifeItem("칫솔", "생활용품", "20200820", System.DateTime.Now.ToString());
m_LifeItems.Add(item);
m_LifeItemTableViewController.AddData(item);
m_LifeItemTableViewController.ItemUpdate();
SaveInfo();
}
public void SaveInfo()
{
try
{
///파일이 있는지 없는지 확인하는 로직은 만들어야함 일단은 진행하도록함if()
FileInfo fileInfo = new FileInfo(_filePath);
if (fileInfo.Exists)
{
///파일이 존재한다면
XmlDocument document = new XmlDocument();
document.Load(_filePath);
XmlElement FList = document["LifeItems"];
foreach (var obj in m_LifeItems)
{
XmlElement objElement = document.CreateElement("물건정보");
objElement.SetAttribute("이름", obj.name);
objElement.SetAttribute("타입", obj.type);
objElement.SetAttribute("유통기한시작", obj.startDate);
objElement.SetAttribute("유통기한끝", obj.endDate);
objElement.SetAttribute("유통기한", obj.startDate + "~" + obj.endDate);
FList.AppendChild(objElement);
}
document.Save(_filePath);
}
else
{
///파일이 존재하지않는다면
XmlDocument document = new XmlDocument();
XmlElement FList = document.CreateElement("LifeItems");
document.AppendChild(FList);
foreach (var obj in m_LifeItems)
{
XmlElement objElement = document.CreateElement("물건정보");
objElement.SetAttribute("이름", obj.name);
objElement.SetAttribute("타입", obj.type);
objElement.SetAttribute("유통기한시작", obj.startDate);
objElement.SetAttribute("유통기한끝", obj.endDate);
objElement.SetAttribute("유통기한", obj.startDate + "~" + obj.endDate);
FList.AppendChild(objElement);
}
document.Save(_filePath);
}
}
catch (System.Exception e)
{
Debug.Log("Error:" + e.ToString());
}
}
public void LoadInfo()
{
try
{
XmlDocument document = new XmlDocument();
document.Load(_filePath);
XmlElement LifeItemList = document["LifeItems"];
m_LifeItems = new List<LifeItem>();
GameObject targetObj;
foreach (XmlElement item in LifeItemList.ChildNodes)
{
LifeItem Li = new LifeItem(item.GetAttribute("이름"), item.GetAttribute("타입"), item.GetAttribute("유통기한시작"), item.GetAttribute("유통기한끝"));
m_LifeItems.Add(Li);
m_LifeItemTableViewController.AddData(Li);
}
m_LifeItemTableViewController.ItemUpdate();
}
catch (System.Exception e)
{
Debug.Log("Error:" + e.ToString());
}
}
}
댓글 없음:
댓글 쓰기