using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
[CustomEditor(typeof(Node))]
class NodeCustomEditor : Editor
{
private void OnEnable()
{
MonoBehaviour[] sceneActive = GameObject.FindObjectsOfType<MonoBehaviour>();
foreach (MonoBehaviour mono in sceneActive)
{
Type monoType = mono.GetType();
// Retreive the fields from the mono instance
FieldInfo[] objectFields = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public);
// search all fields and find the attribute [Position]
for (int i = 0; i < objectFields.Length; i++)
{
PositionAttribute attribute = Attribute.GetCustomAttribute(objectFields[i], typeof(PositionAttribute)) as PositionAttribute;
// if we detect any attribute print out the data.
if (attribute != null)
{
Debug.Log(attribute.position.x); // The attribute position Y for that instance variable "1"
Debug.Log(attribute.position.y); // The attribute position X for that instance variable "0"
Debug.Log(mono.GetType()); // The type of the mono script "Node"
Debug.Log(objectFields[i].Name); // The name of the marked variable - "node"
}
}
}
}
}
using UnityEngine;
public class Node : MonoBehaviour
{
[Position(1, 0)]
public object node;
}
using UnityEngine;
using System;
[AttributeUsage(AttributeTargets.Field)]
public class PositionAttribute : Attribute
{
public readonly Vector2 position;
public PositionAttribute(int x, int y)
{
position.x = x;
position.y = y;
}
}