정리/UNITY

InvokeRepeating (반복 재생 기능)

원클릭쓰리버그 2022. 5. 21. 17:36
728x90

 

//Script to remove ragdolls when they reach a max amount (not optimized)

using UnityEngine;

public class ClearRagdolls:MonoBehaviour{
    public int maxRagdolls;
    public GameObject rag;
    public void Start() {
    	InvokeRepeating("ClearRags", 1.0f,1.0f);
    }
    
    public void ClearRags() {
    
    	
    	int counter = 0;
    	foreach(GameObject fooObj in GameObject.FindGameObjectsWithTag("Player"))
    		{		
        		if(fooObj.GetComponent<Rigidbody>().useGravity==true){
        		if(rag == null)
          		rag = fooObj;
          		counter++;
          		}
    		}
    	if(maxRagdolls < counter){
    		Destroy(rag);
    	}
    }
}

위의 스크립트를 참고하면,

 

Start() 함수에서 InvokeRepeating이라는 함수를 실행한다.

이는 ClearRags이라는 함수를 실행하라는 Invoke() 기능 + 반복 기능을 섞은 기능이다.

풀어서 말하면, 

ClearRags()라는 함수를 Start()할때, 1초 뒤에 실행한다. 그리고 1초마다 반복 재생하라는 뜻이다.

 

 

Declaration

public void InvokeRepeating(string methodName, float time, float repeatRate);

Description

Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.

 

https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

'정리 > UNITY' 카테고리의 다른 글

StringToHash  (0) 2022.05.21
유니티 게임 내 Scripts 최적화 (Unity Article)  (0) 2022.04.28
Unity 성능 향상을 위한 권장사항 (Microsoft)  (0) 2022.04.28
Menu_UI 기능 구현  (0) 2022.04.25
IL2CPP  (0) 2022.03.19