Unityã§ãå¤æ°ãèªåçã«GetComponentããå±æ§ãç´¹ä»ãã¾ãã
Â
æ¯åãã¹ã¯ãªããå ã§GetComponentãè¨è¿°ããã®ã¯é¢åã§ã¯ãªãã§ããï¼
å¤æ°ã®è¿½å ãåé¤ããããã³ã«Starté¢æ°ããã§ãã¯ããGetComponentã®å¦çã追å ãã¦åé¤ãã¦â¦ã¨ããä½æ¥ã«è¾æãã¦ããæ¹ãå¤ããã¨æãã¾ãã
ä»åç´¹ä»ããã³ã¼ãã§ããã®ç ©ããããã解æ¾ãããããããã¾ããã
Â
æ¢ã«å é§è ããã¾ããããã®è¨äºã®ã³ã¼ãã§ã¯ã
- privateã¡ã³ã
- éã·ãªã¢ã©ã¤ãºã¡ã³ã
- ããããã£
- ã¤ã³ã¿ã¼ãã§ã¤ã¹
- é å
- 親åé¢ä¿ã®ãªãã¸ã§ã¯ã
ã«ãé©ç¨ã§ãã¾ãã
Â
Â
Â
Â
使ãæ¹
â ã¹ã¯ãªãããããã¸ã§ã¯ãã«ä¿å
â¡BaseMonoBehaviourãç¶æ¿
Â
Â
â ã¹ã¯ãªãããããã¸ã§ã¯ãã«ä¿å
https:// drive.google.com/file/d/1zc9ICMUm58X5X7WtOfPuX8ff5Selw314/view?usp=drive_linkï¼æ§çï¼
Â
ä¸è¨ã®ããã±ã¼ã¸ããããã¸ã§ã¯ãã«ã¤ã³ã¹ãã¼ã«ãã¦ãã ããã
Â
â¡BaseMonoBehaviourãç¶æ¿
å©ç¨ãããã¯ã©ã¹ã«ãBaseMonoBehaviourãç¶æ¿ããã¾ãã
åå¾ãããã£ã¼ã«ãã«ã[GetComponent]å±æ§ãä»ä¸ãã¦ãã ããã
Â
ãµã³ãã«
using UnityEngine;
using System.Linq;
using MantenseiLib; //å¿ããã«ï¼
public class Test01 : BaseMonoBehaviour //ç¶æ¿ãå¿ããã«ï¼
{
//ããã©ã«ãã®è¨æ³
[GetComponent] SpriteRenderer sr;
//é層æå®ããå ´å
[GetComponent(relation = HierarchyRelation.Self | HierarchyRelation.Parent)] CircleCollider2D col;
//ããããã£ã«ä½¿ã
[AddComponent] public Rigidbody2D Rb2d { get; private set; }
//é
åã«ä½¿ã
[GetComponents(relation = HierarchyRelation.All)]
public Collider2D[] Colliders { get; private set; }
private void Start()
{
Debug.Log(sr);
Debug.Log(col);
Debug.Log(Rb2d);
Debug.Log(string.Join(", ", Colliders.Select(x => x.gameObject.name)));
}
}
Â
ã§ã¯ãä¸è¨ã®ã¹ã¯ãªãããã¢ã¿ãããã¦ã¿ã¾ãããã
Â
å®è¡ãã¾ãã
Â
Â
Â
Â
ã¾ã¨ã
BaseMonoBehaviourãç¶æ¿ããæ¹æ³ãç´¹ä»ãã¾ããããããã©ã¼ãã³ã¹ãæ°ã«ãªãå ´åã¯ãåå¥ã«GetOrAddComponent()ãå¼ã³åºããããFindObjectOfType()ãªã©ã§ä¸æ¬åå¾ãããããæ¹æ³ã試ãã¨ããããããã¾ããã
ãã ãå¼ã³åºããé¢åã ã£ãããã²ã¼ã ã®éä¸ã§Instantiateãããªãã¸ã§ã¯ãã«ã¯å¹ããªãã£ãããããããæçµçã«ã¯ç¶æ¿ã«è½ã¡çããããªæ°ããã¾ãã
ï¼ããããå¼ã³åºãæ¹æ³ãããã°å ±åãããã°ãã²æãã¦ãã ããï¼
Â
Â
â»æ³¨æ
- ParentãChildrenãæå®ããå ´åãSelfãæå®ãã¦ããªãã¦ãèªåèªèº«ã®ã³ã³ãã¼ãã³ããåå¾ããã¾ãï¼GetComponentInParentãGetComponentInChildrenãèªåèªèº«ã対象ã«ããããï¼ããParentãChildrenãæå®ããéãèªåèªèº«ã®ã³ã³ãã¼ãã³ããå«ã¾ãªãããã«å¤æ´ãã¾ããï¼
- [AddComponent]ã¯è¦ªåãªãã¸ã§ã¯ãã«ã¯ä½¿ç¨ã§ãã¾ããã
Â
Â
Â
Â
ä»å使ç¨ããã³ã¼ãã¯ãã¡ãã§ãã
AttributeUtility
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using UnityEngine;
namespace MantenseiLib
{
public static partial class AttributeUtility
{
///
/// æå®ããããããã£ã¨å±æ§ã®ãã¢ãè¿ãã¾ãã
///
public static IEnumerable<(MemberInfo memberInfo, T attribute)> GetAttributedFields(object obj) where T : Attribute
{
Type type = obj.GetType();
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
var fieldPairs = GetAttributePairs(fields);
var propertyPairs = GetAttributePairs(properties);
return fieldPairs.Concat(propertyPairs);
}
static IEnumerable<(MemberInfo, T)> GetAttributePairs(IEnumerable members) where T : Attribute
{
foreach (var member in members)
{
var attributes = GetAttributes(member);
foreach (var attribute in attributes)
yield return (member, attribute);
}
}
public static IEnumerable GetAttributes(MemberInfo info) where T : Attribute
{
var attributes = Attribute.GetCustomAttributes(info, typeof(T), true);
if (attributes == null)
yield break;
foreach (var attribute in attributes)
yield return (T)attribute;
}
}
}
Â
GetComponentAttributes
using System;
namespace MantenseiLib
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class GetComponentAttribute : Attribute
{
public HierarchyRelation relation { get; set; } = HierarchyRelation.Self;
public virtual QuantityType quantity { get; } = QuantityType.Single;
public virtual GetComponentType GetComponentType { get; } = GetComponentType.GetComponent;
public bool HideErrorHandling { get; set; } = false;
public GetComponentAttribute() { }
public GetComponentAttribute(HierarchyRelation relation) { this.relation = relation; }
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class GetComponentsAttribute : GetComponentAttribute
{
public override QuantityType quantity => QuantityType.Multiple;
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class AddComponentAttribute : GetComponentAttribute
{
public override GetComponentType GetComponentType { get => GetComponentType.AddComponent; }
}
[Flags]
public enum HierarchyRelation
{
Self = 1, // èªåèªèº«
Parent = 2, // 親
Children = 4, // å
None = 0,
All = Self | Parent | Children,
}
public enum GetComponentType
{
GetComponent,
AddComponent,
}
public enum QuantityType
{
Single,
Multiple
}
}
Â
GetComponentUtility
using System;
using System;
using System.Reflection;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
namespace MantenseiLib
{
public static class GetComponentUtility
{
static bool _hideErrorHandling = false;
public static void GetOrAddComponent(MonoBehaviour monoBehaviour)
{
var getComponentPairs = AttributeUtility.GetAttributedFields(monoBehaviour);
foreach (var info in getComponentPairs)
{
object component = null;
var memberInfo = info.memberInfo;
var attribute = info.attribute;
switch (attribute.GetComponentType)
{
case GetComponentType.GetComponent:
component = GetComponentByRelations(monoBehaviour, memberInfo, attribute.relation, attribute.quantity);
break;
case GetComponentType.AddComponent:
component = AddComponentByRelation(monoBehaviour, memberInfo, attribute.relation);
break;
}
if (component == null && !attribute.HideErrorHandling && !_hideErrorHandling)
{
Debug.LogWarning($"\"{memberInfo.GetMemberType()}\" ã® \"{memberInfo.Name}\" ã® \"{monoBehaviour.name}\" ãè¦ã¤ããã¾ãã");
}
else
{
SetComponent(monoBehaviour, memberInfo, component);
}
}
}
private static object GetComponentByRelations(MonoBehaviour obj, MemberInfo memberInfo, HierarchyRelation relations, QuantityType quantity)
{
Type componentType = memberInfo.GetMemberType();
bool isArray = componentType.IsArray;
Type elementType = isArray ? componentType.GetElementType() : componentType;
object components = null;
if (quantity == QuantityType.Single)
{
foreach (HierarchyRelation relation in Enum.GetValues(typeof(HierarchyRelation)))
{
if (relation != HierarchyRelation.None && relations.HasFlag(relation))
{
components = GetComponentByRelation(obj, elementType, relation, quantity);
if (components as UnityEngine.Object != null) // Objectã®nullã¯æåãéãã®ã§ãã£ã¹ããã¦nullãã§ãã¯
{
break;
}
}
}
}
else if (quantity == QuantityType.Multiple)
{
List
Â
BaseMonoBehaviour
using System.Collections;
using System.Collections.Generic;
using MantenseiLib;
using UnityEngine;
namespace MantenseiLib
{
public abstract partial class BaseMonoBehaviour : MonoBehaviour
{
protected virtual void Awake()
{
GetComponentUtility.GetOrAddComponent(this);
}
}
}