Unity Editor模式编辑属性即时生效

1.创建的标签名(SetProperty)

public class SetPropertyAttribute : PropertyAttribute

{

    public string Name { get; private set; }

    public bool IsDirty { get; set; }

 

    public SetPropertyAttribute(string name)

    {

        this.Name = name;

    }

}

2.在Editor文件夹创建

 

// Copyright (c) 2014 Luminary LLC

// Licensed under The MIT License (See LICENSE for full text)

using UnityEngine;

using UnityEditor;

using System;

using System.Collections;

using System.Reflection;

 

[CustomPropertyDrawer(typeof(SetPropertyAttribute))]

public class SetPropertyDrawer : PropertyDrawer

{

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)

    {

        // Rely on the default inspector GUI

        EditorGUI.BeginChangeCheck ();

        EditorGUI.PropertyField(position, property, label);

 

        // Update only when necessary

        SetPropertyAttribute setProperty = attribute as SetPropertyAttribute;

        if (EditorGUI.EndChangeCheck())

        {

            // When a SerializedProperty is modified the actual field does not have the current value set (i.e.  

            // FieldInfo.GetValue() will return the prior value that was set) until after this OnGUI call has completed. 

            // Therefore, we need to mark this property as dirty, so that it can be updated with a subsequent OnGUI event 

            // (e.g. Repaint)

            setProperty.IsDirty = true;

        } 

        else if (setProperty.IsDirty)

        {

            // The propertyPath may reference something that is a child field of a field on this Object, so it is necessary

            // to find which object is the actual parent before attempting to set the property with the current value.

            object parent = GetParentObjectOfProperty(property.propertyPath, property.serializedObject.targetObject);

            Type type = parent.GetType();

            PropertyInfo pi = type.GetProperty(setProperty.Name);

            if (pi == null)

            {

                Debug.LogError("Invalid property name: " + setProperty.Name + "\nCheck your [SetProperty] attribute");

            }

            else

            {

                // Use FieldInfo instead of the SerializedProperty accessors as we'd have to deal with every 

                // SerializedPropertyType and use the correct accessor

                pi.SetValue(parent, fieldInfo.GetValue(parent), null);

            }

            setProperty.IsDirty = false;

        }

    }

    

    private object GetParentObjectOfProperty(string path, object obj)

    {

        string[] fields = path.Split('.');

 

        // We've finally arrived at the final object that contains the property

        if (fields.Length == 1)

        {

            return obj;

        }

 

        // We may have to walk public or private fields along the chain to finding our container object, so we have to allow for both

        FieldInfo fi = obj.GetType().GetField(fields[0], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

        obj = fi.GetValue(obj);

 

        // Keep searching for our object that contains the property

        return GetParentObjectOfProperty(string.Join(".", fields, 1, fields.Length - 1), obj);

    }

}

3.Demo

public class SetPropertyTest : MonoBehaviour

{

    [SerializeField,SetProperty("PosX")]

    private float posX;

    public float PosX{

        get{

            return posX;

        }

        set{

            posX = value;

            Debug.Log (posX);

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值