MergeActionParenting.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace GitMerge
  4. {
  5. /// <summary>
  6. /// The MergeAction that handles a differing parents for a Transform.
  7. /// </summary>
  8. public class MergeActionParenting : MergeAction
  9. {
  10. private Transform transform;
  11. private Transform ourParent;
  12. private Transform theirParent;
  13. public MergeActionParenting(Transform transform, Transform ourParent, Transform theirParent)
  14. : base(transform.gameObject, null)
  15. {
  16. this.transform = transform;
  17. this.ourParent = ourParent;
  18. this.theirParent = theirParent;
  19. }
  20. protected override void ApplyOurs()
  21. {
  22. transform.parent = ourParent;
  23. }
  24. protected override void ApplyTheirs()
  25. {
  26. var ourVersion = ObjectDictionaries.GetOurCounterpartFor(theirParent) as Transform;
  27. if (theirParent && !ourVersion)
  28. {
  29. if (EditorUtility.DisplayDialog("The chosen parent currently does not exist.", "Do you want do add it?", "Yes", "No"))
  30. {
  31. ObjectDictionaries.EnsureExistence(theirParent.gameObject);
  32. ourVersion = ObjectDictionaries.GetOurCounterpartFor(theirParent) as Transform;
  33. transform.parent = ourVersion;
  34. }
  35. else
  36. {
  37. throw new System.Exception("User Abort.");
  38. }
  39. }
  40. else
  41. {
  42. transform.parent = ourVersion;
  43. }
  44. }
  45. public override void OnGUI()
  46. {
  47. GUILayout.BeginVertical();
  48. GUILayout.Label("Parent");
  49. GUILayout.BeginHorizontal();
  50. GUILayout.Label(ourParent ? ourParent.ToString() : "None", GUILayout.Width(100));
  51. if (MergeButton(">>>", usingOurs))
  52. {
  53. UseOurs();
  54. }
  55. var c = GUI.backgroundColor;
  56. GUI.backgroundColor = Color.white;
  57. var newParent = EditorGUILayout.ObjectField(transform.parent, typeof(Transform), true, GUILayout.Width(170)) as Transform;
  58. if (newParent != transform.parent)
  59. {
  60. transform.parent = newParent;
  61. UsedNew();
  62. }
  63. GUI.backgroundColor = c;
  64. if (MergeButton("<<<", usingTheirs))
  65. {
  66. UseTheirs();
  67. }
  68. GUILayout.Label(theirParent ? theirParent.ToString() : "None", GUILayout.Width(100));
  69. GUILayout.EndHorizontal();
  70. GUILayout.EndVertical();
  71. }
  72. private static bool MergeButton(string text, bool green)
  73. {
  74. if (green)
  75. {
  76. GUI.color = Color.green;
  77. }
  78. bool result = GUILayout.Button(text, GUILayout.ExpandWidth(false));
  79. GUI.color = Color.white;
  80. return result;
  81. }
  82. }
  83. }