Monitor ini file changes and perform joint debugging in C# unity project, a beginner's guide

1. Unity creates a new empty object and attaches the script

2. The script is written as follows: Note that file monitoring needs to be executed in updata, and the class must be inherited from MonoBehaviour. This is because in the unity project, one is the start entrance Start(), and the other is a bit like window refresh. Of course, I don’t know if it is understood in this way. I am also a novice anyway a>


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Web;
using UnityEngine;

namespace FileWatchered
{
    public class IniFile
    {
        public string path; //INI文件名


        //声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key,
                    string val, string filePath);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def,
                    StringBuilder retVal, int size, string filePath);


        //类的构造函数,传递INI文件名
        public IniFile(string INIPath)
        {
            path = INIPath;
        }


        //写INI文件
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }


        //读取INI文件指定
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }
    }


   


    class scr : MonoBehaviour
    {
        static FileSystemEventArgs curChangeEventArgs;
   
        void Start()
        {
            Debug.Log("sss");
            curChangeEventArgs = null;
            string CurDir = System.Environment.CurrentDirectory;
            //Response.Write("<script>alert('begin!')</script>");
            CurDir = "";
            WatcherStrat("F:/test", "*.ini"); //*.*表示监听文件夹下所有文件,“.txt”监视某种格式的文件
            Console.ReadKey();

            
        }

        void ReadIniInfo()
        {

            IniFile m_iniFile = new IniFile("F:/test/100.ini");
            string strType = m_iniFile.IniReadValue("product", "type");
            string strProductId = m_iniFile.IniReadValue("product", "productId");
            string strImageurl = m_iniFile.IniReadValue("product", "imageUrl");
            Debug.Log(strType);
            Debug.Log(strProductId);
            Debug.Log(strImageurl);
        }

        void Update()
        {
            if (curChangeEventArgs != null)
            {
                if (curChangeEventArgs.ChangeType == WatcherChangeTypes.Created)
                {
                    Debug.Log("Created");
                }
                else if (curChangeEventArgs.ChangeType == WatcherChangeTypes.Changed)
                {
                    Debug.Log("Change");
                    ReadIniInfo();
                }

                curChangeEventArgs = null;
            }
        }

        private  void WatcherStrat(string path, string filter)
        {
            Debug.Log("WatcherStrat");

            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = path;

            watcher.Filter = filter;

            watcher.Changed += new FileSystemEventHandler(OnProcess);

            watcher.Created += new FileSystemEventHandler(OnProcess);

            watcher.Deleted += new FileSystemEventHandler(OnProcess);

            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            watcher.EnableRaisingEvents = true;

            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess

                                   | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;

            watcher.IncludeSubdirectories = true;
        }

        private static void OnProcess(object source, FileSystemEventArgs e)
        {
            curChangeEventArgs = e;

            /*if (e.ChangeType == WatcherChangeTypes.Created)
            {
                Debug.Log("Created");
                //Response.Write("<script>Changed('Created!')</script>");
                OnCreated(source, e);
            }

            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                Debug.Log("Change");

                IniFile ini = new IniFile("F:/test/100.ini");
                ini.IniWriteValue("一号车道", "OneLane", "18");
                string getinivalue = ini.IniReadValue("一号车道", "OneLane");

                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                sb.Append("<script>");
                sb.Append(getinivalue);
                sb.Append("</script>");
                //Response.Write(sb.ToString());
                OnChanged(source, e);
            }

            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e);
            }*/
        }

        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件新建事件处理逻辑 {0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件改变事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("文件删除事件处理逻辑{0}  {1}   {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("文件重命名事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }
    }
}

3.vs Debugging》Attach the unity debugger and turn on the unity play, then you can perform joint debugging

4. Specify ini to see the effect. I wrote it hard. You can modify the path yourself.

F:/test/100.ini

[product]
type = 2
productId = 5
imageUrl=E:/9999.png

5. Do not set full screen when exporting exe from unity

 

Guess you like

Origin blog.csdn.net/qq_30377315/article/details/127938696