본문 바로가기

프로그램/유니티 스크립트 소스

[유니티 스크립트 소스] Text Mesh Pro

728x90
반응형

1. Button

 

using UnityEngine;
using UnityEngine.UI;

public class Demo : MonoBehaviour
{
    public Button m_btn1, m_btn2, m_btn3;
        
    void Start()
    {
        m_btn1.onClick.AddListener(TaskOnClick);
        m_btn2.onClick.AddListener(delegate { TaskWithParameters("Hello"); });
        m_btn3.onClick.AddListener(() => ButtonClicked(42));
        m_btn3.onClick.AddListener(TaskOnClick);
    }
    
    void TaskOnClick()
    {
        Debug.Log("TaskOnClick");
    }

    void TaskWithParameters(string message)
    {
        Debug.Log(message);
    }

    void ButtonClicked(int buttonNo)
    {
        Debug.Log("Button clicked = " + buttonNo);
    }
}

 

 

2. Dropdown

 

using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Demo : MonoBehaviour
{
    public TMP_Dropdown m_tdd1;
    private List<string> tdd1Options
        = new List<string> { "0", "1" };

    void Start()
    {
        m_tdd1.ClearOptions();
        m_tdd1.AddOptions(tdd1Options);

        m_tdd1.onValueChanged.AddListener(delegate {
            DropdownValueChanged(m_tdd1);
        });
    }

    void DropdownValueChanged(TMP_Dropdown change)
    {
        Debug.Log("New Value: " + change.value);
        Debug.Log("New Value: " + change.options[change.value].text);
        Debug.Log("New Value: " + change.captionText.text);
    }
}

 

 

.3 InputField

 

using UnityEngine;
using TMPro;

public class Demo : MonoBehaviour
{
    public TMP_InputField m_tif1, m_tif2;

    void Start()
    {
        m_tif1.onValueChanged.AddListener(delegate {
            ValueChangeCheck(m_tif1);
        });

        m_tif2.onEndEdit.AddListener(delegate {
            LockInput(m_tif2);
        });
    }

    void ValueChangeCheck(TMP_InputField change)
    {
        float number;

        if (float.TryParse(change.text, out number))
            Debug.Log(number);

        else
            Debug.Log($"Unable to parse '{change.text}'.");
    }

    void LockInput(TMP_InputField input)
    {
        if (input.text.Length > 0)
        {
            Debug.Log("Text has been entered");
        }

        else if (input.text.Length == 0)
        {
            Debug.Log("Main Input Empty");
        }
    }
}

 

 

4. Slider

 

using UnityEngine;
using UnityEngine.UI;

public class Demo : MonoBehaviour
{
    public Slider m_sld1;

    public void Start()
    {
        m_sld1.minValue = 0;
        m_sld1.maxValue = 100;

        m_sld1.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
    }

    public void ValueChangeCheck()
    {
        Debug.Log(m_sld1.value);
    }
}

 

 

5. Toggle

 

using UnityEngine;
using UnityEngine.UI;

public class Demo : MonoBehaviour
{
    Toggle m_Toggle;
    public Text m_Text;

    void Start()
    {
        m_Toggle.onValueChanged.AddListener(delegate {
            ToggleValueChanged(m_Toggle);
        });
    }

    void ToggleValueChanged(Toggle change)
    {
        Debug.Log(change.isOn);
    }
}

 

728x90
반응형