2024/04 21

[C#] 다른 클래스의 코루틴 함수 호출하기

Player player = other.GetComponent(); player.StartCoroutine(coContinousDamage(_damage, 1.0f, 3)); Player 클래스의 코루틴 함수를 위와 같은 방식으로 호출하려고 하면 해당 함수를 찾을 수 없다고 에러가 뜬다 Player player = other.GetComponent(); player.StartCoroutine(player.coContinousDamage(_damage, 1.0f, 3)); 코루틴도 인스턴스명.코루틴명 으로 찾아주어야 해당 코루틴 함수를 찾아서 실행시킬 수 있다

[C#] Dictionary 형태로 json 데이터 파싱하기

{ "enemyStats" : [ { "ID": 1001, "hp": 20, "damage": 35, "moveSpeed": 3, "skillGuage": 20, "objectDropPer": 35 }, { "ID": 1002, "hp": 25, "damage": 30, "moveSpeed": 2, "skillGuage": 15, "objectDropPer": 30 }, { "ID": 1003, "hp": 20, "damage": 30, "moveSpeed": 4, "skillGuage": 25, "objectDropPer": 40 } ] } json 데이터를 준비해 준다 * json은 반드시 단일 오브젝트 형태로 존재해야 하기 때문에 배열을 감싸는 오브젝트를 하나 만들어주어야 한다(위 코드에서는 ene..

[C#] json 데이터 파싱하기

{ "life": "4", "hp": "100", "damage": "7", "moveSpeed": "5" } 우선 json 파일을 준비해 준다 json에서 { } 괄호는 오브젝트고, [ ] 괄호는 배열이라고 생각하면 된다 [Serializable] public class PlayerData { public int life; public int hp; public int damage; public float moveSpeed; } 데이터 틀을 만들어 준다 직렬화를 위해 반드시 위에 [Serializable]을 붙여주어야 한다(using System) 이때 각 변수들의 이름은 json 데이터에서의 각 항목들의 이름과 일치해야 한다 public class DataManager { public PlayerDa..

[Unity] 특정 방향으로 2D 오브젝트 회전시키기

Vector2 dir; transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg); * 유니티는 회전 각도를 나타날 때 쿼터니언을 사용하기 때문에 Vector3가 아니라 Quaternion.Euler를 사용해야 한다. (짐벌락 현상을 방지하기 위함) 2D상에서의 회전은 Z축을 회전시키면 되기 때문에 x와 y의 값은 0으로 고정시켜 둔다 Mathf의 Atan2는 아크탄젠트 함수(역삼각함수)로, y와 x를 가지고 사잇각을 구할 수 있다 * Atan과 Atan2함수가 있는데, Atan 함수는 인자로 float값(y/x)를 받고, Atan2 함수는 인자로 y값과 x값을 각각 받는다. 따라서 Atan 함수는 만약 x가..

[Selenium] Expected Conditions(EC)

파이썬에서 코드 동작을 멈추기 위해서 time.sleep()을 사용해도 되지만, 특정 조건을 만족할 때까지 정지하도록 하는 코드를 사용할 수 있다 from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome(options=chrome_options) WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.XPATH, f'{xpath}')) ) 사용은 위와 같이 하면 된다 위 예시에서는 해당 element가 클릭 가능한 상태가 될 때까지 최대 5초간 기다린다 5초가 지나기 전에 조건을 만족하면 진행되고, 5초가 지나도 조건을 만족하지 못하면 에러가 ..

Programing/Python 2024.04.05

[Selenium] 드롭다운 메뉴 선택하기

from selenium.webdriver.support.select import Select dropdown_element = driver.find_element(By.XPATH, f'{xpath}') dropdown_select = Select(dropdown_element) dropdown_select.select_by_value("1") selenium의 Select를 이용해서 드롭다운 메뉴를 선택할 수 있다 dropdown_select.select_by_value("1") dropdown_select.select_by_index(1) dropdown_select.select_by_visible_text('옵션3') 여러 방법으로 드롭다운 메뉴 중 하나를 선택할 수 있다

Programing/Python 2024.04.05

[Selenium] iframe

웹 페이지를 탐색하다 보면 분명 xpath 등을 그대로 복사해왔는데도 요소가 찾아지질 않는 문제가 발생하기도 한다 이는 iframe(inline frame, 페이지 속의 페이지) 때문이고, 해결하기 위해서는 해당 요소가 들어있는 iframe으로 이동한 뒤에 해당 요소를 찾아주어야 한다 ifrm = driver.find_element(By.CSS_SELECTOR, '#ifrm') driver.switch_to.frame(ifrm) iframe도 다른 html 요소들과 마찬가지로 selector나 xpath등이 존재하기 때문에 이를 이용하여 찾아주고, switch_to.frame() 함수를 이용해서 이동해 주면 된다 만약 iframe 안에 있는 iframe으로 이동하기 위해서는 위 과정을 두 번 거쳐 주면 ..

Programing/Python 2024.04.05

[Selenium] handle

셀레니움을 통해 웹페이지를 탐색하다 새로운 창이 뜨더라도 기존 창에서만 조작이 가능하고, 새로운 창의 요소에 대해서는 접근을 하지 못한다 따라서 handle을 변경하여 새로운 창에서도 요소들을 가져올 수 있게 해보겠다 main_window = driver.current_window_handle 이렇게 현재 창을 가져올 수 있다 for handle in driver.window_handles: if handle != main_window: driver.switch_to.window(handle) break 전체 창은 driver.window_handles로 가져올 수 있고, switch_to.window() 함수를 이용해 handle을 변경할 수 있다 main_window가 아닌 window_handle은..

Programing/Python 2024.04.05

[Selenium] 파이썬으로 Chrome 조작하기

터미널에서 pip install selenium으로 셀레니움을 설치해준다 (* selenium 4 이상부터는 크롬 드라이브 설치가 필요 없다) import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys 임포트 해 주고 chrome_options = Options() chrome_options.add_experimental_option("detach", True) 크롬 창이 자동으로 꺼지지 않게 옵션을 설정해 준다 driver = ..

Programing/Python 2024.04.05