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 void ToggleControlEnabled() { // 切换模型控制状态 isControlEnabled = !isControlEnabled; animator.enabled = !animator.enabled; } 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); // 删除旧的 Outline 组件 if (selectedPart == clickedPart) { // 如果选中的是同一个零件 selectedPart = null; return; } } selectedPart = clickedPart; // 选中新的零件 outline = selectedPart.gameObject.AddComponent<Outline>(); // 添加新的 Outline 组件 // outline.OutlineColor = selectedColor; // 设置选中零件的颜色 // outline.OutlineWidth = 4; // 设置轮廓宽度 } else if (selectedPart != null) { // 如果点击空白处,取消选中状态 Destroy(outline); selectedPart = null; } } if (Input.GetMouseButton(0) && selectedPart != null) { // 鼠标左键拖动选中零件 Vector3 mousePosition = Input.mousePosition; mousePosition.z = Camera.main.WorldToScreenPoint(selectedPart.position).z; Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition); // 获取鼠标在世界坐标中的位置 selectedPart.position = worldMousePosition; // 移动选中零件 } } 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; }}修改代码:声明一个已经存在的坐标轴模型,点击零件时,在零件上方显示坐标轴。
- Assistant
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 是否启用 private GameObject coordinateAxis; // 坐标轴物体 private Transform axisInstance;
public void ToggleControlEnabled() { // 切换模型控制状态 isControlEnabled = !isControlEnabled; animator.enabled = !animator.enabled; } private void Start() { // 初始化坐标轴物体 coordinateAxis = Resources.Load<GameObject>("CoordinateAxis"); if (coordinateAxis == null) { Debug.LogError("Can't find CoordinateAxis prefab."); } } 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); // 删除旧的 Outline 组件 if (selectedPart == clickedPart) { // 如果选中的是同一个零件 selectedPart = null; if (axisInstance != null) Destroy(axisInstance.gameObject); // 删除坐标轴实例 return; } } selectedPart = clickedPart; // 选中新的零件 outline = selectedPart.gameObject.AddComponent<Outline>(); // 添加新的 Outline 组件 // outline.OutlineColor = selectedColor; // 设置选中零件的颜色 // outline.OutlineWidth = 4; // 设置轮廓宽度 if (axisInstance != null) Destroy(axisInstance.gameObject); // 删除旧的坐标轴实例 axisInstance = Instantiate(coordinateAxis, selectedPart.position, Quaternion.identity, selectedPart); // 实例化坐标轴 } else if (selectedPart != null) { // 如果点击空白处,取消选中状态 Destroy(outline); selectedPart = null; if (axisInstance != null) Destroy(axisInstance.gameObject); // 删除坐标轴实例 } } if (Input.GetMouseButton(0) && selectedPart != null) { // 鼠标左键拖动选中零件 Vector3 mousePosition = Input.mousePosition; mousePosition.z = Camera.main.WorldToScreenPoint(selectedPart.position).z; Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition); // 获取鼠标在世界坐标中的位置 selectedPart.position = worldMousePosition; // 移动选中零件 axisInstance.position = selectedPart.position; // 移动坐标轴实例 } } 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; }}
- 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 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; return; } } selectedPart = clickedPart; // 选中新的零件 outline = selectedPart.gameObject.AddComponent(); // 添加新的 Outline 组件 // outline.OutlineColor = selectedColor; // 设置选中零件的颜色 // outline.OutlineWidth = 4; // 设置轮廓宽度 } else if (selectedPart != null) { // 如果点击空白处,取消选中状态 Destroy(outline); selectedPart = null; } } if (Input.GetMouseButton(0) && selectedPart != null) { // 鼠标左键拖动选中零件 Vector3 mousePosition = Input.mousePosition; mousePosition.z = Camera.main.WorldToScreenPoint(selectedPart.position).z; Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition); // 获取鼠标在世界坐标中的位置 selectedPart.position = worldMousePosition; // 移动选中零件 } }
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; } }修改代码:public一个已经存在的坐标轴模型,点击零件时,在零件上方显示坐标轴。请写出完整代码
- Assistant
使用的是Unity 2020.3版本,以下是完整代码:
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 coordinateAxis; // 坐标轴物体 private GameObject axisInstance; // 坐标轴实例
public void ToggleControlEnabled() { // 切换模型控制状态 isControlEnabled = !isControlEnabled; animator.enabled = !animator.enabled; } private void Start() { animator = GetComponent<Animator>(); if (coordinateAxis == null) Debug.LogError("Can't find CoordinateAxis GameObject"); } private void Update() { 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; Destroy(axisInstance); // 删除坐标轴实例 return; } } selectedPart = clickedPart; // 选中新的零件 outline = selectedPart.gameObject.AddComponent<Outline>(); // 添加新的 Outline 组件 // outline.OutlineColor = selectedColor; // 设置选中零件的颜色 // outline.OutlineWidth = 4; // 设置轮廓宽度 axisInstance = Instantiate(coordinateAxis, selectedPart.position, Quaternion.identity, selectedPart); // 实例化坐标轴 } else if (selectedPart != null) { // 如果点击空白处,取消选中状态 Destroy(outline); selectedPart = null; Destroy(axisInstance); // 删除坐标轴实例 } } if (Input.GetMouseButton(0) && selectedPart != null) { // 鼠标左键拖动选中零件 Vector3 mousePosition = Input.mousePosition; mousePosition.z = Camera.main.WorldToScreenPoint(selectedPart.position).z; Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition); // 获取鼠标在世界坐标中的位置 selectedPart.position = worldMousePosition; // 移动选中零件 if (axisInstance != null) axisInstance.transform.position = selectedPart.position; // 移动坐标轴实例 } } 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; }}