📚AutoPivotSetting on 3DObject

public class AutoBoundPivot : MonoBehaviour
{
    public enum PivotDirection
    {
        Forward = 0,
        Backward,
        Bottom,
        Top,
        Left,
        Right,
    }

    public static void RollbackRootObject(GameObject rootObj)
    {
        var childObj = rootObj.transform.Find("Model").gameObject;

        var sharedMesh = childObj.GetComponent<MeshFilter>().sharedMesh;
        var meshFilter = UtilFunction.CopyComponent<MeshFilter>(childObj.GetComponent<MeshFilter>(), rootObj);

        var shardMaterials = childObj.GetComponent<MeshRenderer>().sharedMaterials;
        var meshRenderer = UtilFunction.CopyComponent<MeshRenderer>(childObj.GetComponent<MeshRenderer>(), rootObj);

        meshFilter.sharedMesh = sharedMesh;
        meshRenderer.sharedMaterials = shardMaterials;

        DestroyImmediate(rootObj.GetComponent<BoxCollider>());
        DestroyImmediate(childObj);

        rootObj.SetActive(false);
    }

    public static void SetChildObjByPivotDirection(PivotDirection dir, GameObject rootObj)
    {
        var rootBoxCol = rootObj.AddComponent<BoxCollider>();
        var childObj = Instantiate(rootObj, rootObj.transform);
        childObj.name = "Model";
        childObj.transform.localPosition = Vector3.zero;
        var childBoxCol = childObj.GetComponent<BoxCollider>();
        var bounds = childBoxCol.bounds;
        var fromObj = new GameObject("from");
        fromObj.transform.parent = childObj.transform;

        if (dir == PivotDirection.Forward)
        {
            fromObj.transform.localPosition = childBoxCol.center + Vector3.forward * bounds.size.z * 0.5f;
            childObj.transform.Rotate(90f, 0f, 0f); // objectė˜ front가 픾ëī‡ -> ė•žėœžëĄœ ė—Žė–īė ļėžˆëŠ”ęą°
            childObj.transform.localPosition = rootObj.transform.position - fromObj.transform.position;

            rootBoxCol.center = Vector3.up * bounds.size.z * 0.5f;
            rootBoxCol.size = new Vector3(bounds.size.x, bounds.size.z, bounds.size.y);
        }
        else if (dir == PivotDirection.Backward)
        {
            fromObj.transform.localPosition = childBoxCol.center + Vector3.back * bounds.size.z * 0.5f;
            childObj.transform.Rotate(-90f, 0f, 0f); // objectė˜ backėī 픾ëī‡ -> ë’Ī로 누ė› ėŒ.
            childObj.transform.localPosition = rootObj.transform.position - fromObj.transform.position;

            rootBoxCol.center = Vector3.up * bounds.size.z * 0.5f;
            rootBoxCol.size = new Vector3(bounds.size.x, bounds.size.z, bounds.size.y);
        }
        else if (dir == PivotDirection.Bottom)
        {
            fromObj.transform.localPosition = childBoxCol.center - Vector3.up * bounds.size.y * 0.5f;
            childObj.transform.Rotate(0f, 0f, 0f); // ęļ°ëģļėƒíƒœ ė„œėžˆėŒ.
            childObj.transform.localPosition = rootObj.transform.position - fromObj.transform.position;

            rootBoxCol.center = Vector3.up * bounds.size.y * 0.5f;
            rootBoxCol.size = new Vector3(bounds.size.x, bounds.size.y, bounds.size.z);
        }
        else if (dir == PivotDirection.Top)
        {
            fromObj.transform.localPosition = childBoxCol.center + Vector3.down * bounds.size.y * 0.5f;
            childObj.transform.Rotate(180f, 0f, 0f); // ėƒ 하 ë’Īė§‘ėŒ.
            childObj.transform.localPosition = rootObj.transform.position - fromObj.transform.position + Vector3.up * bounds.size.y;

            rootBoxCol.center = Vector3.up * bounds.size.y * 0.5f;
            rootBoxCol.size = new Vector3(bounds.size.x, bounds.size.y, bounds.size.z);
        }
        else if (dir == PivotDirection.Left)
        {
            fromObj.transform.localPosition = childBoxCol.center + Vector3.left * bounds.size.x * 0.5f;
            childObj.transform.Rotate(0f, 0f, 90f); // ė™žėŠ―ėī ė•„ëž˜ëĄœ 눕ęļ°.
            childObj.transform.localPosition = rootObj.transform.position - fromObj.transform.position;

            rootBoxCol.center = Vector3.up * bounds.size.x * 0.5f;
            rootBoxCol.size = new Vector3(bounds.size.y, bounds.size.x, bounds.size.z);
        }
        else if (dir == PivotDirection.Right)
        {
            fromObj.transform.localPosition = childBoxCol.center + Vector3.right * bounds.size.x * 0.5f;
            childObj.transform.Rotate(0f, 0f, -90f); // ė˜ĪëĨļėŠ―ėī ė•„ëž˜ëĄœ 눕ęļ°.
            childObj.transform.localPosition = rootObj.transform.position - fromObj.transform.position;

            rootBoxCol.center = Vector3.up * bounds.size.x * 0.5f;
            rootBoxCol.size = new Vector3(bounds.size.y, bounds.size.x, bounds.size.z);
        }

        // ë‹ĪëĨļ ėŧī폮넌íŠļ ė œęą° Transform, BoxCollider ė œė™ļ.
        foreach (var component in rootObj.GetComponents<Component>())
        {
            if (component == rootBoxCol || component == rootObj.transform) { continue; }
            DestroyImmediate(component);
        }
        DestroyImmediate(fromObj);
        DestroyImmediate(childObj.GetComponent<BoxCollider>());
        rootObj.SetActive(false);
    }
}

public class UtilFunction
{
    public static T CopyComponent<T>(T original, GameObject destination) where T : Component
    {
        System.Type type = original.GetType();
        var dst = destination.GetComponent(type) as T;
        if (!dst) dst = destination.AddComponent(type) as T;
        var fields = type.GetFields();
        foreach (var field in fields)
        {
            if (field.IsStatic) continue;
            field.SetValue(dst, field.GetValue(original));
        }
        var props = type.GetProperties();
        foreach (var prop in props)
        {
            if (!prop.CanWrite || !prop.CanWrite || prop.Name == "name") continue;
            prop.SetValue(dst, prop.GetValue(original, null), null);
        }
        return dst as T;
    }
}

Last updated