写一段代码unity的代码,点击选中模型,将public的坐标轴模型加在… · The Chat Archive写一段代码unity的代码,点击选中模型,将public的坐标轴模型加在选中模型的圆心,将坐标轴设置为选中模型的父物体,将坐标轴模型的三个轴模型设置为三个轴方向,通过鼠标点击拖拽…
Download ▾
写一段代码unity的代码,点击选中模型,将public的坐标轴模型加在选中模型的圆心,将坐标轴设置为选中模型的父物体,将坐标轴模型的三个轴模型设置为三个轴方向,通过鼠标点击拖拽坐标轴的轴模型进行单一方向的移动。
Assistant
Assistant
using UnityEngine;
using cakeslice; // 使用 Outline 插件
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 是否启用
public GameObject axisPrefab; // 坐标轴预制体
code
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<Animator>();
if (!isControlEnabled) return; // 如果模型控制未启用,直接返回
if (Input.GetMouseButtonDown(0))
{ // 鼠标左
using UnityEngine;
public class SelectAxis : MonoBehaviour
{
public GameObject axisPrefab;
code
private Transform selectedObject;
private GameObject axisObject;
private float axisLength = 1f;
private bool isDragging = false;
private Vector3 lastMousePosition;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
selectedObject = hit.transform;
if (selectedObject != null)
{
CreateAxis();
}
}
}
if (axisObject != null)
{
if (isDragging)
{
DragAxis();
}
else
{
RotateAxis();
}
}
}
void CreateAxis()
{
if (axisObject != null)
{
Destroy(axisObject);
}
axisObject = Instantiate(axisPrefab, selectedObject.position, Quaternion.identity, selectedObject);
axisObject.transform.localScale = new Vector3(axisLength, axisLength, axisLength);
axisObject.transform.localRotation = Quaternion.identity;
AxisController axisController = axisObject.GetComponent<AxisController>();
if (axisController != null)
{
axisController.axisDirections[0] = selectedObject.right;
axisController.axisDirections[1] = selectedObject.up;
axisController.axisDirections[2] = selectedObject.forward;
}
}
void RotateAxis()
{
if (axisObject == null)
{
return;
}
for (int i = 0; i < 3; i++)
{
Transform axis = axisObject.transform.GetChild(i);
if (axis == null)
{
continue;
}
Vector3 direction = axisController.axisDirections[i];
Vector3 worldDirection = axisObject.transform.TransformDirection(direction);
Quaternion rotation = Quaternion.LookRotation(worldDirection);
axis.rotation = rotation;
}
}
void DragAxis()
{
if (axisObject == null)
{
return;
}
Vector3 mousePosition = Input.mousePosition;
Vector3 delta = mousePosition - lastMousePosition;
Vector3 axisDirection = axisObject.transform.GetChild(0).up;
selectedObject.position += axisDirection * delta.magnitude * Mathf.Sign(Vector3.Dot(delta, axisDirection));
lastMousePosition = mousePosition;
}
}
public class AxisController : MonoBehaviour
{
public Vector3[] axisDirections;
}
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; // 坐标轴预制体
code
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<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;
HideAxis();
return;
}
}
selectedPart = clickedPart; // 选中新的零件
outline = selectedPart.gameObject.AddComponent<Outline>(); // 添加新的 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);
selectedPart.position += selectedPart.transform.TransformDirection(offset); // 移动选中零件
if (axisObj != null) // 如果坐标轴对象存在
{
axisObj.transform.position = selectedPart.position + selectedPart.up * 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)
{ // 判断物体是否是指定父物体的子物体
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;
}
}根据这段代码改编:将模型子零件移动的代码删除,转为将显示的坐标轴模型作为选中模型的父物体,通过鼠标拖动坐标轴进行坐标轴方向的移动
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; // 坐标轴预制体
code
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<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;
HideAxis();
return;
}
}
selectedPart = clickedPart; // 选中新的零件
outline = selectedPart.gameObject.AddComponent<Outline>(); // 添加新的 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 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的坐标轴模型移到被选中模型上,将坐标轴作为被选中零件的父物体。
}将其改为
Not yet included in a published release.