using UnityEngine;
using cakeslice;
using UnityEngine.EventSystems;

public class MoxingControl : MonoBehaviour
{
private Transform selectedPart = null;
private Outline outline = null;
private 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;

public GameObject axisPrefab;

private bool isDraggingAxis = false;
private Vector3 axisDir = Vector3.zero;

private void Start()
{
axis = Instantiate(axisPrefab, Vector3.zero, Quaternion.identity);
axis.SetActive(false);
}

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.SetParent(transform);
axis.transform.position = selectedPart.position;
axisOffset = selectedPart.position - axis.transform.position;
mouseOffset = selectedPart.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);

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

}
}

private void LateUpdate()
{
if (isDraggingAxis && selectedPart != null)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(selectedPart.position);
Vector3 mousePos = Input.mousePosition;
mousePos.z = screenPos.z;
Vector3 worldDelta = Camera.main.ScreenToWorldPoint(mousePos) - selectedPart.position;

if (axisDir == Vector3.right)
selectedPart.position += Vector3.Project(worldDelta, selectedPart.right);
else if (axisDir == Vector3.up)
selectedPart.position += Vector3.Project(worldDelta, selectedPart.up);
else if (axisDir == Vector3.forward)
selectedPart.position += Vector3.Project(worldDelta, selectedPart.forward);

axis.transform.position = selectedPart.position - axisOffset;
}
}

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;
}

private bool IsDescendant(Transform child, Transform parent)
{
while (child != null)
{
if (child == parent)
return true;
child = child.parent;
}
return false;
}

public void ToggleControlEnabled()
{
isControlEnabled = !isControlEnabled;
animator.enabled = !animator.enabled;
}

public void OnAxisMouseDown(string axisName)
{
switch (axisName)
{
case “XAxis”:
axisDir = transform.right;
break;
case “YAxis”:
axisDir = transform.up;
break;
case “ZAxis”:
axisDir = transform.forward;
break;
default:
axisDir = Vector3.zero;
break;
}
isDraggingAxis = true;
}

public void OnAxisMouseUp()
{
isDraggingAxis = false;
axisDir = Vector3.zero;
}
}


public class AxisController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public MoxingControl moxingControl;

public void OnPointerDown(PointerEventData eventData)
{
moxingControl.OnAxisMouseDown(gameObject.name);
}

public void OnPointerUp(PointerEventData eventData)
{
moxingControl.OnAxisMouseUp();
}
}请将这两段代码和为一段