using UnityEngine; using cakeslice; // 使用 Outline 插件 public class MoxingControl :…
- User
using UnityEngine; using cakeslice; // 使用 Outline 插件
public class MoxingControl : MonoBehaviour { private Transform selectedPart = null; // 当前选中的零件 private Outline outline = null; // 选中零件的 Outline 组件 private Color selectedColor = Color.yellow; // 选中零件的颜色 private bool isControlEnabled = false; // 模型控制是否启用 private Animator animator; // Animator 组件 private bool animatorEnabled = true; // Animator 是否启用 public GameObject axisPrefab; // 坐标轴预制体
private GameObject axisObj; // 当前选中零件的坐标轴对象 private bool axisVisible = false; // 坐标轴是否可见 private float axisOffset = 0f; // 坐标轴距离选中零件的偏移量
public void ToggleControlEnabled() { // 切换模型控制状态 isControlEnabled = !isControlEnabled; animator.enabled = !animator.enabled; }
private void Update() { animator = GetComponent(); if (!isControlEnabled) return; // 如果模型控制未启用,直接返回 if (Input.GetMouseButtonDown(0)) { // 鼠标左键点击选择零件 Transform clickedPart = GetClickedPart(transform); if (clickedPart != null) { // 如果点击到零件 if (selectedPart != null) { // 如果已有选中的零件 Destroy(outline); // 删除旧的 Outline 组件 if (selectedPart == clickedPart) { // 如果选中的是同一个零件 selectedPart = null; HideAxis(); return; } } selectedPart = clickedPart; // 选中新的零件 outline = selectedPart.gameObject.AddComponent(); // 添加新的 Outline 组件 // outline.OutlineColor = selectedColor; // 设置选中零件的颜色 // outline.OutlineWidth = 4; // 设置轮廓宽度
ShowAxis(selectedPart); // 显示坐标轴 } else if (selectedPart != null) { // 如果点击空白处,取消选中状态 Destroy(outline); selectedPart = null; HideAxis(); } } if (Input.GetMouseButton(0) && selectedPart != null) { // 鼠标左键拖动选中零件 float y= Input.GetAxis(“Mouse X”) / 100f; float z= Input.GetAxis(“Mouse Y”) / 100f; float x= Input.GetAxis(“Mouse ScrollWheel”) * 0.5f; Vector3 offset = new Vector3(x, y, z); axisObj.transform.position += offset; // 移动坐标轴 } }
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) { // 判断物体是否是指定父物体的子物体 foreach (Transform t in parent) { if (t == child) return true; if (IsDescendant(child, t)) return true; } return false; }
private void ShowAxis(Transform part) { if (axisObj != null) Destroy(axisObj); // 删除旧的坐标轴对象 axisObj = Instantiate(axisPrefab, part.position + part.up * axisOffset, part.rotation, part); // 创建新的坐标轴对象并设置为当前选中零件的子物体 axisVisible = true; }
private void HideAxis() { if (!axisVisible) return; // 如果坐标轴已经隐藏,直接返回 if (axisObj != null) Destroy(axisObj); // 删除坐标轴对象 axisVisible = false; }将创造的坐标轴模型删去,改为每次点击选中零件,将public的坐标轴模型移到被选中模型上,将坐标轴作为被选中零件的父物体。 }将其改为
- Assistant
使用Unity自带的Gizmos绘制坐标轴。具体实现如下:
using UnityEngine;
public class MoxingControl : MonoBehaviour { private Transform selectedPart = null; // 当前选中的零件 private Color selectedColor = Color.yellow; // 选中零件的颜色 private bool isControlEnabled = false; // 模型控制是否启用 private Animator animator; // Animator 组件 private bool animatorEnabled = true; // Animator 是否启用
public void ToggleControlEnabled() { // 切换模型控制状态 isControlEnabled = !isControlEnabled; animator.enabled = !animator.enabled; } private void OnDrawGizmosSelected() { if (selectedPart != null) { Gizmos.color = Color.red; Gizmos.DrawLine(selectedPart.position, selectedPart.position + selectedPart.right * 0.5f); Gizmos.color = Color.green; Gizmos.DrawLine(selectedPart.position, selectedPart.position + selectedPart.up * 0.5f); Gizmos.color = Color.blue; Gizmos.DrawLine(selectedPart.position, selectedPart.position + selectedPart.forward * 0.5f); } } private void Update() { animator = GetComponent<Animator>(); if (!isControlEnabled) return; // 如果模型控制未启用,直接返回 if (Input.GetMouseButtonDown(0)) { // 鼠标左键点击选择零件 Transform clickedPart = GetClickedPart(transform); if (clickedPart != null) { // 如果点击到零件 if (selectedPart != null) { // 如果已有选中的零件 if (selectedPart == clickedPart) { // 如果选中的是同一个零件 selectedPart = null; return; } } selectedPart = clickedPart; // 选中新的零件 } else if (selectedPart != null) { // 如果点击空白处,取消选中状态 selectedPart = null; } } if (Input.GetMouseButton(0) && selectedPart != null) { // 鼠标左键拖动选中零件 float y = Input.GetAxis("Mouse X") / 100f; float z = Input.GetAxis("Mouse Y") / 100f; float x = Input.GetAxis("Mouse ScrollWheel") * 0.5f; Vector3 offset = new Vector3(x, y, z); selectedPart.position += offset; // 移动选中零件 } } 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) { // 判断物体是否是指定父物体的子物体 foreach (Transform t in parent) { if (t == child) return true; if (IsDescendant(child, t)) return true; } return false; }}
注意,在Unity中需要将该脚本组件添加到要控制的模型上才能生效。同时,需要在场景中将要控制的模型选中,否则无法将Gizmos绘制在该模型上。