Advertisement
apieceoffruit

AssetProcessor

Mar 31st, 2021
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1.   void OnPreprocessAsset()
  2.     {
  3.         metaData = assetImporter.userData;
  4.         filename = Path.GetFileNameWithoutExtension(assetPath);
  5.         fileWithExt = Path.GetFileName(assetPath);
  6.         assetsFolder = Application.dataPath;
  7.         projectFolder = Directory.GetParent(assetsFolder);
  8.         importFilePath = assetImporter.assetPath;
  9.         fileCurrentTimestamp = WithoutMS(File.GetLastWriteTime(importFilePath));
  10.         if (!EditorApplication.isPlaying)
  11.         {
  12.             if (assetPath.Contains(".xd"))
  13.             {
  14.                 if (!metaData.Contains("alreadyImported: true"))
  15.                 {
  16.                     if (EditorUtility.DisplayDialog("Parse Adobe Xd file to Unity?",
  17.                         "Do you want to convert " + fileWithExt + " to Unity scenes?",
  18.                         "Convert", "Add file to project as is"))
  19.                     {
  20.                         CoreImporter();
  21.                     }
  22.  
  23.                 }
  24.                 else if (metaData.Contains("alreadyImported: true"))
  25.                 {
  26.                     string dateOf = "dateOfModification: ";
  27.                     int from = metaData.IndexOf(dateOf) + dateOf.Length;
  28.                     if (metaData.LastIndexOf("~;") > 0) fileLastTimeStamp = WithoutMS(DateTime.Parse(metaData.Substring(from, metaData.LastIndexOf("~;") - from)));
  29.                     if (metaData.Contains("reimport: true;"))
  30.                     {
  31.                         ReimportAsset();
  32.                         Selection.activeObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
  33.                     }
  34.                     else if (fileCurrentTimestamp > fileLastTimeStamp && !metaData.Contains("reimport: true;"))
  35.                     {
  36.                         if (EditorUtility.DisplayDialog("Reload imported file?",
  37.                            "File " + fileWithExt + " has changed. Do you want to reload? All changes made in Unity will be lost.",
  38.                            "Reload", "Keep current version"))
  39.                         {
  40.                             ReimportAsset();
  41.                             Selection.activeObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
  42.                         }
  43.                     }
  44.                 }
  45.             }
  46.  
  47.         }
  48.         else
  49.             Debug.LogWarning("Can't import Xd files in playmode");
  50.     }
  51.  
  52.     public void ReimportAsset()
  53.     {
  54.         if (Directory.Exists(projectFolder + "/~Temp-" + filename)) Directory.Delete(projectFolder + "/~Temp-" + filename, true);
  55.         if (Directory.Exists("Assets/Resources/[AdobeXd]-" + filename)) Directory.Delete("Assets/Resources/[AdobeXd]-" + filename, true);
  56.         CoreImporter();
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement