Last active
August 27, 2018 13:32
-
-
Save TsubameUnity/339ac26ef9faa8788746912175042a12 to your computer and use it in GitHub Desktop.
http://tsubakit1.hateblo.jp/entry/2017/04/28/005237 の処理改良版(プレハブがプレハブ参照のまま複製可能)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
public class ObjectDeplicate { | |
[MenuItem("Edit/DummyDeplicate %d", false, -1)] | |
static void CreateEmptyObject() { | |
var select = Selection.objects; | |
// Projectのオブジェクトのコピー | |
var project = Selection.objects.Where(obj => AssetDatabase.GetAssetPath(obj) != string.Empty); | |
foreach (var obj in project) { | |
string path = AssetDatabase.GetAssetPath(obj); | |
var newPath = AssetDatabase.GenerateUniqueAssetPath(path); | |
AssetDatabase.CopyAsset(path, newPath); | |
} | |
// ヒエラルキーのコピー | |
var prefab = Selection.objects.Except(project).Except(scene).ToArray(); | |
foreach (var obj in prefab) { | |
// Selection.activeObjectなど単体での差し替えでは切り替わらない | |
Selection.objects = new GameObject[] { obj as GameObject }; | |
Unsupported.DuplicateGameObjectsUsingPasteboard(); | |
// ここで参照がコピー先に切り替わる | |
var gameObject = Selection.objects[0] as GameObject; | |
int start = gameObject.name.LastIndexOf(" ("); | |
gameObject.name = gameObject.name.Substring(0, start); | |
gameObject.transform.SetSiblingIndex((obj as GameObject).transform.GetSiblingIndex() + 1); | |
Undo.RegisterCreatedObjectUndo(gameObject, "deplicate"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment