Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Runtime/FindAssetsAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
Expand All @@ -13,7 +13,7 @@ namespace Kogane
/// <summary>
/// AssetDatabase.FindAssets を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FindAssetsAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -27,20 +27,20 @@ public sealed class FindAssetsAttribute
/// </summary>
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<Object>( assetPath );
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
var asset = AssetDatabase.LoadAssetAtPath<Object>(assetPath);

serializedProperty.objectReferenceValue = asset;
}
Expand Down
17 changes: 11 additions & 6 deletions Runtime/FindAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
Expand All @@ -11,7 +11,7 @@ namespace Kogane
/// <summary>
/// GameObject.Find を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FindAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -27,7 +27,7 @@ public sealed class FindAttribute
/// <summary>
/// コンストラクタ
/// </summary>
public FindAttribute( string name )
public FindAttribute(string name)
{
m_name = name;
}
Expand All @@ -38,12 +38,17 @@ public FindAttribute( string name )
/// </summary>
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
}
Expand Down
17 changes: 11 additions & 6 deletions Runtime/FindChildAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
Expand All @@ -11,7 +11,7 @@ namespace Kogane
/// <summary>
/// Transform.Find を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FindChildAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -27,7 +27,7 @@ public sealed class FindChildAttribute
/// <summary>
/// コンストラクタ
/// </summary>
public FindChildAttribute( string name )
public FindChildAttribute(string name)
{
m_name = name;
}
Expand All @@ -38,12 +38,17 @@ public FindChildAttribute( string name )
/// </summary>
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
}
Expand Down
25 changes: 15 additions & 10 deletions Runtime/FindObjectOfTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using UnityEngine;
using Object = UnityEngine.Object;
Expand All @@ -13,7 +13,7 @@ namespace Kogane
/// <summary>
/// Object.FindObjectOfType を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FindObjectOfTypeAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -30,14 +30,14 @@ public sealed class FindObjectOfTypeAttribute
/// <summary>
/// コンストラクタ
/// </summary>
public FindObjectOfTypeAttribute() : this( true )
public FindObjectOfTypeAttribute() : this(true)
{
}

/// <summary>
/// コンストラクタ
/// </summary>
public FindObjectOfTypeAttribute( bool includeInactive )
public FindObjectOfTypeAttribute(bool includeInactive)
{
m_includeInactive = includeInactive;
}
Expand All @@ -49,17 +49,22 @@ public FindObjectOfTypeAttribute( bool includeInactive )
/// </summary>
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
Expand Down
32 changes: 19 additions & 13 deletions Runtime/FindObjectsOfTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System;
using System.Reflection;
using UnityEngine;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
Expand All @@ -12,7 +13,7 @@ namespace Kogane
/// <summary>
/// Object.FindObjectsOfType を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FindObjectsOfTypeAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -29,14 +30,14 @@ public sealed class FindObjectsOfTypeAttribute
/// <summary>
/// コンストラクタ
/// </summary>
public FindObjectsOfTypeAttribute() : this( true )
public FindObjectsOfTypeAttribute() : this(true)
{
}

/// <summary>
/// コンストラクタ
/// </summary>
public FindObjectsOfTypeAttribute( bool includeInactive )
public FindObjectsOfTypeAttribute(bool includeInactive)
{
m_includeInactive = includeInactive;
}
Expand All @@ -48,28 +49,33 @@ public FindObjectsOfTypeAttribute( bool includeInactive )
/// </summary>
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;
}
Expand Down
17 changes: 11 additions & 6 deletions Runtime/FindWithTagAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
Expand All @@ -11,7 +11,7 @@ namespace Kogane
/// <summary>
/// GameObject.FindWithTag を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FindWithTagAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -27,7 +27,7 @@ public sealed class FindWithTagAttribute
/// <summary>
/// コンストラクタ
/// </summary>
public FindWithTagAttribute( string tag )
public FindWithTagAttribute(string tag)
{
m_tag = tag;
}
Expand All @@ -38,12 +38,17 @@ public FindWithTagAttribute( string tag )
/// </summary>
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
}
Expand Down
15 changes: 10 additions & 5 deletions Runtime/GetComponentAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
Expand All @@ -11,7 +11,7 @@ namespace Kogane
/// <summary>
/// GetComponent を実行する Attribute
/// </summary>
[AttributeUsage( AttributeTargets.Field )]
[AttributeUsage(AttributeTargets.Field)]
public sealed class GetComponentAttribute
: Attribute,
IGetComponentAttribute
Expand All @@ -25,13 +25,18 @@ public sealed class GetComponentAttribute
/// </summary>
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
}
Expand Down
Loading