NetworkBehaviour.OnValidate: GetComponentInParent(bool) 2020 support (credits FakeByte)

This commit is contained in:
mischa 2023-07-18 15:37:04 +08:00
parent 27e2238d0b
commit 70c34ecf5d

View File

@ -301,14 +301,23 @@ protected virtual void OnValidate()
// parents.
// only run this in Editor. don't add more runtime overhead.
// GetComponentInParent(includeInactive) is needed and only available in 2021+
// Prefabs are not considered active, so this check requires to scan inactive.
#if UNITY_EDITOR && UNITY_2021_3_OR_NEWER
// GetComponentInParent(includeInactive) is needed because Prefabs are not
// considered active, so this check requires to scan inactive.
#if UNITY_EDITOR
#if UNITY_2021_3_OR_NEWER // 2021 has GetComponentInParents(active)
if (GetComponent<NetworkIdentity>() == null &&
GetComponentInParent<NetworkIdentity>(true) == null)
{
Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents.");
}
#elif UNITY_2020_3_OR_NEWER // 2020 only has GetComponentsInParents(active), we can use this too
NetworkIdentity[] parentsIds = GetComponentsInParent<NetworkIdentity>(true);
int parentIdsCount = parentsIds != null ? parentsIds.Length : 0;
if (GetComponent<NetworkIdentity>() == null && parentIdsCount == 0)
{
Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents.");
}
#endif
#endif
}