ApplicationStatusCreater.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor.ProjectWindowCallback;
  4. using System.IO;
  5. using UnityEditor;
  6. public class ApplicationStatusCreater
  7. {
  8. [MenuItem("Assets/Create/Application Status", false, 90)]
  9. public static void CreateNewLua()
  10. {
  11. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
  12. ScriptableObject.CreateInstance<CreateScriptAssetAction>(),
  13. GetSelectedPathOrFallback() + "/New Application Status.cs",
  14. null,
  15. "Assets/Script/Core/Editor/res/ApplicationStatusTemplate.txt");
  16. }
  17. public static string GetSelectedPathOrFallback()
  18. {
  19. string path = "Assets";
  20. foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
  21. {
  22. path = AssetDatabase.GetAssetPath(obj);
  23. if (!string.IsNullOrEmpty(path) && File.Exists(path))
  24. {
  25. path = Path.GetDirectoryName(path);
  26. break;
  27. }
  28. }
  29. return path;
  30. }
  31. class CreateScriptAssetAction : EndNameEditAction
  32. {
  33. public override void Action(int instanceId, string pathName, string resourceFile)
  34. {
  35. //创建资源
  36. UnityEngine.Object obj = CreateAssetFromTemplate(pathName, resourceFile);
  37. //高亮显示该资源
  38. ProjectWindowUtil.ShowCreatedAsset(obj);
  39. }
  40. internal static UnityEngine.Object CreateAssetFromTemplate(string pahtName, string resourceFile)
  41. {
  42. //获取要创建的资源的绝对路径
  43. string fullName = Path.GetFullPath(pahtName);
  44. string className = FileTool.RemoveExpandName( FileTool.GetFileNameByPath(fullName));
  45. //读取本地模板文件
  46. StreamReader reader = new StreamReader(resourceFile);
  47. string content = reader.ReadToEnd();
  48. reader.Close();
  49. //获取资源的文件名
  50. // string fileName = Path.GetFileNameWithoutExtension(pahtName);
  51. //替换默认的文件名
  52. content = content.Replace("{0}", className);
  53. //写入新文件
  54. StreamWriter writer = new StreamWriter(fullName, false, System.Text.Encoding.UTF8);
  55. writer.Write(content);
  56. writer.Close();
  57. //刷新本地资源
  58. AssetDatabase.ImportAsset(pahtName);
  59. AssetDatabase.Refresh();
  60. return AssetDatabase.LoadAssetAtPath(pahtName, typeof(UnityEngine.Object));
  61. }
  62. }
  63. }