diff --git a/Runtime/FindAssetsAttribute.cs b/Runtime/FindAssetsAttribute.cs index 0b58fcd..75524d0 100644 --- a/Runtime/FindAssetsAttribute.cs +++ b/Runtime/FindAssetsAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using UnityEngine; @@ -13,7 +13,7 @@ namespace Kogane /// /// AssetDatabase.FindAssets を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class FindAssetsAttribute : Attribute, IGetComponentAttribute @@ -27,20 +27,20 @@ public sealed class FindAssetsAttribute /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { var guid = AssetDatabase - .FindAssets( $"t:{fieldInfo.FieldType.Name}" ) + .FindAssets($"t:{fieldInfo.FieldType.Name}") .FirstOrDefault() ; - if ( string.IsNullOrWhiteSpace( guid ) ) return; + if (string.IsNullOrWhiteSpace(guid)) return; - var assetPath = AssetDatabase.GUIDToAssetPath( guid ); - var asset = AssetDatabase.LoadAssetAtPath( assetPath ); + var assetPath = AssetDatabase.GUIDToAssetPath(guid); + var asset = AssetDatabase.LoadAssetAtPath(assetPath); serializedProperty.objectReferenceValue = asset; } diff --git a/Runtime/FindAttribute.cs b/Runtime/FindAttribute.cs index 4932af0..3d4c582 100644 --- a/Runtime/FindAttribute.cs +++ b/Runtime/FindAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// GameObject.Find を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class FindAttribute : Attribute, IGetComponentAttribute @@ -27,7 +27,7 @@ public sealed class FindAttribute /// /// コンストラクタ /// - public FindAttribute( string name ) + public FindAttribute(string name) { m_name = name; } @@ -38,12 +38,17 @@ public FindAttribute( string name ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - serializedProperty.objectReferenceValue = GameObject.Find( m_name ); + if (serializedProperty.isArray) + { + return; + } + + serializedProperty.objectReferenceValue = GameObject.Find(m_name); } #endif } diff --git a/Runtime/FindChildAttribute.cs b/Runtime/FindChildAttribute.cs index cbae2e4..60dd763 100644 --- a/Runtime/FindChildAttribute.cs +++ b/Runtime/FindChildAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// Transform.Find を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class FindChildAttribute : Attribute, IGetComponentAttribute @@ -27,7 +27,7 @@ public sealed class FindChildAttribute /// /// コンストラクタ /// - public FindChildAttribute( string name ) + public FindChildAttribute(string name) { m_name = name; } @@ -38,12 +38,17 @@ public FindChildAttribute( string name ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - serializedProperty.objectReferenceValue = monoBehaviour.transform.Find( m_name ); + if (serializedProperty.isArray) + { + return; + } + + serializedProperty.objectReferenceValue = monoBehaviour.transform.Find(m_name); } #endif } diff --git a/Runtime/FindObjectOfTypeAttribute.cs b/Runtime/FindObjectOfTypeAttribute.cs index 2d18e29..94767c6 100644 --- a/Runtime/FindObjectOfTypeAttribute.cs +++ b/Runtime/FindObjectOfTypeAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; using Object = UnityEngine.Object; @@ -13,7 +13,7 @@ namespace Kogane /// /// Object.FindObjectOfType を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class FindObjectOfTypeAttribute : Attribute, IGetComponentAttribute @@ -30,14 +30,14 @@ public sealed class FindObjectOfTypeAttribute /// /// コンストラクタ /// - public FindObjectOfTypeAttribute() : this( true ) + public FindObjectOfTypeAttribute() : this(true) { } /// /// コンストラクタ /// - public FindObjectOfTypeAttribute( bool includeInactive ) + public FindObjectOfTypeAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -49,17 +49,22 @@ public FindObjectOfTypeAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + var prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); - var fieldType = fieldInfo.FieldType; + var fieldType = fieldInfo.FieldType; - serializedProperty.objectReferenceValue = prefabStage != null && prefabStage.IsPartOfPrefabContents( monoBehaviour.gameObject ) - ? prefabStage.FindComponentOfType( fieldType ) - : Object.FindObjectOfType( fieldType, m_includeInactive ) + serializedProperty.objectReferenceValue = prefabStage != null && prefabStage.IsPartOfPrefabContents(monoBehaviour.gameObject) + ? prefabStage.scene.GetRootGameObjects()[0].GetComponentInChildren(fieldType, m_includeInactive) + : Object.FindObjectOfType(fieldType, m_includeInactive) ; } #endif diff --git a/Runtime/FindObjectsOfTypeAttribute.cs b/Runtime/FindObjectsOfTypeAttribute.cs index c0d8706..1d15d08 100644 --- a/Runtime/FindObjectsOfTypeAttribute.cs +++ b/Runtime/FindObjectsOfTypeAttribute.cs @@ -1,6 +1,7 @@ -using System; +using System; using System.Reflection; using UnityEngine; +using System.Linq; #if UNITY_EDITOR using UnityEditor; using UnityEditor.SceneManagement; @@ -12,7 +13,7 @@ namespace Kogane /// /// Object.FindObjectsOfType を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class FindObjectsOfTypeAttribute : Attribute, IGetComponentAttribute @@ -29,14 +30,14 @@ public sealed class FindObjectsOfTypeAttribute /// /// コンストラクタ /// - public FindObjectsOfTypeAttribute() : this( true ) + public FindObjectsOfTypeAttribute() : this(true) { } /// /// コンストラクタ /// - public FindObjectsOfTypeAttribute( bool includeInactive ) + public FindObjectsOfTypeAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -48,28 +49,33 @@ public FindObjectsOfTypeAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (!serializedProperty.isArray) + { + return; + } + var prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); - var fieldType = fieldInfo.FieldType; - var elementType = fieldType.GetElementType(); + var fieldType = fieldInfo.FieldType; + var elementType = fieldType.GetElementType() ?? fieldType.GetGenericArguments().SingleOrDefault(); var components = prefabStage != null - ? prefabStage.FindComponentsOfType( elementType ) - : UnityEngine.Object.FindObjectsOfType( elementType, m_includeInactive ) + ? prefabStage.scene.GetRootGameObjects()[0].GetComponentsInChildren(elementType, m_includeInactive) + : UnityEngine.Object.FindObjectsOfType(elementType, m_includeInactive) ; var componentCount = components.Length; serializedProperty.arraySize = componentCount; - for ( var i = 0; i < componentCount; i++ ) + for (var i = 0; i < componentCount; i++) { - var element = serializedProperty.GetArrayElementAtIndex( i ); - var component = components[ i ]; + var element = serializedProperty.GetArrayElementAtIndex(i); + var component = components[i]; element.objectReferenceValue = component; } diff --git a/Runtime/FindWithTagAttribute.cs b/Runtime/FindWithTagAttribute.cs index af877c3..87cf48c 100644 --- a/Runtime/FindWithTagAttribute.cs +++ b/Runtime/FindWithTagAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// GameObject.FindWithTag を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class FindWithTagAttribute : Attribute, IGetComponentAttribute @@ -27,7 +27,7 @@ public sealed class FindWithTagAttribute /// /// コンストラクタ /// - public FindWithTagAttribute( string tag ) + public FindWithTagAttribute(string tag) { m_tag = tag; } @@ -38,12 +38,17 @@ public FindWithTagAttribute( string tag ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - serializedProperty.objectReferenceValue = GameObject.FindWithTag( m_tag ); + if (serializedProperty.isArray) + { + return; + } + + serializedProperty.objectReferenceValue = GameObject.FindWithTag(m_tag); } #endif } diff --git a/Runtime/GetComponentAttribute.cs b/Runtime/GetComponentAttribute.cs index ff6dcfd..0c2864a 100644 --- a/Runtime/GetComponentAttribute.cs +++ b/Runtime/GetComponentAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// GetComponent を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentAttribute : Attribute, IGetComponentAttribute @@ -25,13 +25,18 @@ public sealed class GetComponentAttribute /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + serializedProperty.objectReferenceValue = - monoBehaviour.GetComponent( fieldInfo.FieldType ); + monoBehaviour.GetComponent(fieldInfo.FieldType); } #endif } diff --git a/Runtime/GetComponentInChildrenAttribute.cs b/Runtime/GetComponentInChildrenAttribute.cs index fd11c75..7040a6e 100644 --- a/Runtime/GetComponentInChildrenAttribute.cs +++ b/Runtime/GetComponentInChildrenAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// GetComponentInChildren を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentInChildrenAttribute : Attribute, IGetComponentAttribute @@ -27,14 +27,14 @@ public sealed class GetComponentInChildrenAttribute /// /// コンストラクタ /// - public GetComponentInChildrenAttribute() : this( true ) + public GetComponentInChildrenAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentInChildrenAttribute( bool includeInactive ) + public GetComponentInChildrenAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -45,13 +45,18 @@ public GetComponentInChildrenAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + serializedProperty.objectReferenceValue = - monoBehaviour.GetComponentInChildren( fieldInfo.FieldType, m_includeInactive ); + monoBehaviour.GetComponentInChildren(fieldInfo.FieldType, m_includeInactive); } #endif } diff --git a/Runtime/GetComponentInChildrenOnlyAttribute.cs b/Runtime/GetComponentInChildrenOnlyAttribute.cs index c464d3c..39b4123 100644 --- a/Runtime/GetComponentInChildrenOnlyAttribute.cs +++ b/Runtime/GetComponentInChildrenOnlyAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using UnityEngine; @@ -12,7 +12,7 @@ namespace Kogane /// /// 自分自身は対象にしない GetComponentInChildren を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentInChildrenOnlyAttribute : Attribute, IGetComponentAttribute @@ -28,14 +28,14 @@ public sealed class GetComponentInChildrenOnlyAttribute /// /// コンストラクタ /// - public GetComponentInChildrenOnlyAttribute() : this( true ) + public GetComponentInChildrenOnlyAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentInChildrenOnlyAttribute( bool includeInactive ) + public GetComponentInChildrenOnlyAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -46,14 +46,19 @@ public GetComponentInChildrenOnlyAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + serializedProperty.objectReferenceValue = monoBehaviour - .GetComponentsInChildren( fieldInfo.FieldType, m_includeInactive ) - .FirstOrDefault( x => x.gameObject != monoBehaviour.gameObject ) + .GetComponentsInChildren(fieldInfo.FieldType, m_includeInactive) + .FirstOrDefault(x => x.gameObject != monoBehaviour.gameObject) ; } #endif diff --git a/Runtime/GetComponentInParentAttribute.cs b/Runtime/GetComponentInParentAttribute.cs index 91df665..aec2866 100644 --- a/Runtime/GetComponentInParentAttribute.cs +++ b/Runtime/GetComponentInParentAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// GetComponentInParent を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentInParentAttribute : Attribute, IGetComponentAttribute @@ -25,13 +25,18 @@ public sealed class GetComponentInParentAttribute /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + serializedProperty.objectReferenceValue = - monoBehaviour.GetComponentInParent( fieldInfo.FieldType ); + monoBehaviour.GetComponentInParent(fieldInfo.FieldType); } #endif } diff --git a/Runtime/GetComponentInParentOnlyAttribute.cs b/Runtime/GetComponentInParentOnlyAttribute.cs index 4f2b5f2..fa01be7 100644 --- a/Runtime/GetComponentInParentOnlyAttribute.cs +++ b/Runtime/GetComponentInParentOnlyAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using UnityEngine; @@ -12,7 +12,7 @@ namespace Kogane /// /// 自分自身は対象にしない GetComponentInParent を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentInParentOnlyAttribute : Attribute, IGetComponentAttribute @@ -28,14 +28,14 @@ public sealed class GetComponentInParentOnlyAttribute /// /// コンストラクタ /// - public GetComponentInParentOnlyAttribute() : this( true ) + public GetComponentInParentOnlyAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentInParentOnlyAttribute( bool includeInactive ) + public GetComponentInParentOnlyAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -46,14 +46,19 @@ public GetComponentInParentOnlyAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + serializedProperty.objectReferenceValue = monoBehaviour - .GetComponentsInParent( fieldInfo.FieldType, m_includeInactive ) - .FirstOrDefault( x => x.gameObject != monoBehaviour.gameObject ) + .GetComponentsInParent(fieldInfo.FieldType, m_includeInactive) + .FirstOrDefault(x => x.gameObject != monoBehaviour.gameObject) ; } #endif diff --git a/Runtime/GetComponentsAttribute.cs b/Runtime/GetComponentsAttribute.cs index 9230003..24ede64 100644 --- a/Runtime/GetComponentsAttribute.cs +++ b/Runtime/GetComponentsAttribute.cs @@ -1,6 +1,7 @@ -using System; +using System; using System.Reflection; using UnityEngine; +using System.Linq; #if UNITY_EDITOR using UnityEditor; @@ -11,7 +12,7 @@ namespace Kogane /// /// GetComponents を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentsAttribute : Attribute, IGetComponentAttribute @@ -25,22 +26,28 @@ public sealed class GetComponentsAttribute /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - var fieldType = fieldInfo.FieldType; - var elementType = fieldType.GetElementType(); - var components = monoBehaviour.GetComponents( elementType ); + if (!serializedProperty.isArray) + { + return; + } + + var fieldType = fieldInfo.FieldType; + var elementType = fieldType.GetElementType() ?? fieldType.GetGenericArguments().SingleOrDefault(); + + var components = monoBehaviour.GetComponents(elementType); var componentCount = components.Length; serializedProperty.arraySize = componentCount; - for ( var i = 0; i < componentCount; i++ ) + for (var i = 0; i < componentCount; i++) { - var element = serializedProperty.GetArrayElementAtIndex( i ); - var component = components[ i ]; + var element = serializedProperty.GetArrayElementAtIndex(i); + var component = components[i]; element.objectReferenceValue = component; } diff --git a/Runtime/GetComponentsInChildrenAttribute.cs b/Runtime/GetComponentsInChildrenAttribute.cs index 63c4ac5..f4711a0 100644 --- a/Runtime/GetComponentsInChildrenAttribute.cs +++ b/Runtime/GetComponentsInChildrenAttribute.cs @@ -1,6 +1,7 @@ -using System; +using System; using System.Reflection; using UnityEngine; +using System.Linq; #if UNITY_EDITOR using UnityEditor; @@ -11,7 +12,7 @@ namespace Kogane /// /// GetComponentsInChildren を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentsInChildrenAttribute : Attribute, IGetComponentAttribute @@ -27,14 +28,14 @@ public sealed class GetComponentsInChildrenAttribute /// /// コンストラクタ /// - public GetComponentsInChildrenAttribute() : this( true ) + public GetComponentsInChildrenAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentsInChildrenAttribute( bool includeInactive ) + public GetComponentsInChildrenAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -45,27 +46,28 @@ public GetComponentsInChildrenAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - var fieldType = fieldInfo.FieldType; - var elementType = fieldType.GetElementType(); + if (!serializedProperty.isArray) + { + return; + } - // 配列やリストではないフィールドに GetComponentsInChildrenAttribute を付けると - // elementType が null になる - if ( elementType == null ) return; + var fieldType = fieldInfo.FieldType; + var elementType = fieldType.GetElementType() ?? fieldType.GetGenericArguments().SingleOrDefault(); - var components = monoBehaviour.GetComponentsInChildren( elementType, m_includeInactive ); + var components = monoBehaviour.GetComponentsInChildren(elementType, m_includeInactive); var componentCount = components.Length; serializedProperty.arraySize = componentCount; - for ( var i = 0; i < componentCount; i++ ) + for (var i = 0; i < componentCount; i++) { - var element = serializedProperty.GetArrayElementAtIndex( i ); - var component = components[ i ]; + var element = serializedProperty.GetArrayElementAtIndex(i); + var component = components[i]; element.objectReferenceValue = component; } diff --git a/Runtime/GetComponentsInChildrenOnlyAttribute.cs b/Runtime/GetComponentsInChildrenOnlyAttribute.cs index 60da26c..6201692 100644 --- a/Runtime/GetComponentsInChildrenOnlyAttribute.cs +++ b/Runtime/GetComponentsInChildrenOnlyAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using UnityEngine; @@ -12,7 +12,7 @@ namespace Kogane /// /// 自分自身は対象にしない GetComponentsInChildren を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentsInChildrenOnlyAttribute : Attribute, IGetComponentAttribute @@ -28,14 +28,14 @@ public sealed class GetComponentsInChildrenOnlyAttribute /// /// コンストラクタ /// - public GetComponentsInChildrenOnlyAttribute() : this( true ) + public GetComponentsInChildrenOnlyAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentsInChildrenOnlyAttribute( bool includeInactive ) + public GetComponentsInChildrenOnlyAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -46,17 +46,22 @@ public GetComponentsInChildrenOnlyAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - var fieldType = fieldInfo.FieldType; - var elementType = fieldType.GetElementType(); + if (!serializedProperty.isArray) + { + return; + } + + var fieldType = fieldInfo.FieldType; + var elementType = fieldType.GetElementType() ?? fieldType.GetGenericArguments().SingleOrDefault(); var components = monoBehaviour - .GetComponentsInChildren( elementType, m_includeInactive ) - .Where( x => x.gameObject != monoBehaviour.gameObject ) + .GetComponentsInChildren(elementType, m_includeInactive) + .Where(x => x.gameObject != monoBehaviour.gameObject) .ToArray() ; @@ -64,10 +69,10 @@ SerializedProperty serializedProperty serializedProperty.arraySize = componentCount; - for ( var i = 0; i < componentCount; i++ ) + for (var i = 0; i < componentCount; i++) { - var element = serializedProperty.GetArrayElementAtIndex( i ); - var component = components[ i ]; + var element = serializedProperty.GetArrayElementAtIndex(i); + var component = components[i]; element.objectReferenceValue = component; } diff --git a/Runtime/GetComponentsInParentAttribute.cs b/Runtime/GetComponentsInParentAttribute.cs index a200751..c3ad33c 100644 --- a/Runtime/GetComponentsInParentAttribute.cs +++ b/Runtime/GetComponentsInParentAttribute.cs @@ -1,6 +1,7 @@ -using System; +using System; using System.Reflection; using UnityEngine; +using System.Linq; #if UNITY_EDITOR using UnityEditor; @@ -11,7 +12,7 @@ namespace Kogane /// /// GetComponentsInParent を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentsInParentAttribute : Attribute, IGetComponentAttribute @@ -27,14 +28,14 @@ public sealed class GetComponentsInParentAttribute /// /// コンストラクタ /// - public GetComponentsInParentAttribute() : this( true ) + public GetComponentsInParentAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentsInParentAttribute( bool includeInactive ) + public GetComponentsInParentAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -45,22 +46,28 @@ public GetComponentsInParentAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - var fieldType = fieldInfo.FieldType; - var elementType = fieldType.GetElementType(); - var components = monoBehaviour.GetComponentsInParent( elementType, m_includeInactive ); + if (!serializedProperty.isArray) + { + return; + } + + var fieldType = fieldInfo.FieldType; + var elementType = fieldType.GetElementType() ?? fieldType.GetGenericArguments().SingleOrDefault(); + + var components = monoBehaviour.GetComponentsInParent(elementType, m_includeInactive); var componentCount = components.Length; serializedProperty.arraySize = componentCount; - for ( var i = 0; i < componentCount; i++ ) + for (var i = 0; i < componentCount; i++) { - var element = serializedProperty.GetArrayElementAtIndex( i ); - var component = components[ i ]; + var element = serializedProperty.GetArrayElementAtIndex(i); + var component = components[i]; element.objectReferenceValue = component; } diff --git a/Runtime/GetComponentsInParentOnlyAttribute.cs b/Runtime/GetComponentsInParentOnlyAttribute.cs index f5be091..fbde2e5 100644 --- a/Runtime/GetComponentsInParentOnlyAttribute.cs +++ b/Runtime/GetComponentsInParentOnlyAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using UnityEngine; @@ -12,7 +12,7 @@ namespace Kogane /// /// 自分自身は対象にしない GetComponentsInParent を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetComponentsInParentOnlyAttribute : Attribute, IGetComponentAttribute @@ -28,14 +28,14 @@ public sealed class GetComponentsInParentOnlyAttribute /// /// コンストラクタ /// - public GetComponentsInParentOnlyAttribute() : this( true ) + public GetComponentsInParentOnlyAttribute() : this(true) { } /// /// コンストラクタ /// - public GetComponentsInParentOnlyAttribute( bool includeInactive ) + public GetComponentsInParentOnlyAttribute(bool includeInactive) { m_includeInactive = includeInactive; } @@ -46,17 +46,22 @@ public GetComponentsInParentOnlyAttribute( bool includeInactive ) /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { - var fieldType = fieldInfo.FieldType; - var elementType = fieldType.GetElementType(); + if (!serializedProperty.isArray) + { + return; + } + + var fieldType = fieldInfo.FieldType; + var elementType = fieldType.GetElementType() ?? fieldType.GetGenericArguments().SingleOrDefault(); var components = monoBehaviour - .GetComponentsInParent( elementType, m_includeInactive ) - .Where( x => x.gameObject != monoBehaviour.gameObject ) + .GetComponentsInParent(elementType, m_includeInactive) + .Where(x => x.gameObject != monoBehaviour.gameObject) .ToArray() ; @@ -64,10 +69,10 @@ SerializedProperty serializedProperty serializedProperty.arraySize = componentCount; - for ( var i = 0; i < componentCount; i++ ) + for (var i = 0; i < componentCount; i++) { - var element = serializedProperty.GetArrayElementAtIndex( i ); - var component = components[ i ]; + var element = serializedProperty.GetArrayElementAtIndex(i); + var component = components[i]; element.objectReferenceValue = component; } diff --git a/Runtime/GetOrAddComponentAttribute.cs b/Runtime/GetOrAddComponentAttribute.cs index 85e143a..a771024 100644 --- a/Runtime/GetOrAddComponentAttribute.cs +++ b/Runtime/GetOrAddComponentAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using UnityEngine; #if UNITY_EDITOR @@ -11,7 +11,7 @@ namespace Kogane /// /// GetOrAddComponent を実行する Attribute /// - [AttributeUsage( AttributeTargets.Field )] + [AttributeUsage(AttributeTargets.Field)] public sealed class GetOrAddComponentAttribute : Attribute, IGetComponentAttribute @@ -25,16 +25,21 @@ public sealed class GetOrAddComponentAttribute /// public void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ) { + if (serializedProperty.isArray) + { + return; + } + var fieldType = fieldInfo.FieldType; - if ( !monoBehaviour.TryGetComponent( fieldType, out var component ) ) + if (!monoBehaviour.TryGetComponent(fieldType, out var component)) { - component = monoBehaviour.gameObject.AddComponent( fieldType ); + component = monoBehaviour.gameObject.AddComponent(fieldType); } serializedProperty.objectReferenceValue = component; diff --git a/Runtime/IGetComponentAttribute.cs b/Runtime/IGetComponentAttribute.cs index 90a3c07..f04bfb7 100644 --- a/Runtime/IGetComponentAttribute.cs +++ b/Runtime/IGetComponentAttribute.cs @@ -1,4 +1,4 @@ -using System.Reflection; +using System.Reflection; using UnityEngine; #if UNITY_EDITOR using UnityEditor; @@ -21,8 +21,8 @@ public interface IGetComponentAttribute /// void Inject ( - MonoBehaviour monoBehaviour, - FieldInfo fieldInfo, + MonoBehaviour monoBehaviour, + FieldInfo fieldInfo, SerializedProperty serializedProperty ); #endif