게임엔진관련/유니티 엔진

[Unity] 유니티 토글 Toggle 버튼 관련

AlrepondTech 2019. 11. 19. 11:37
반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

 

#토글 누를때마다 이벤트 체크하기

- 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

  1. Toggle sound = GameObject.Find("soundOnOff").GetComponent();
  2. sound.onValueChanged.AddListener(gameSoundStatusChanged); // --- Error
  3.  

and the function is

  1. public void gameSoundStatusChanged()
  2. {....}
  3.  

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.

Add comment

 

 

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.

  1. public Slider myMusicVolumeSlider;
  2. AudioSource myBGMusic;
  3.  
  4. void Start ()
  5. {
  6. myBGMusic = GetComponent<AudioSource>();
  7. myMusicVolumeSlider.value = 0.2f;
  8. myMusicVolumeSlider.onValueChanged.AddListener(RaiseVolume);
  9. }
  10.  
  11. void Update ()
  12. {
  13. //I Don't want to run this every frame....
  14. //myBGMusic.volume = myMusicVolumeSlider.value;
  15. }
  16.  
  17. public void RaiseVolume(float value)
  18. {
  19. myBGMusic.volume = myMusicVolumeSlider.value;
  20. //Or even just... myBGMusic.volume = value;
  21. }
  22.  

Add comment ·  Hide 2 · Share

 

 

 

 

반응형

 

 

728x90

 

 

 

=================================

=================================

=================================

 

 


출처: 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:

  1. void Start ()
  2. {
  3. GameObject cb1 = (GameObject)Instantiate(cbPrefab);
  4.  
  5. Toggle t = cb1.GetComponent<Toggle>();
  6. t.onValueChanged.AddListener(() => handleCheckbox(value)); **error here**
  7. }
  8.  
  9.  
  10. void handleCheckbox(bool value)
  11. {
  12. print("handleCheckbox. value=" + value);
  13. }
  14.  
  15.  

The compile error that I get is:

  1. error CS1593: Delegate `UnityEngine.Events.UnityAction<bool>' does not take `0' arguments
  2.  
  3.  

Could somebody help me to fix this? Thanks!

 

Best Answer

Answer by fafase · '15년 Feb월 16일 AM 08시 16분

  1. t.onValueChanged.AddListener ( (value) => { // you are missing this
  2. handleCheckbox(value); // this is just a basic method call within another method
  3. } // and this one
  4. ); // closing the AddListener method parameter

 

 

 

=================================

=================================

=================================

 

 

 

 

 

#기타관련링크

- http://blog.naver.com/PostView.nhn?blogId=showmeii1201&logNo=220189810413&parentCategoryNo=&categoryNo=33&viewDate=&isShowPopularPosts=false&from=postView

 

- https://www.dragonkyoutube.ga/2019/07/blog-post_16.html

 

 

 

=================================

=================================

=================================

 

 

반응형