浏览代码

Added InitOnlyAttribute

InitOnlyAttribute adds a helpbox in the inspector during play mode to indicate that the field with the attribute is only used for initialization, and thus will only affect new instances, or after re-entering play mode.
/main
DeivSky 4 年前
当前提交
49c14d67
共有 6 个文件被更改,包括 76 次插入0 次删除
  1. 38
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs
  2. 11
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs.meta
  3. 8
      UOP1_Project/Assets/Scripts/StateMachine/Utilities.meta
  4. 8
      UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs
  5. 11
      UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs.meta

38
UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs


using UnityEditor;
using UnityEngine;
namespace UOP1.StateMachine.Editor
{
[CustomPropertyDrawer(typeof(InitOnlyAttribute))]
public class InitOnlyAttributeDrawer : PropertyDrawer
{
private static readonly string _text = "Changes to this field will only take effect on new state machine instances, or the next time you enter play mode.";
private static readonly GUIStyle _style = new GUIStyle(GUI.skin.GetStyle("helpbox")) { padding = new RectOffset(5, 5, 5, 5) };
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (EditorApplication.isPlaying)
{
position.height = _style.CalcHeight(new GUIContent(_text), EditorGUIUtility.currentViewWidth);
EditorGUI.HelpBox(position, _text, MessageType.Info);
position.y += position.height + EditorGUIUtility.standardVerticalSpacing;
position.height = EditorGUI.GetPropertyHeight(property, label);
}
EditorGUI.PropertyField(position, property, label);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = EditorGUI.GetPropertyHeight(property, label);
if (EditorApplication.isPlaying)
{
height += _style.CalcHeight(new GUIContent(_text), EditorGUIUtility.currentViewWidth)
+ EditorGUIUtility.standardVerticalSpacing;
}
return height;
}
}
}

11
UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs.meta


fileFormatVersion: 2
guid: b95d43eb2b350bb4daea0ec542dbff60
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/StateMachine/Utilities.meta


fileFormatVersion: 2
guid: fbce2081495798d40b0abeac468abcd6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs


using System;
using UnityEngine;
namespace UOP1.StateMachine
{
[AttributeUsage(AttributeTargets.Field)]
public class InitOnlyAttribute : PropertyAttribute { }
}

11
UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs.meta


fileFormatVersion: 2
guid: c46c099afcb2c684a9253941297b4eb9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存