Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

写一段unity代码,模型拆装部分,通过鼠标左键点击模型的子零件进行选中,为其添加一个outline组件,再为其添加一个坐标轴模型(已存在,声明即可),坐标轴模型作为被点击子零件…

  1. User

    写一段unity代码,模型拆装部分,通过鼠标左键点击模型的子零件进行选中,为其添加一个outline组件,再为其添加一个坐标轴模型(已存在,声明即可),坐标轴模型作为被点击子零件的父物体,坐标轴出现在被点击零部件的上方,通过鼠标点击坐标轴拖拽模型零部件进行x、y、z方向的位移。

  2. Assistant

    using UnityEngine; using System.Collections;

    public class ModelDisassembly : MonoBehaviour {

    public GameObject outlinePrefab; // 轮廓线预制件
    public GameObject axisPrefab; // 坐标轴预制件
    
    private GameObject selectedPart; // 被选中的零件
    private GameObject axisObj; // 坐标轴
    
    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) { 
                if (hit.collider.gameObject != gameObject) {
                    // 点击到模型子零件
                    GameObject clickedPart = hit.collider.gameObject;
                    if (selectedPart != clickedPart) {
                        // 取消旧的轮廓线
                        if (selectedPart != null) {
                            selectedPart.GetComponent<Outline>().enabled = false;
                        }
                        // 添加新的轮廓线
                        selectedPart = clickedPart;
                        selectedPart.GetComponent<Outline>().enabled = true;
                        // 添加坐标轴
                        AddAxis();
                    }
                }
            }
            else {
                // 点击到空白处
                if (selectedPart != null) {
                    selectedPart.GetComponent<Outline>().enabled = false;
                    selectedPart = null;
                }
                if (axisObj != null) {
                    Destroy(axisObj);
                }
            }
        }
        else if (Input.GetMouseButton(0)) {
            // 拖拽移动零件
            if (axisObj != null) {
                Vector3 offset = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, axisObj.transform.position.z)) -
                    axisObj.transform.parent.transform.position;
                selectedPart.transform.position += offset;
            }
        }
    }
    
    private void AddAxis() {
        if (axisObj != null) {
            Destroy(axisObj);
        }
        // 创建坐标轴
        axisObj = Instantiate(axisPrefab, selectedPart.transform.position, Quaternion.identity) as GameObject;
        axisObj.transform.SetParent(selectedPart.transform);
        // 设置坐标轴位置
        Vector3 axisPos = axisObj.transform.localPosition;
        axisPos.y = selectedPart.GetComponent<MeshRenderer>().bounds.max.y + axisObj.transform.localScale.y / 2;
        axisObj.transform.localPosition = axisPos;
    }
    

    }

  3. User

    using UnityEngine; using cakeslice; public class MoxingControl : MonoBehaviour { private Transform selectedPart = null; private Outline outline = null; private Color selectedColor = Color.yellow; private bool isControlEnabled = false; private GameObject axis = null; private Vector3 axisOffset = Vector3.zero; private Vector3 mouseOffset = Vector3.zero; private Animator animator; private bool animatorEnabled = true; 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); if (selectedPart == clickedPart) { selectedPart = null; if (axis != null) Destroy(axis); return; } } selectedPart = clickedPart; outline = selectedPart.gameObject.AddComponent(); // outline.OutlineColor = selectedColor; // outline.OutlineWidth = 4; if (axis == null) axis = Instantiate(Resources.Load(“Axis”) as GameObject, selectedPart); axis.transform.localPosition = Vector3.zero; axis.SetActive(true); axisOffset = axis.transform.position - selectedPart.position; mouseOffset = selectedPart.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, axisOffset.z)); } else if (selectedPart != null) { Destroy(outline); selectedPart = null; if (axis != null) axis.SetActive(false); } } if (Input.GetMouseButton(0) && selectedPart != null && axis != null) { Vector3 mousePosition = Input.mousePosition; mousePosition.z = axisOffset.z; Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition) + mouseOffset; Vector3 localPos = selectedPart.parent.InverseTransformPoint(worldMousePosition - axisOffset); localPos.x = axis.transform.localEulerAngles.y == 90 ? -localPos.x : localPos.x; localPos.y = axis.transform.localEulerAngles.x == 90 ? -localPos.y : localPos.y; localPos.z = axis.transform.localEulerAngles.y == 0 ? -localPos.z : localPos.z; selectedPart.localPosition = new Vector3( Mathf.Round(localPos.x * 100) / 100, Mathf.Round(localPos.y * 100) / 100, Mathf.Round(localPos.z * 100) / 100 ); } } 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; } }修改这段代码,坐标轴模型已经存在,声明一个坐标轴模型即可,并且坐标轴模型出现在被点击选中模型的上方一点的位置。请将完整的代码写出

  4. Assistant

    using UnityEngine; using cakeslice;

    public class ModelController : 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 = true; private Color selectedColor = Color.yellow;

    public GameObject axisPrefab;
    
    private void Start() {
        axis = Instantiate(axisPrefab, Vector3.zero, Quaternion.identity);
        axis.SetActive(false);
    }
    
    private void Update(){
        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(selectedPart.transform);
                axis.transform.localPosition = new Vector3(0, selectedPart.GetComponent<Renderer>().bounds.extents.y + 0.1f, 0);
                axisOffset = selectedPart.transform.position - axis.transform.position;
                mouseOffset = selectedPart.transform.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 = selectedPart.parent.InverseTransformPoint(worldMousePosition - axisOffset);
            localPosition.x = axis.transform.localEulerAngles.y == 90 ? -localPosition.x : localPosition.x;
            localPosition.y = axis.transform.localEulerAngles.x == 90 ? -localPosition.y : localPosition.y;
            localPosition.z = axis.transform.localEulerAngles.y == 0 ? -localPosition.z : localPosition.z;
            selectedPart.localPosition = new Vector3(Mathf.Round(localPosition.x * 100) / 100, Mathf.Round(localPosition.y * 100) / 100, Mathf.Round(localPosition.z * 100) / 100);
        }
    
    }
    
    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;
    }
    

    }

  5. User

    using UnityEngine; using cakeslice;

    public class ModelController : 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 = true; private Color selectedColor = Color.yellow;

    public GameObject axisPrefab;

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

    private void Update(){ 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.OutlineColor = selectedColor; outline.OutlineWidth = 2;

    axis.SetActive(true); axis.transform.SetParent(selectedPart.transform); axis.transform.localPosition = new Vector3(0, selectedPart.GetComponent().bounds.extents.y + 0.1f, 0); axisOffset = selectedPart.transform.position - axis.transform.position; mouseOffset = selectedPart.transform.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 = selectedPart.parent.InverseTransformPoint(worldMousePosition - axisOffset); localPosition.x = axis.transform.localEulerAngles.y == 90 ? -localPosition.x : localPosition.x; localPosition.y = axis.transform.localEulerAngles.x == 90 ? -localPosition.y : localPosition.y; localPosition.z = axis.transform.localEulerAngles.y == 0 ? -localPosition.z : localPosition.z; selectedPart.localPosition = new Vector3(Mathf.Round(localPosition.x * 100) / 100, Mathf.Round(localPosition.y * 100) / 100, Mathf.Round(localPosition.z * 100) / 100); }

    }

    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; } }添加一个开关进行整段代码功能的控制。

  6. Assistant

    在代码中新增一个 ToggleControlEnabled 函数,当调用该函数时,将 isControlEnabled 取反,以开启和关闭控制功能。修改其他函数中的代码,只有当 isControlEnabled 为 true 才执行相应的操作。

    修改后的代码如下:

    using UnityEngine;
    using cakeslice;
    
    public class ModelController : 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;
    
        public GameObject axisPrefab;
    
        private void Start() {
            axis = Instantiate(axisPrefab, Vector3.zero, Quaternion.identity);
            axis.SetActive(false);
        }
    
        private void Update(){
            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(selectedPart.transform);
                    axis.transform.localPosition = new Vector3(0, selectedPart.GetComponent<Renderer>().bounds.extents.y + 0.1f, 0);
                    axisOffset = selectedPart.transform.position - axis.transform.position;
                    mouseOffset = selectedPart.transform.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 = selectedPart.parent.InverseTransformPoint(worldMousePosition - axisOffset);
                localPosition.x = axis.transform.localEulerAngles.y == 90 ? -localPosition.x : localPosition.x;
                localPosition.y = axis.transform.localEulerAngles.x == 90 ? -localPosition.y : localPosition.y;
                localPosition.z = axis.transform.localEulerAngles.y == 0 ? -localPosition.z : localPosition.z;
                selectedPart.localPosition = new Vector3(Mathf.Round(localPosition.x * 100) / 100, Mathf.Round(localPosition.y * 100) / 100, Mathf.Round(localPosition.z * 100) / 100);
            }
    
        }
    
        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;
        }
    }