Unity Event Trigger > Event Additional Questions.

Asked 2 years ago, Updated 2 years ago, 130 views

 public void CreateObject()
    {
        int randomType = UnityEngine.Random.Range(0, 2);

        GameObject temp = (GameObject)Instantiate(Resources.Load("Prefabs/Object"));

        int randomItem;
        temp.AddComponent<ItemScript>();
        ItemScript newItem = temp.GetComponent<ItemScript>();
        switch (randomType)
        {
            case 0:
                randomItem = UnityEngine.Random.Range(0, ObjectManager.Instance.ItemContainer.Foods.Count);
                newItem.Item = ObjectManager.Instance.ItemContainer.Foods[randomItem];

                break;
            case 1:
                randomItem = UnityEngine.Random.Range(0, ObjectManager.Instance.ItemContainer.Rides.Count);
                newItem.Item = ObjectManager.Instance.ItemContainer.Rides[randomItem];

                break;
        }
        temp.transform.SetParent(GameObject.Find("ObjectCanvas").gameObject.transform);
    }

Click a button to create an object. The object will be created and add the itemscript script as below. The itemscript contains the stored information depending on the item. Below is part of the itemscript.

  void Start()
    {
        transform.name = Item.ItemName;
        transform.localPosition = new Vector3(200, 100);
        actionPanel = transform.GetChild(2).gameObject;
        speechBubble = transform.GetChild(1).gameObject;

        for (int i = 0; i < transform.GetChild(2).childCount; i++)
        {
            string[] str = transform.GetChild(2).GetChild(i).name.Split( );
            string order = str[0];

            EventTrigger trigger = actionPanel.transform.GetChild(i).GetComponent<EventTrigger>();
            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = EventTriggerType.PointerClick;
            if (i == 0)
            {
                entry.callback.AddListener((eventData) => { EAT(); });
                trigger.triggers.Add(entry);
            }
            else if (i == 1)
            {
                entry.callback.AddListener((eventData) => { this.GetComponent<ItemScript>().GRAB(); });
                trigger.triggers.Add(entry);
            }
            else if (i == 2)
            {
                entry.callback.AddListener((eventData) => { this.GetComponent<ItemScript>().TALK(); });
                trigger.triggers.Add(entry);
            }
            else if (i == 3)
            {
                entry.callback.AddListener((eventData) => { transform.GetComponent<ItemScript>().USE(); });
                trigger.triggers.Add(entry);
            }

        }
    }

The child of the object has four choices. I want to add the 4 button functions in the itemscript to each event trigger Each pointer click is added, but the event is not applied, so I post a question.

unity callback delegates

2022-09-21 15:59

1 Answers

When I looked up another example, they explicitly created a TriggerEvent object in the callback as shown below. Try it this way.

    public EventTrigger et;

    void Start () {
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerClick;
        entry.callback = new EventTrigger.TriggerEvent();
        UnityEngine.Events.UnityAction<BaseEventData> call = new UnityEngine.Events.UnityAction<BaseEventData>(abc);
        entry.callback.AddListener(call);

        et.triggers.Add(entry);
    }

    void abc(UnityEngine.EventSystems.BaseEventData baseEvent){
        Debug.Log ("abc");
    }

Does the fact that the pointer click is added respectively, but the event is not applied means that it comes out from the inspector view to the pointer click?

According to the code you wrote, methods such as EAT(); and GRAB() should be called.

The part that could be suspected is

(1) i<transform.GetChild(2).childCount Make sure that the for statement, which is the execution condition, rotates properly.

(2) Check if there are other UI elements that take click events first. For example, if two buttons are overlapped, the button behind you may not be pressed.

Additional Add Components will return the added components, so you can reduce the amount of adding components like this.

//temp.AddComponent<ItemScript>();
//ItemScript newItem = temp.GetComponent<ItemScript>();
//in one line as below 
ItemScript newItem = temp.AddComponent<ItemScript>();


2022-09-21 15:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.