简体   繁体   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#. 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#. Is there way or any logic?有没有办法或任何逻辑? Let me know if you dont understand what I want to implement.如果你不明白我想要实现什么,请告诉我。 thanks for your help.谢谢你的帮助。

Here is some code:这是一些代码:

    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();        


    }


    }



} 

I do not fully understand your question, but if you are trying to know if/when a user presses a key, you can use Input.GetKeyDown().我不完全理解您的问题,但如果您想知道用户是否/何时按下某个键,您可以使用 Input.GetKeyDown()。 Example例子

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

OR或者

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

Here, you replace space with the key you wish to check.在这里,您将空格替换为您要检查的键。 Full documentation can be found on Unity docs https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html完整文档可在 Unity 文档https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html上找到

First of all, since your object is divided into separate parts, create a script called "CheckIfSelected" or something similar, then in the update method, use raycast to check if the object is clicked.首先,由于你的 object 被分成不同的部分,创建一个名为“CheckIfSelected”或类似的脚本,然后在更新方法中,使用 raycast 检查 object 是否被点击。 Once the object is clicked, get the material on the object, then change the color as such: (Sorry about the formatting and any possible typos, i am not using an ide)单击 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