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>();
}
{
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 (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๋ฅผ ์ฐ๋๊ฒ ๊ทธ๋ฅ ์๋๊ผฌ์
ํ๊ฐ์ข ใฑใฑ
3๋ฒ ์๋ ref ๋ง๊ณ out ์ฐ๋ ๊ฑด ์ด๋ฐ? - dc App
4๋ฒ์๋ out์ ๋ชป์
์ฌ์ค ํฐ์ด๋ผ ์ฝ๋๊ฐ ์ ๋๋ก ์๋ณด์ ใ ใ - dc App
์ ํ์ฅ๋ฉ์๋๋ค ๊ทธ๋ผ ์ด์ฉ ์ ์์ง. 3, 4๋ฒ ๋๊ฐ ์ ํํ ๊ฒ. - dc App
์ฐ๋ฐ
5. _rigidbdy ??= GetComponent();
์ด๊ฒ ๊น๋ํ๊ธด ํ๋ฐ ์ ๋ํฐ์์ง ์ค๋ธ์ ํธ์ ??= fake null๋์ ๋ชป์... ์์ ๋ค๋ฅธ๊ฑฐ ๋ชป๋ฃ๋๊ฒ๋ ์๊ธฐ๋ ํ๊ณ
GetComponent(typeof(Rigidbody));