FlaShG / GitMerge-for-Unity

Unity plugin for merging sceneand prefabs files when using git.
GNU General Public License v2.0
197 stars 27 forks source link

Support for RectTransforms & Focus on Prefabs #67

Open Dak0r opened 1 year ago

Dak0r commented 1 year ago

If a conflict happens in a prefab that is a ui element (has a RectTransform), the prefab is not visible in the Scene View as it's missing a parent canvas. Workaround: You can add a canvas manually and make the prefab instance a child to work around it.

Furthermore the "Focus" function doesn't work correctly (~for RectTransforms?~ it's actually broken for all prefabs). It's always focussing on the root game object, instead the one that was changed. Workaround: You have to re-focus manually by finding & double clicking the gameObject in the Hierarchy.

Expected:

Dak0r commented 1 year ago

The focus issue doesn't seem to be a bug related to RectTransforms, but rather an issue with prefabs in general.

I managed to get the desired behaviour with this "hacked" code, the current version always passed along the root object ourPrefabInstance

            var objectToHighlight = ours;
            if (MergeManagerBase.isMergingPrefab) {
                string path = GetPath(ours);
                path = path.Substring(path.IndexOf("/", 1) + 1);
                objectToHighlight = MergeManagerPrefab.ourPrefabInstance.transform.Find(path).gameObject;
            }
            if (objectToHighlight && inMergePhase && objectToHighlight.hideFlags == HideFlags.None)
            {
                objectToHighlight.Highlight();
            }

(GetPath was copied over from GameObjectMergeActions.cs )

And in GameObjectExtension.cs :

        public static void Highlight(this GameObject go)
        {
            // Focussing on the same object twice, zooms in to the coordinate instead of the bounding box.
            if (Selection.activeObject == go) {
                return;
            }
        ...

Edit: Appending to the existing comment to not spam everyones inboxes I implemented the automatic creation of a Canvas for Ui Element Prefabs:

In MergeManagerPrefab.cs, in line 52ff:

            // Instantiate our object in order to view it while merging.
            ourPrefabInstance = PrefabUtility.InstantiatePrefab(ourPrefab) as GameObject;

            // UI Elements need a Canvas to be displayed correctly:
            if (ourPrefabInstance.GetComponentInChildren<RectTransform>() != null) {
                GameObject defaultCanvas = new GameObject("Canvas", typeof(Canvas));
                ourPrefabInstance.transform.SetParent(defaultCanvas.transform, false);
            }

PS: Don't worry I'll create PR with (cleaner variants of) these changes 😄