Ejemplo n.º 1
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //if (property.objectReferenceValue != null)
        //{
        //    EditorGUI.PropertyField(position, property, label);
        //    return;
        //}
        AutohookAttribute autohookAttribute = (AutohookAttribute)attribute;

        // check if auto set hookType
        if (autohookAttribute.hookType == AutohookAttribute.HookType.Auto)
        {
            if (property.name.EndsWith("Prefab") && GetTypeFromProperty(property).Equals(typeof(GameObject)))
            {
                autohookAttribute.hookType = AutohookAttribute.HookType.Prefab;
            }
            else
            {
                autohookAttribute.hookType = AutohookAttribute.HookType.Component;
            }
        }

        if (autohookAttribute.hookType == AutohookAttribute.HookType.Component)
        {
            //Assert.IsFalse(GetTypeFromProperty(property).Equals(typeof(GameObject)),
            //    "use [Autohook] to modify "+property.name);
            var component = FindAutohookTarget(property);
            if (component != null)
            {
                //  if (property.objectReferenceValue == null)
                property.objectReferenceValue = component;
            }
        }
        else
        {
            //Assert.IsTrue(GetTypeFromProperty(property).Equals(typeof(GameObject)),
            //    "use [Autohook(AutohookAttribute.HookType.Prefab)] to modify " + property.name);
            string name = property.name;
            if (name.EndsWith("Prefab"))
            {
                name = name.Remove(name.Length - 6);
            }
            var prefab = FindPrefab(property, name, autohookAttribute.prefabPath);
            if (prefab != null)
            {
                property.objectReferenceValue = prefab;
            }
        }


        EditorGUI.PropertyField(position, property, label);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Takes a SerializedProperty and finds a local component that can be slotted into it.
    /// Local in this context means its a component attached to the same GameObject.
    /// This could easily be changed to use GetComponentInParent/GetComponentInChildren
    /// </summary>
    /// <param name="property"></param>
    /// <returns></returns>
    private Component FindAutohookTarget(SerializedProperty property)
    {
        var root = property.serializedObject;

        if (root.targetObject is Component)
        {
            // first, lets find the type of component were trying to autohook...
            var type = GetTypeFromProperty(property);

            if (type == null)
            {
                return(null);
            }

            // ...then use GetComponent(type) to see if there is one on our object.
            var component = (Component)root.targetObject;

            Component ret = component.GetComponent(type);

            if (ret != null)
            {
                return(ret);
            }

            AutohookAttribute autohookAttribute = PropertyUtility.GetAttribute <AutohookAttribute>(property);
            switch (autohookAttribute.Mode)
            {
            case AutohookAttribute.AutohookMode.AlsoChildren:
                return(component.GetComponentInChildren(type));

            case AutohookAttribute.AutohookMode.AlsoParent:
                return(component.GetComponentInParent(type));

            case AutohookAttribute.AutohookMode.AllParents: {
                Transform t = component.transform.parent;
                while (t != null)
                {
                    ret = t.GetComponent(type);
                    if (ret != null)
                    {
                        return(ret);
                    }
                    t = t.parent;
                }
                return(null);
            }

            case AutohookAttribute.AutohookMode.SiblingsAndChildren: {
                return(component.transform.parent?.GetComponentInChildren(type));
            }

            case AutohookAttribute.AutohookMode.UpwardsAllChildren: {
                Transform t = component.transform.parent;
                while (t != null)
                {
                    ret = t.GetComponentInChildren(type);
                    if (ret != null)
                    {
                        return(ret);
                    }
                    t = t.parent;
                }
                return(null);
            }
            }


            return(ret);
        }
        else
        {
            Debug.Log("OH NO handle fails here better pls");
        }

        return(null);
    }