简体   繁体   中英

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#. 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(). 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

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. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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