InterfaceAttribute.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using UnityEngine;
  2. using System;
  3. namespace Rokid.UXR.Interaction {
  4. /// <summary>
  5. /// When this attribute is attached to a MonoBehaviour field within a
  6. /// Unity Object, this allows an interface to be specified in to to
  7. /// entire only a specific type of MonoBehaviour can be attached.
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
  10. public class InterfaceAttribute : PropertyAttribute
  11. {
  12. public Type[] Types = null;
  13. public string TypeFromFieldName;
  14. /// <summary>
  15. /// Creates a new Interface attribute.
  16. /// </summary>
  17. /// <param name="type">The type of interface which is allowed.</param>
  18. /// <param name="types">Extra types of interface which is allowed.</param>
  19. public InterfaceAttribute(Type type, params Type[] types)
  20. {
  21. Debug.Log(type.Name + $" {type.Name} needs to be an interface.");
  22. Debug.Assert(type.IsInterface, $"{type.Name} needs to be an interface.");
  23. Types = new Type[types.Length + 1];
  24. Types[0] = type;
  25. for (int i = 0; i < types.Length; i++)
  26. {
  27. Debug.Assert(types[i].IsInterface, $"{types[i].Name} needs to be an interface.");
  28. Types[i + 1] = types[i];
  29. }
  30. }
  31. public InterfaceAttribute(string typeFromFieldName)
  32. {
  33. this.TypeFromFieldName = typeFromFieldName;
  34. }
  35. }
  36. }