Skip to content

Commit

Permalink
Fix handling on U# GetComponent analogues
Browse files Browse the repository at this point in the history
- GetComponent was not considering all components correctly and was also breaking when it'd hit on-UdonSharpBehaviour UdonBehaviours Resolves #57
  • Loading branch information
MerlinVR committed Oct 6, 2020
1 parent 7087ad4 commit f1e3ebf
Showing 1 changed file with 53 additions and 37 deletions.
90 changes: 53 additions & 37 deletions Assets/UdonSharp/Editor/Editors/UdonSharpComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ public static void ApplyProxyModifications(this UdonSharpBehaviour behaviour, Pr
#endregion

#region Utility functions
private static UdonSharpBehaviour ConvertToUdonSharpComponent(UdonBehaviour behaviour, System.Type type, ProxySerializationPolicy proxySerializationPolicy)
private static UdonSharpBehaviour ConvertToUdonSharpComponentIntnl(UdonBehaviour behaviour, System.Type type, ProxySerializationPolicy proxySerializationPolicy)
{
if (behaviour == null)
return null;

if (!UdonSharpEditorUtility.IsUdonSharpBehaviour(behaviour))
return null;

UdonSharpBehaviour udonSharpBehaviour = UdonSharpEditorUtility.GetProxyBehaviour(behaviour, ProxySerializationPolicy.NoSerialization);
System.Type uSharpBehaviourType = udonSharpBehaviour.GetType();

Expand All @@ -72,9 +75,22 @@ private static UdonSharpBehaviour ConvertToUdonSharpComponent(UdonBehaviour beha
return null;
}

private static T ConvertToUdonSharpComponent<T>(UdonBehaviour behaviour, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour
private static UdonSharpBehaviour ConvertToUdonSharpComponent(UdonBehaviour[] behaviours, System.Type type, ProxySerializationPolicy proxySerializationPolicy)
{
foreach (UdonBehaviour behaviour in behaviours)
{
UdonSharpBehaviour udonSharpBehaviour = ConvertToUdonSharpComponentIntnl(behaviour, type, proxySerializationPolicy);

if (udonSharpBehaviour)
return udonSharpBehaviour;
}

return null;
}

private static T ConvertToUdonSharpComponent<T>(UdonBehaviour[] behaviours, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour
{
return (T)ConvertToUdonSharpComponent(behaviour, typeof(T), proxySerializationPolicy);
return (T)ConvertToUdonSharpComponent(behaviours, typeof(T), proxySerializationPolicy);
}

private static UdonSharpBehaviour[] ConvertToUdonSharpComponents(UdonBehaviour[] behaviours, System.Type type, ProxySerializationPolicy proxySerializationPolicy)
Expand All @@ -86,7 +102,7 @@ private static UdonSharpBehaviour[] ConvertToUdonSharpComponents(UdonBehaviour[]

foreach (UdonBehaviour behaviour in behaviours)
{
UdonSharpBehaviour udonSharpBehaviour = ConvertToUdonSharpComponent(behaviour, type, proxySerializationPolicy);
UdonSharpBehaviour udonSharpBehaviour = ConvertToUdonSharpComponentIntnl(behaviour, type, proxySerializationPolicy);

if (udonSharpBehaviour)
udonSharpBehaviours.Add(udonSharpBehaviour);
Expand All @@ -104,7 +120,7 @@ private static T[] ConvertToUdonSharpComponents<T>(UdonBehaviour[] behaviours, P

foreach (UdonBehaviour behaviour in behaviours)
{
UdonSharpBehaviour udonSharpBehaviour = ConvertToUdonSharpComponent<T>(behaviour, proxySerializationPolicy);
UdonSharpBehaviour udonSharpBehaviour = ConvertToUdonSharpComponentIntnl(behaviour, typeof(T), proxySerializationPolicy);

if (udonSharpBehaviour)
udonSharpBehaviours.Add((T)udonSharpBehaviour);
Expand Down Expand Up @@ -152,35 +168,35 @@ public static T AddUdonSharpComponent<T>(this GameObject gameObject) where T : U
#region GetComponent
[PublicAPI]
public static T GetUdonSharpComponent<T>(this GameObject gameObject) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponent<UdonBehaviour>(), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(gameObject.GetComponents<UdonBehaviour>(), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponent<T>(this GameObject gameObject, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponent<UdonBehaviour>(), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(gameObject.GetComponents<UdonBehaviour>(), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponent(this GameObject gameObject, System.Type type) =>
ConvertToUdonSharpComponent(gameObject.GetComponent<UdonBehaviour>(), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(gameObject.GetComponents<UdonBehaviour>(), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponent(this GameObject gameObject, System.Type type, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(gameObject.GetComponent<UdonBehaviour>(), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(gameObject.GetComponents<UdonBehaviour>(), type, proxySerializationPolicy);

[PublicAPI]
public static T GetUdonSharpComponent<T>(this Component component) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponent<UdonBehaviour>(), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(component.GetComponents<UdonBehaviour>(), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponent<T>(this Component component, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponent<UdonBehaviour>(), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(component.GetComponents<UdonBehaviour>(), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponent(this Component component, System.Type type) =>
ConvertToUdonSharpComponent(component.GetComponent<UdonBehaviour>(), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(component.GetComponents<UdonBehaviour>(), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponent(this Component component, System.Type type, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(component.GetComponent<UdonBehaviour>(), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(component.GetComponents<UdonBehaviour>(), type, proxySerializationPolicy);
#endregion

#region GetComponents
Expand Down Expand Up @@ -220,67 +236,67 @@ public static UdonSharpBehaviour[] GetUdonSharpComponents(this Component compone
#region GetComponentInChildren
[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this GameObject gameObject) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponentInChildren<UdonBehaviour>(), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(gameObject.GetComponentsInChildren<UdonBehaviour>(), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this GameObject gameObject, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponentInChildren<UdonBehaviour>(), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(gameObject.GetComponentsInChildren<UdonBehaviour>(), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this GameObject gameObject, System.Type type) =>
ConvertToUdonSharpComponent(gameObject.GetComponentInChildren<UdonBehaviour>(), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(gameObject.GetComponentsInChildren<UdonBehaviour>(), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this GameObject gameObject, System.Type type, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(gameObject.GetComponentInChildren<UdonBehaviour>(), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(gameObject.GetComponentsInChildren<UdonBehaviour>(), type, proxySerializationPolicy);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this GameObject gameObject, bool includeInactive) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponentInChildren<UdonBehaviour>(includeInactive), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(gameObject.GetComponentsInChildren<UdonBehaviour>(includeInactive), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this GameObject gameObject, bool includeInactive, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponentInChildren<UdonBehaviour>(includeInactive), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(gameObject.GetComponentsInChildren<UdonBehaviour>(includeInactive), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this GameObject gameObject, System.Type type, bool includeInactive) =>
ConvertToUdonSharpComponent(gameObject.GetComponentInChildren<UdonBehaviour>(includeInactive), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(gameObject.GetComponentsInChildren<UdonBehaviour>(includeInactive), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this GameObject gameObject, System.Type type, bool includeInactive, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(gameObject.GetComponentInChildren<UdonBehaviour>(includeInactive), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(gameObject.GetComponentsInChildren<UdonBehaviour>(includeInactive), type, proxySerializationPolicy);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this Component component) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponentInChildren<UdonBehaviour>(), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(component.GetComponentsInChildren<UdonBehaviour>(), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this Component component, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponentInChildren<UdonBehaviour>(), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(component.GetComponentsInChildren<UdonBehaviour>(), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this Component component, System.Type type) =>
ConvertToUdonSharpComponent(component.GetComponentInChildren<UdonBehaviour>(), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(component.GetComponentsInChildren<UdonBehaviour>(), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this Component component, System.Type type, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(component.GetComponentInChildren<UdonBehaviour>(), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(component.GetComponentsInChildren<UdonBehaviour>(), type, proxySerializationPolicy);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this Component component, bool includeInactive) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponentInChildren<UdonBehaviour>(includeInactive), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(component.GetComponentsInChildren<UdonBehaviour>(includeInactive), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponentInChildren<T>(this Component component, bool includeInactive, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponentInChildren<UdonBehaviour>(includeInactive), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(component.GetComponentsInChildren<UdonBehaviour>(includeInactive), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this Component component, System.Type type, bool includeInactive) =>
ConvertToUdonSharpComponent(component.GetComponentInChildren<UdonBehaviour>(includeInactive), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(component.GetComponentsInChildren<UdonBehaviour>(includeInactive), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInChildren(this Component component, System.Type type, bool includeInactive, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(component.GetComponentInChildren<UdonBehaviour>(includeInactive), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(component.GetComponentsInChildren<UdonBehaviour>(includeInactive), type, proxySerializationPolicy);
#endregion

#region GetComponentsInChildren
Expand Down Expand Up @@ -352,35 +368,35 @@ public static UdonSharpBehaviour[] GetUdonSharpComponentsInChildren(this Compone
#region GetComponentInParent
[PublicAPI]
public static T GetUdonSharpComponentInParent<T>(this GameObject gameObject) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponentInParent<UdonBehaviour>(), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(gameObject.GetComponentsInParent<UdonBehaviour>(), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponentInParent<T>(this GameObject gameObject, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(gameObject.GetComponentInParent<UdonBehaviour>(), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(gameObject.GetComponentsInParent<UdonBehaviour>(), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInParent(this GameObject gameObject, System.Type type) =>
ConvertToUdonSharpComponent(gameObject.GetComponentInParent<UdonBehaviour>(), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(gameObject.GetComponentsInParent<UdonBehaviour>(), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInParent(this GameObject gameObject, System.Type type, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(gameObject.GetComponentInParent<UdonBehaviour>(), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(gameObject.GetComponentsInParent<UdonBehaviour>(), type, proxySerializationPolicy);

[PublicAPI]
public static T GetUdonSharpComponentInParent<T>(this Component component) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponentInParent<UdonBehaviour>(), ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent<T>(component.GetComponentsInParent<UdonBehaviour>(), ProxySerializationPolicy.Default);

[PublicAPI]
public static T GetUdonSharpComponentInParent<T>(this Component component, ProxySerializationPolicy proxySerializationPolicy) where T : UdonSharpBehaviour =>
ConvertToUdonSharpComponent<T>(component.GetComponentInParent<UdonBehaviour>(), proxySerializationPolicy);
ConvertToUdonSharpComponent<T>(component.GetComponentsInParent<UdonBehaviour>(), proxySerializationPolicy);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInParent(this Component component, System.Type type) =>
ConvertToUdonSharpComponent(component.GetComponentInParent<UdonBehaviour>(), type, ProxySerializationPolicy.Default);
ConvertToUdonSharpComponent(component.GetComponentsInParent<UdonBehaviour>(), type, ProxySerializationPolicy.Default);

[PublicAPI]
public static UdonSharpBehaviour GetUdonSharpComponentInParent(this Component component, System.Type type, ProxySerializationPolicy proxySerializationPolicy) =>
ConvertToUdonSharpComponent(component.GetComponentInParent<UdonBehaviour>(), type, proxySerializationPolicy);
ConvertToUdonSharpComponent(component.GetComponentsInParent<UdonBehaviour>(), type, proxySerializationPolicy);
#endregion

#region GetComponentsInParent
Expand Down

0 comments on commit f1e3ebf

Please sign in to comment.