Advertisement
Guest User

Untitled

a guest
May 26th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. using UnityEditor;
  2. using System.CodeDom;
  3. using System.IO;
  4. using System.CodeDom.Compiler;
  5.  
  6. public class CreateInterfaceMenuItem {
  7. [MenuItem("Assets/Create/File/Interface...", false, 10)]
  8. private static void CreateInterface() {
  9. var filePath = EditorUtility.SaveFilePanelInProject("Save file", "Interface", "cs", "Enter interface name");
  10. if (!string.IsNullOrEmpty(filePath)) {
  11. using (var streamWriter = new StreamWriter(File.Create(filePath))) {
  12. using (var textWriter = new IndentedTextWriter(streamWriter, " ")) {
  13. var fileName = Path.GetFileNameWithoutExtension(filePath);
  14. var unit = BuildInterfaceGraph(fileName);
  15. var provider = CodeDomProvider.CreateProvider("CSharp");
  16. provider.GenerateCodeFromCompileUnit(unit, textWriter, new CodeGeneratorOptions());
  17.  
  18. }
  19. }
  20. }
  21. AssetDatabase.Refresh();
  22. }
  23.  
  24. private static CodeCompileUnit BuildInterfaceGraph(string interfaceName) {
  25. var codeCompileUnit = new CodeCompileUnit();
  26.  
  27. var codeNamespace = new CodeNamespace();
  28. codeNamespace.Imports.AddRange(new [] {
  29. new CodeNamespaceImport("UnityEngine"),
  30. new CodeNamespaceImport("System.Collections")
  31. });
  32. codeCompileUnit.Namespaces.Add(codeNamespace);
  33.  
  34. var declaration = new CodeTypeDeclaration(interfaceName);
  35. declaration.IsInterface = true;
  36. codeNamespace.Types.Add(declaration);
  37.  
  38. return codeCompileUnit;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement