[Unity] 유니티 토글 Toggle 버튼 관련
=================================
=================================
=================================
#토글 누를때마다 이벤트 체크하기
- CTest1클래스를 Toggle UI의 컴포넌트에 넣자.
- 아래 코드와 같이 "onValueChanged.AddListener(...)" 에 이벤트로 들어온 값을 확인하면 된다.
CTest1 : MonoBehaviour
{
Toggle _toggle = null;
void Start()
{
_toggle = gameObject.GetComponent<Toggle>();
_toggle.gameObject.SetActive(true);
_toggle.onValueChanged.AddListener(
(bool bOn) =>
{
bool val = bOn; //누루는 이벤트 마다 값이 들어온다.
}
);
}
}
=================================
=================================
=================================
Adding a listener to Toggle.onValueChanged via Script
Hello,
I was trying to add a listener on a Toggle Button but i am not able to write that. Can anybody please help me. Here is what i am doing
- Toggle sound = GameObject.Find("soundOnOff").GetComponent();
- sound.onValueChanged.AddListener(gameSoundStatusChanged); // --- Error
and the function is
- public void gameSoundStatusChanged()
- {....}
it says .addlistener has some invalid parameters. Cause it asks for parameter of type UnityAction function. Please help me on how to write the function in given syntax.
Answer by batteryyrettab · '14년 Oct월 24일 AM 07시 20분
I just had the same issue. You just need to pass a float parameter into your method. eg.
- public Slider myMusicVolumeSlider;
- AudioSource myBGMusic;
- void Start ()
- {
- myBGMusic = GetComponent<AudioSource>();
- myMusicVolumeSlider.value = 0.2f;
- myMusicVolumeSlider.onValueChanged.AddListener(RaiseVolume);
- }
- void Update ()
- {
- //I Don't want to run this every frame....
- //myBGMusic.volume = myMusicVolumeSlider.value;
- }
- public void RaiseVolume(float value)
- {
- myBGMusic.volume = myMusicVolumeSlider.value;
- //Or even just... myBGMusic.volume = value;
- }
· Hide 2 · Share
=================================
=================================
=================================
출처: https://answers.unity.com/questions/902399/addlistener-to-a-toggle.html
AddListener to a Toggle.
Hello. I would like to attach a listener to checkbox:
- void Start ()
- {
- GameObject cb1 = (GameObject)Instantiate(cbPrefab);
- Toggle t = cb1.GetComponent<Toggle>();
- t.onValueChanged.AddListener(() => handleCheckbox(value)); **error here**
- }
- void handleCheckbox(bool value)
- {
- print("handleCheckbox. value=" + value);
- }
The compile error that I get is:
- error CS1593: Delegate `UnityEngine.Events.UnityAction<bool>' does not take `0' arguments
Could somebody help me to fix this? Thanks!
Best Answer
Answer by fafase · '15년 Feb월 16일 AM 08시 16분
- t.onValueChanged.AddListener ( (value) => { // you are missing this
- handleCheckbox(value); // this is just a basic method call within another method
- } // and this one
- ); // closing the AddListener method parameter
=================================
=================================
=================================
#기타관련링크
- https://www.dragonkyoutube.ga/2019/07/blog-post_16.html
=================================
=================================
=================================