簡體   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