Start a new topic
Answered

Unity: Sync model after runtime import

Hi,


I import a model into my Unity-project at runtime.


After import I want to permanantly check if the model was updated (timestamp changed) and then auto-reload the model.

How can I do this live sync feature by script?


Thanks very much!


Best Answer

Hello Carsten,


According to Microsoft .NET documentation, you can use FileSystemInfo.LastWriteTime method. In Unity, you can adapt this:


using UnityEngine;
using System;
using System.IO;

public class LastWriteTime : MonoBehaviour
{
    public string filePath = "C:/myFile.pxz";

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) {
            if (File.Exists(filePath)) {
                FileInfo fi = new FileInfo(filePath);
                try {
                    Debug.Log(fi.LastWriteTime);
                } catch (Exception e) {
                    Debug.LogException(e);
                }
            }
        }
    }
}



Answer

Hello Carsten,


According to Microsoft .NET documentation, you can use FileSystemInfo.LastWriteTime method. In Unity, you can adapt this:


using UnityEngine;
using System;
using System.IO;

public class LastWriteTime : MonoBehaviour
{
    public string filePath = "C:/myFile.pxz";

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) {
            if (File.Exists(filePath)) {
                FileInfo fi = new FileInfo(filePath);
                try {
                    Debug.Log(fi.LastWriteTime);
                } catch (Exception e) {
                    Debug.LogException(e);
                }
            }
        }
    }
}


Thank you!

Login or Signup to post a comment