ProjectClonerWindow.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityProjectCloner;
  6. using System.IO;
  7. namespace UnityProjectCloner
  8. {
  9. /// <summary>
  10. /// Provides Unity Editor window for ProjectCloner.
  11. /// </summary>
  12. public class ProjectClonerWindow : EditorWindow
  13. {
  14. /// <summary>
  15. /// True if currently open project is a clone.
  16. /// </summary>
  17. public bool isClone
  18. {
  19. get { return ProjectCloner.IsClone(); }
  20. }
  21. /// <summary>
  22. /// Returns true if project clone exists.
  23. /// </summary>
  24. public bool isCloneCreated
  25. {
  26. get { return ProjectCloner.GetCloneProjectsPath().Count >= 1; }
  27. }
  28. [MenuItem("Tools/Project Cloner")]
  29. private static void InitWindow()
  30. {
  31. ProjectClonerWindow window = (ProjectClonerWindow)EditorWindow.GetWindow(typeof(ProjectClonerWindow));
  32. window.titleContent = new GUIContent("Project Cloner");
  33. window.Show();
  34. }
  35. private void OnGUI()
  36. {
  37. if (isClone)
  38. {
  39. /// If it is a clone project...
  40. string originalProjectPath = ProjectCloner.GetOriginalProjectPath();
  41. if (originalProjectPath == string.Empty)
  42. {
  43. /// If original project cannot be found, display warning message.
  44. string thisProjectName = ProjectCloner.GetCurrentProject().name;
  45. string supposedOriginalProjectName = ProjectCloner.GetCurrentProject().name.Replace("_clone", "");
  46. EditorGUILayout.HelpBox(
  47. "This project is a clone, but the link to the original seems lost.\nYou have to manually open the original and create a new clone instead of this one.\nThe original project should have a name '" + supposedOriginalProjectName + "', if it was not changed.",
  48. MessageType.Warning);
  49. }
  50. else
  51. {
  52. /// If original project is present, display some usage info.
  53. EditorGUILayout.HelpBox(
  54. "This project is a clone of the project '" + Path.GetFileName(originalProjectPath) + "'.\nIf you want to make changes the project files or manage clones, please open the original project through Unity Hub.",
  55. MessageType.Info);
  56. }
  57. }
  58. else
  59. {
  60. /// If it is an original project...
  61. if (isCloneCreated)
  62. {
  63. GUILayout.BeginVertical("HelpBox");
  64. GUILayout.Label("Clones of this Project");
  65. /// If clone(s) is created, we can either open it or delete it.
  66. var cloneProjectsPath = ProjectCloner.GetCloneProjectsPath();
  67. for (int i = 0; i < cloneProjectsPath.Count; i++)
  68. {
  69. GUILayout.BeginVertical("GroupBox");
  70. string cloneProjectPath = cloneProjectsPath[i];
  71. EditorGUILayout.LabelField("Clone " + i);
  72. EditorGUILayout.TextField("Clone project path", cloneProjectPath, EditorStyles.textField);
  73. if (GUILayout.Button("Open in New Editor"))
  74. {
  75. ProjectCloner.OpenProject(cloneProjectPath);
  76. }
  77. GUILayout.BeginHorizontal();
  78. if (GUILayout.Button("Delete"))
  79. {
  80. bool delete = EditorUtility.DisplayDialog(
  81. "Delete the clone?",
  82. "Are you sure you want to delete the clone project '" + ProjectCloner.GetCurrentProject().name + "_clone'? If so, you can always create a new clone from ProjectCloner window.",
  83. "Delete",
  84. "Cancel");
  85. if (delete)
  86. {
  87. ProjectCloner.DeleteClone(cloneProjectPath);
  88. }
  89. }
  90. //Offer a solution to user in-case they are stuck with deleting project
  91. if (GUILayout.Button("?", GUILayout.Width(30)))
  92. {
  93. var openUrl = EditorUtility.DisplayDialog("Can't delete clone?",
  94. "Sometime clone can't be deleted due to it's still being opened by another unity instance running in the background." +
  95. "\nYou can read this answer from ServerFault on how to find and kill the process.", "Open Answer");
  96. if (openUrl)
  97. {
  98. Application.OpenURL("https://serverfault.com/a/537762");
  99. }
  100. }
  101. GUILayout.EndHorizontal();
  102. GUILayout.EndVertical();
  103. }
  104. GUILayout.EndVertical();
  105. //Have difficulty with naming
  106. //GUILayout.Label("Other", EditorStyles.boldLabel);
  107. if (GUILayout.Button("Add new clone"))
  108. {
  109. ProjectCloner.CreateCloneFromCurrent();
  110. }
  111. }
  112. else
  113. {
  114. /// If no clone created yet, we must create it.
  115. EditorGUILayout.HelpBox("No project clones found. Create a new one!", MessageType.Info);
  116. if (GUILayout.Button("Create new clone"))
  117. {
  118. ProjectCloner.CreateCloneFromCurrent();
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }