Skip to content

Instantly share code, notes, and snippets.

@ysalihtuncel
Created May 26, 2025 08:18
Show Gist options
  • Save ysalihtuncel/3daaa8ff1ecb9937c806c2122849b7ed to your computer and use it in GitHub Desktop.
Save ysalihtuncel/3daaa8ff1ecb9937c806c2122849b7ed to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
public static class CenterPivotContextMenu
{
[MenuItem("CONTEXT/Transform/Center Pivot (Create Parent)")]
private static void CenterPivot(MenuCommand command)
{
Transform selectedTransform = (Transform)command.context;
GameObject selected = selectedTransform.gameObject;
MeshFilter meshFilter = selected.GetComponent<MeshFilter>();
if (meshFilter == null || meshFilter.sharedMesh == null)
{
Debug.LogWarning("Seçilen objede geçerli bir MeshFilter bulunamadı.");
return;
}
Mesh mesh = meshFilter.sharedMesh;
Bounds bounds = mesh.bounds;
Vector3 centerLocal = bounds.center;
Vector3 centerWorld = selected.transform.TransformPoint(centerLocal);
var _parent = selected.transform.parent;
GameObject parent = new GameObject($"{selected.name}_PivotCentered");
Undo.RegisterCreatedObjectUndo(parent, "Create Centered Parent");
parent.transform.position = centerWorld;
parent.transform.rotation = selected.transform.rotation;
parent.transform.localScale = selected.transform.localScale;
Undo.SetTransformParent(selected.transform, parent.transform, "Reparent Object");
parent.transform.SetParent(_parent);
Debug.Log($"'{selected.name}' objesi için pivot ortalanmış yeni parent oluşturuldu.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment