public static bool IsComponentMissing<T>(this MonoBehaviour monoBehaviour, T component, out T getComponent) where T : Component
{
if (component == null)
{
Debug.LogWarning("Component " + typeof(T) + " is missing. Adding it now.");
getComponent = monoBehaviour.GetComponent<T>();
return true;
}
else
{
getComponent = null;
return false;
}
}

public static void EnsureExist<T>(T component, Action ensuring) where T : Component
{
if (component != null) return;

Debug.LogWarning("Component " + typeof(T) + " is missing. Adding it now.");
ensuring.Invoke();
}

public static T EnsureExist<T>(this MonoBehaviour monoBehaviour, T component) where T : Component
{
if (component != null) return component;

Debug.LogWarning("Component " + nameof(T) + " is missing. Adding it now.");
return monoBehaviour.GetComponent<T>();
}

public static void EnsureExist<T>(this MonoBehaviour monoBehaviour, ref T component) where T : Component
{
if (component != null) return;

Debug.LogWarning("Component " + nameof(T) + " is missing. Adding it now.");
component = monoBehaviour.GetComponent<T>();
}




void Awake()
{
// 1
if (this.IsComponentMissing(_rigidbody, out var getComponent)) _rigidbody = getComponent;

// 2
Utils.EnsureExist(_rigidbody, () => _rigidbody = GetComponent<Rigidbody>());

// 3
_rigidbody = this.EnsureExist(_rigidbody);

// 4
this.EnsureExist(ref _rigidbody);
}


1. If๋ฌธ์ด ํ•จ์ˆ˜ ๋ฐ–์œผ๋กœ ๋‚˜์™€์„œ ๊ธธ์–ด์ง€๊ฑฐ๋‚˜ ์ค‘๊ด„ํ˜ธ๊ฐ€ ํ•„์š”ํ•จย 

2. ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ผ์ผํžˆ ์ž‘์„ฑํ•ด์ฃผ๊ธด ๋ญ”๊ฐ€ ๊ทธ๋Ÿผ

3. ์ด๋ฏธ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ๋Œ€์ž…ํ•˜๋Š”๊ฒŒ ๊ฑฐ์Šฌ๋ฆผ

4. ref๋ฅผ ์“ฐ๋Š”๊ฒŒ ๊ทธ๋ƒฅ ์•„๋‹ˆ๊ผฌ์›€


ํ‰๊ฐ€์ข€ ใ„ฑใ„ฑ