본문 바로가기
개발💻/Unity

[Unity] 텍스트 끝에 이모지 생성

by Sports Entrepreneur 2022. 10. 4.
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class UImanager : MonoBehaviour
{
    public TextMeshProUGUI result, input;                         // 결과, 입력 text
    public TMP_InputField inputfield;
    public GameObject img;
    public Image img1;
    public string text;
    float x_offset = 25.0f, y_offset = 15.0f;                    // 글자위치의 offset

    private void Update()
    {
        // 아무키나 눌렀을 때 inputfield에 있는 text를 결과 text box에 출력
        if(Input.anyKey)                                         
        {
            text = input.text;
            result.text = text;
        }
    }

    public void OnClickClear() 
    {
        inputfield.text = " ";                                   // inputfield의 텍스트를 지움
        result.text = input.text;                                // inputfield의 text를 결과 text box에 출력
        Destroy(GameObject.FindGameObjectWithTag("text"), 0.1f); // text 태그를 가진 오브젝트를 삭제
    }

    public void OnClickEnter()
    {
        inputfield.text += "<br>";                               // inputfield의 텍스트에 줄바꿈 xml tag를 추가
    }

    public void OnClickCloneImg()                                
    {
        // result text의 정보
        TMP_TextInfo textInfo = result.textInfo;
        
        // 마지막 글자
        TMP_CharacterInfo characterInfo = textInfo.characterInfo[textInfo.characterCount - 1];                               

        Vector3 bottomRight =                                                                    
            result.transform.TransformPoint(new Vector3(characterInfo.bottomRight.x + x_offset, 
            characterInfo.bottomRight.y + y_offset, 0));
        
        // 마지막 글자의 오른쪽아래 위치로 img 생성
        Instantiate(img, bottomRight, Quaternion.identity, GameObject.Find("Canvas").transform); 
    }

    public void OnClickMoveEmoji()
    {
        TMP_TextInfo textInfo = result.textInfo;
        TMP_CharacterInfo characterInfo = textInfo.characterInfo[textInfo.characterCount -1];
        
        // 마지막 글자의 오른쪽아래 위치로 img 생성
        img1.transform.position =                                                               
            input.transform.TransformPoint(new Vector2(characterInfo.bottomRight.x + x_offset, 
            characterInfo.bottomRight.y + y_offset));
    }
}