unity脚本收集
This commit is contained in:
1 parent
ab57544ef5
commit
d88804cd63
16 files changed
+1325
-1
No files matched your search
@@ -0,0 +1,43 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
public class AddParentToSelected : Editor
|
||||
{
|
||||
[MenuItem("Tools/Add Parent To Selected Objects")]
|
||||
public static void AddParentForSelectedObjects()
|
||||
{
|
||||
GameObject[] selectedObjects = Selection.gameObjects;
|
||||
|
||||
if (selectedObjects.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("No objects selected. Please select objects in the Hierarchy.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (GameObject obj in selectedObjects)
|
||||
{
|
||||
int siblingIndex = obj.transform.GetSiblingIndex();
|
||||
Transform parent = obj.transform.parent;
|
||||
|
||||
GameObject newParent = new GameObject($"{obj.name}_Root");
|
||||
Undo.RegisterCreatedObjectUndo(newParent, "Create Parent Object");
|
||||
if (parent != null)
|
||||
{
|
||||
newParent.transform.SetParent(parent);
|
||||
}
|
||||
newParent.transform.position = obj.transform.position;
|
||||
newParent.transform.rotation = obj.transform.rotation;
|
||||
newParent.transform.localScale = obj.transform.localScale;
|
||||
|
||||
newParent.transform.position = obj.transform.position;
|
||||
|
||||
|
||||
Undo.SetTransformParent(obj.transform, newParent.transform, "Set New Parent");
|
||||
newParent.transform.SetSiblingIndex(siblingIndex);
|
||||
obj.transform.localRotation = Quaternion.identity;
|
||||
obj.transform.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
Debug.Log("New parent objects created for selected objects.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user