Exemplo n.º 1
0
    private void AlignmentInspector()
    {
        EditorGUILayout.LabelField("Alignment", EditorStyles.boldLabel);
        alignTo = (AlignToType)EditorGUILayout.EnumPopup("Align to", alignTo);
        alignmentAxis = (AxisFlag)EditorGUILayout.EnumMaskField("Axis", alignmentAxis);

        string buttonLabel = "Select another object to align to";
        bool enableButton = false;
        Transform[] selectedTransforms = Selection.transforms;
        if (selectedTransforms.Length > 1)
        {
            if (alignTo == AlignToType.lastSelected)
            {
                buttonLabel = "Align to " + selectedTransforms[selectedTransforms.Length - 1].name;
            }
            else
            {
                buttonLabel = "Align to " + selectedTransforms[0].name;
            }
            enableButton = true;
        }

        GUI.enabled = enableButton;
        if (GUILayout.Button(buttonLabel))
        {
            AlignTo(alignTo, alignmentAxis);
        }
        GUI.enabled = true;
    }
Exemplo n.º 2
0
    private void AlignTo(AlignToType to , AxisFlag axis)
    {
        Transform[] selectedTransforms = Selection.transforms;

        int targetIndex = 0;
        if (to == AlignToType.lastSelected)
            targetIndex = selectedTransforms.Length - 1;

        for (int i = 0; i < selectedTransforms.Length; i++)
        {
            if (i == targetIndex)
                continue;

            Vector3 temp = selectedTransforms[i].position;

            if ((axis & AxisFlag.X) == AxisFlag.X)
                temp.x = selectedTransforms[targetIndex].position.x;

            if ((axis & AxisFlag.Y) == AxisFlag.Y)
                temp.y = selectedTransforms[targetIndex].position.y;

            if ((axis & AxisFlag.Z) == AxisFlag.Z)
                temp.z = selectedTransforms[targetIndex].position.z;

            Undo.RecordObject(selectedTransforms[i], selectedTransforms[i].name +  " aligned to " + selectedTransforms[targetIndex].name);
            selectedTransforms[i].position = temp;
        }
    }