https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ebc72342-f91c-4439-afac-9ab9ead6ee15/Untitled.png

Unity has a very annoying issue that duplicate object will put your new duplicates at bottom of hierarchy

So, here I found a code in this forum post to solve this issue, and I rewrite asset duplicate part and renew some legacy function This tool really saved my life especially I used to sort my hierarchy with different usage

<aside> 💡 See also [Tool] Color Hierarchy Highlighter to make better sorting of your hierarchy

</aside>

Source Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public static class EditorDuplicateCommand
{
    [MenuItem("Edit/Smart Duplicate %d", false, 0)]
    private static void DuplicateSelection()
    {
        var newSelection = new List<Object>();
        if (Selection.gameObjects.Length > 0)
        {
            for (int ui = 0; ui < Selection.gameObjects.Length; ui++)
            {
                var go = Selection.gameObjects[ui];
                int siblingIndex = go.transform.GetSiblingIndex();
                var newGo = SmartInstantiate(go);
                newGo.transform.parent = go.transform.parent;
                newGo.transform.position = go.transform.position;
                newGo.transform.SetSiblingIndex(siblingIndex + 1);
                newGo.name = go.name;
                newSelection.Add(newGo);
            }
        }
        else
        {
            for (int ui = 0; ui < Selection.objects.Length; ui++)
            {
                var go = Selection.objects[ui];
                var newGo = DuplicateAsset(go);
                newSelection.Add(newGo);
            }
        }
        Selection.objects = newSelection.ToArray();
    }

    private static GameObject SmartInstantiate(GameObject go)
    {
        if (PrefabUtility.GetPrefabInstanceStatus(go) == PrefabInstanceStatus.Connected)
        {
            var prefab = PrefabUtility.GetCorrespondingObjectFromSource(go) as GameObject;
            var newGo = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
            var mods = PrefabUtility.GetPropertyModifications(go);
            PrefabUtility.SetPropertyModifications(newGo, mods);
            return newGo;
        }

        if (PrefabUtility.GetPrefabAssetType(go) != PrefabAssetType.NotAPrefab)
        {
            return DuplicateAsset(go) as GameObject;
        }

        return GameObject.Instantiate(go) as GameObject;
    }

    private static Object DuplicateAsset(Object targetAsset)
    {
        int index = 1;
        while (AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GetAssetPath(targetAsset).Replace(".", " " + index + ".")) != null)
        {
            index++;
            if (index > 100)
            {
                Debug.LogError("Massive Asset Duplicate Detect");
            }
        }
        AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(targetAsset), AssetDatabase.GetAssetPath(targetAsset).Replace(".", " " + index + "."));
        return AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GetAssetPath(targetAsset).Replace(".", " " + index + "."));
    }
}

How to use (Unity 2019.3)

  1. Create this code

  2. Press Ctrl+D and Unity will show this shortcut conflict window, chose Resolve Conflict

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7369f8ee-23cb-48ef-9dc5-8c7aefc287df/Untitled.png

  3. Shortcuts window will opened automatically (you can also find it in Edit→Shortcuts ) Remove one of them and move to other shortcut like Ctrl+Shift+D (I suggest keep native duplicate since current code still have few issue when duplicating non GameObject target)

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7db9ce6a-3d11-4c41-b106-4b4d1a5bc8a2/Untitled.png

  4. Enjoy new duplicate experience!

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/166842a3-3b75-4cf6-93ec-7b32699921d1/TEMP.gif

<aside> 💡 If you want to change back default Duplicate, just go to Shortcuts Window and assign Duplicate function back

</aside>


Support This Page!

         If you like my article, follow my [**Twitter**](<https://twitter.com/FrogskinShen>) or buy me a coffee to support my creation!
                  如果你喜歡我的文章,可以跟隨我的[**Twitter**](<https://twitter.com/FrogskinShen>)或請我喝杯咖啡來支持我 😀

https://html.cafe/x0a614731

                                                  Check Main Page for More Article
                                                           來主頁看看更多文章 

                                                [Froggy's Game Making Note](<https://frogskinnn.notion.site/Froggy-s-Game-Making-Note-3efe78e21eab4034a8af14fcdbde6a30>)