繁体   English   中英

当在统一 c# 中按下编辑按钮时,如何让用户与 object 交互?

[英]How to let user interact with the object when edit button is pressed in unity c#?

I do need some help to implement a way for the user to interact with the model like for example when the edit button is pressed I want the user to select on a part on the object that can highlight in yellow on unity with c#. 有没有办法或任何逻辑? 如果你不明白我想要实现什么,请告诉我。 谢谢你的帮助。

这是一些代码:

    public class BlowupController : MonoBehaviour {



    public void Editcomponents()
    {
        // let user interact when edit button (script) is called. 
          ClearText();
        RemoveStatus = !RemoveStatus;
        var imgs = reticle.GetComponentsInChildren<Image>(true);
        if (RemoveStatus)
        {
            foreach (var img in imgs)
            {
                img.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
            }
        }
        else
        {
            foreach (var img in imgs)
            {
                img.GetComponent<Image>().color = new Color32(255, 255, 255, 255);
            }
        }



    }




    public void ResetComponents(GameObject Remove)
    {
        Debug.Log("HI"); ;
        Debug.Log(value);
        ClearText();
        foreach (Animator ani in AnimotorList)
        {
            ani.gameObject.SetActive(true);
            ani.SetBool("Start", value);
        }
        if (RemoveStatus)
        {
            var imgs = reticle.GetComponentsInChildren<Image>(true);
            foreach (var img in imgs)
            {
                img.GetComponent<Image>().color = new Color32(255, 255, 255, 255);
            }
            var on = Remove.transform.Find("Remove_On");
            on.gameObject.SetActive(true);
            var off = Remove.transform.Find("Remove_Off");
            off.gameObject.SetActive(false);
            RemoveStatus = !RemoveStatus;
        }        
        ResetPanel.SetActive(false);
    }
    public void UndoRemovingComponent(GameObject Remove)
    {
        if (UndoList.Count > 0)
        {
            ClearText();
            var ob = UndoList[UndoList.Count - 1];
            ob.SetActive(true);
            UndoList.Remove(ob);
            if (UndoList.Count == 0)
            {
                var imgs = reticle.GetComponentsInChildren<Image>(true);
                foreach (var img in imgs)
                {
                    img.GetComponent<Image>().color = new Color32(255, 255, 255, 255);
                }
                var on = Remove.transform.Find("Remove_On");
                on.gameObject.SetActive(true);
                var off = Remove.transform.Find("Remove_Off");
                off.gameObject.SetActive(false);
                ResetPanel.SetActive(false);
                UndoPanel.SetActive(false);
                RemoveStatus = !RemoveStatus;
            }            

        }
    }



     private IEnumerator Start()
    {
        yield return new WaitForSeconds(initialTimeDelay);
        if (reticle == null)
        {
            reticle = FindObjectOfType<Reticle>().gameObject;
        }
        TransformSync();        


    }


    }



} 

我不完全理解您的问题,但如果您想知道用户是否/何时按下某个键,您可以使用 Input.GetKeyDown()。 例子

if (Input.GetKeyDown("space"))
        {
            print("space key was pressed");
        }

或者

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            print("space key was pressed");
        }
    }

在这里,您将空格替换为您要检查的键。 完整文档可在 Unity 文档https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html上找到

首先,由于你的 object 被分成不同的部分,创建一个名为“CheckIfSelected”或类似的脚本,然后在更新方法中,使用 raycast 检查 object 是否被点击。 单击 object 后,获取 object 上的材料,然后更改颜色:(抱歉格式和任何可能的错别字,我没有使用 ide)

function Update () {
         if (Input.GetMouseButtonDown(0)) {
             var hit: RaycastHit;
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

             if (Physics.Raycast(ray, hit)) {
                 if (hit.transform.gameObject == gameObject ){
                      gameObject.renderer.material.color = new Color(1,1,1);
                 }
             }
         }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM