ClickExercise.cs 2.86 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ClickExercise : MonoBehaviour
{
    RawImage btnImage;
    RawImage btnImage2;
    GameObject btnParent; //버튼들의 부모

    GameObject quad;    //커서
    Vector3 pos;
    private float timer;

    private void Start()
    {
        btnParent = GameObject.Find("BtnGroup").gameObject; // "부모의 이름"으로 찾습니다.

        quad = GameObject.Find("Quad_s").gameObject;
        pos = quad.transform.position;
        Debug.Log(pos);
    }

    public void GetBtn()
    {
        Debug.Log("GetBtn");

        GameObject tempBtn = EventSystem.current.currentSelectedGameObject;
        btnImage = tempBtn.GetComponent<RawImage>(); // 해당 오브젝트의 image 컴포넌트를 받음

        int btnLength = btnParent.transform.childCount; // 자식의 갯수를 파악
        Debug.Log(btnLength);
        for (int i = 0; i < btnLength; i++) // 자식의 갯수 만큼 i++ 실행
        {
            //transfrom.Getchild(i)를 통해 버튼을 하나씩 색인
            GameObject tempBtns = btnParent.transform.GetChild(i).gameObject; // 해당 버튼 이미지 컴포넌트를 불러온 후 변경
            btnImage2 = tempBtns.GetComponent<RawImage>();
            if (tempBtns.name == "Squat")
            {
                btnImage2.texture = Resources.Load("Squat_off", typeof(Texture2D)) as Texture2D;
            }
            else if (tempBtns.name == "Lunge")
            {
                btnImage2.texture = Resources.Load("Lunge_off", typeof(Texture2D)) as Texture2D;
            }
            else if (tempBtns.name == "SideHiKick")
            {
                btnImage2.texture = Resources.Load("SideHiKick_off", typeof(Texture2D)) as Texture2D;
            }
            //Debug.Log(tempBtns);
        }

        if (btnImage.name == "Squat")
        {
            btnImage.texture = Resources.Load("Squat_on", typeof(Texture2D)) as Texture2D;
        }
        else if (btnImage.name == "Lunge")
        {
            btnImage.texture = Resources.Load("Lunge_on", typeof(Texture2D)) as Texture2D;
        }
        else if (btnImage.name == "SideHiKick")
        {
            Debug.Log("SideHighKick");
            btnImage.texture = Resources.Load("SideHiKick_on", typeof(Texture2D)) as Texture2D;
        }

            
    }

    void Update()
    {
        
        pos = quad.transform.position;
        Debug.Log(pos);

        /*
        if (pos.x >= 155 && pos.x <= 185 && pos.y >= 13 && pos.y <= 60) {

            btnImage2.texture = Resources.Load("Squat_off", typeof(Texture2D)) as Texture2D;
            btnImage.texture = Resources.Load("SideHiKick_on", typeof(Texture2D)) as Texture2D;
            btnImage2.texture = Resources.Load("Lunge_off", typeof(Texture2D)) as Texture2D;
        }
       
    */
    }
}