Within my Unity Project in my code, (see below), I have a button called Check which executes when CheckPress() function is called. I read two approaches by which this can be done.
Appraoch 1: Using Check.onClick.AddListener(CheckPress); and then attaching script to Canvas. When I press run and click the Check button, this works.
Approach 2: The other approach I read is that, I can attach my script to Canvas. From there I select the button Check from hierarchy, then under inspector I see On Click(), I click + sign and then drag the Canvas and place it in Object (see the attached image), then under No Function I select the CheckButtonBehaviour Script and select the CheckPress(). When I press run and click the Check button, this also works as intended.
I wanted to know if these two approaches are same or different. If they are different, then in which context either approach should be implemented.
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Threading;
using UnityEditor;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System;
using System.Linq;
public class CheckButtonBehaviour : MonoBehaviour
{
public Button Check;
public Button Play;
public Button NextSignal;
public List<string> signals = new List<string> {"Speech1", "Speech2", "Speech3", "Speech4"};
public List<string> shuffledSignals = new List<string>();
public int index = 1;
public int counter = 3;
private static int localPort;
private string IP;
public int port = 8050;
IPEndPoint remoteEndPoint;
UdpClient client;
void Start()
{
var rndr = new System.Random();
shuffledSignals = signals.OrderBy(i => rndr.Next()).ToList();
IP = "127.0.0.1";
port = 8050;
remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
client = new UdpClient();
Play.gameObject.SetActive(false);
NextSignal.gameObject.SetActive(false);
Check.onClick.AddListener(CheckPress);
}
public void CheckPress()
{
byte[] data0 = Encoding.ASCII.GetBytes("starting"); ///OLD CODE part
client.Send(data0, data0.Length, remoteEndPoint);
Debug.Log("<color=blue>Something is sent to MAX</color> : starting");
Check.gameObject.SetActive(true);
Check.interactable = false;
Check.enabled = false;
Play.gameObject.SetActive(true);
NextSignal.gameObject.SetActive(true);
}