SteamVR_CopyExampleInputFiles.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System;
  5. using System.Linq;
  6. using System.IO;
  7. namespace Valve.VR
  8. {
  9. public class SteamVR_CopyExampleInputFiles : Editor
  10. {
  11. public const string steamVRInputExampleJSONCopiedKey = "SteamVR_Input_CopiedExamples";
  12. public const string exampleJSONFolderName = "ExampleJSON";
  13. [UnityEditor.Callbacks.DidReloadScripts]
  14. private static void OnReloadScripts()
  15. {
  16. CopyFiles();
  17. }
  18. public static void CopyFiles(bool force = false)
  19. {
  20. bool hasCopied = EditorPrefs.GetBool(steamVRInputExampleJSONCopiedKey, false);
  21. if (hasCopied == false || force == true)
  22. {
  23. string actionsFilePath = SteamVR_Settings.instance.actionsFilePath;
  24. bool exists = File.Exists(actionsFilePath);
  25. if (exists == false)
  26. {
  27. MonoScript[] monoScripts = MonoImporter.GetAllRuntimeMonoScripts();
  28. Type steamVRInputType = typeof(SteamVR_Input);
  29. MonoScript monoScript = monoScripts.FirstOrDefault(script => script.GetClass() == steamVRInputType);
  30. string path = AssetDatabase.GetAssetPath(monoScript);
  31. int lastIndex = path.LastIndexOf("/");
  32. path = path.Substring(0, lastIndex + 1);
  33. path += exampleJSONFolderName;
  34. string dataPath = Application.dataPath;
  35. lastIndex = dataPath.LastIndexOf("/Assets");
  36. dataPath = dataPath.Substring(0, lastIndex + 1);
  37. path = dataPath + path;
  38. string[] files = Directory.GetFiles(path, "*.json");
  39. foreach (string file in files)
  40. {
  41. lastIndex = file.LastIndexOf("\\");
  42. string filename = file.Substring(lastIndex + 1);
  43. string newPath = Path.Combine(dataPath, filename);
  44. try
  45. {
  46. File.Copy(file, newPath, false);
  47. Debug.Log("<b>[SteamVR]</b> Copied example input JSON to path: " + newPath);
  48. }
  49. catch
  50. {
  51. Debug.LogError("<b>[SteamVR]</b> Could not copy file: " + file + " to path: " + newPath);
  52. }
  53. }
  54. EditorPrefs.SetBool(steamVRInputExampleJSONCopiedKey, true);
  55. }
  56. }
  57. }
  58. }
  59. }