using UnityEngine;
using cakeslice;

public class MoxingControl : MonoBehaviour
{
    private Transform selectedPart = null;
    private Outline outline = null;
    public GameObject axis;
    private Vector3 axisOffset = Vector3.zero;
    private Vector3 mouseOffset = Vector3.zero;
    private bool isControlEnabled = false;
    private Color selectedColor = Color.yellow;
    private Animator animator;
    private bool animatorEnabled = true;

    private void Update()
    {
        animator = GetComponent<Animator>();
        if (!isControlEnabled)
            return;

        if (Input.GetMouseButtonDown(0))
        {
            Transform clickedPart = GetClickedPart(transform);
            if (clickedPart != null)
            {
                if (selectedPart != null)
                {
                    Destroy(outline);
                    if (selectedPart == clickedPart)
                    {
                        selectedPart = null;
                        axis.SetActive(false);
                        return;
                    }

                }

                selectedPart = clickedPart;
                outline = selectedPart.gameObject.AddComponent<Outline>();
                //outline.OutlineColor = selectedColor;
                //outline.OutlineWidth = 2;

                axis.SetActive(true);
                axis.transform.position = selectedPart.position + Vector3.up * 2f;
                axisOffset = axis.transform.position - selectedPart.position;
                mouseOffset = selectedPart.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);

            }
            else
            {
                if (selectedPart != null)
                {
                    Destroy(outline);
                    selectedPart = null;
                    axis.SetActive(false);
                }

            }

        }
        if (Input.GetMouseButton(0) && selectedPart != null)
        {
            Vector3 mousePosition = Input.mousePosition;
            mousePosition.z = axisOffset.z;
            Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition) + mouseOffset;
            Vector3 localPosition = axis.transform.InverseTransformPoint(worldMousePosition - axisOffset);
            selectedPart.position = axis.transform.TransformPoint(localPosition);
        }

    }

    private bool IsDescendant(Transform child, Transform parent)
    {
        foreach (Transform t in parent)
        {
            if (t == child)
                return true;
            if (IsDescendant(child, t))
                return true;
        }
        return false;
    }

    private Transform GetClickedPart(Transform parent)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            Transform clickedPart = hit.transform;
            if (IsDescendant(clickedPart, parent))
            {
                return clickedPart;
            }
        }
        return null;
    }

    public void ToggleControlEnabled()
    {
        isControlEnabled = !isControlEnabled;
        animator.enabled = !animator.enabled;
    }
}修改代码：public一个已经存在的坐标轴模型，点击模型子零件时，坐标轴模型移动到被点击的模型零件上，一直显示，并且坐标轴模型要变为被点击零件的父物体，通过鼠标点击坐标轴在三个轴的方向上移动。