ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 유니티 XML 읽기/쓰기
    Unity/Unity Script 2022. 1. 10. 13:46
    728x90
    using System.Collections.Generic;
    using UnityEngine;
    
    public class XmlManager
    {
        /// <summary>XML로 해당 path 경로에 classForSave 함수를 저장합니다. 
        public static T XMLSerialize<T>(T classForSave, string path)
        {
            System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(T));
            using (System.IO.TextWriter tw = new System.IO.StreamWriter(path))
            {
                sr.Serialize(tw, classForSave);
                tw.Close();
                return classForSave;
            }
            
        }
        /// <summary>XML로 해당 path 경로에서 classForCopy 함수를 불러옵니다.
        public static T XMLDeserialize<T>(string path)
        {
            System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(T));
            using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
            {
                T t = (T)sr.Deserialize(fs);
                fs.Close();
    
                return t;
            }
        }
    
        public static bool IsExist(string path)
        {
            if (System.IO.File.Exists(path))
                return true;
            else
                return false;
        }
    
        /// <summary>pattern(정규식)과 일치하는 파일이 있는지 검사합니다.</summary>
        public static bool IsMatchExist(string path, string pattern)
        {
            System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);
            string[] files =  System.IO.Directory.GetFiles(path);
            foreach(var file in files)
            {
                if (rgx.IsMatch(file))
                {
                    return true;
                }
            }
    
            return false;
        }
    
    }
    728x90

    'Unity > Unity Script' 카테고리의 다른 글

    UITookit VisualElement 드래그 움직이기  (1) 2023.12.11

    댓글

Designed by Tistory.