Created
March 5, 2023 03:56
-
-
Save kaikoga/21b2cbd4ded6985078ee1acd8a964d60 to your computer and use it in GitHub Desktop.
UdonSharpの外部アセットへの参照を突っ込むフィールドにつけるとゆるく型チェックしてくれるやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace Silksprite.Attributes | |
{ | |
public class LooselyTypedAttribute : PropertyAttribute | |
{ | |
public readonly string TypeName; | |
public LooselyTypedAttribute(string typeName) | |
{ | |
TypeName = typeName; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Silksprite.Attributes; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Silksprite.PropertyDrawers | |
{ | |
[CustomPropertyDrawer(typeof(LooselyTypedAttribute))] | |
public class LooselyTypedPropertyDrawer : PropertyDrawer | |
{ | |
string TypeName => ((LooselyTypedAttribute)attribute).TypeName; | |
object FindWrongProperty(SerializedProperty property) | |
{ | |
bool CheckWrongProperty(SerializedProperty item) | |
{ | |
var value = item.objectReferenceValue; | |
if (value == null) return false; | |
return value.GetType().Name != TypeName; | |
} | |
if (property.isArray) | |
{ | |
for (var i = 0; i < property.arraySize; i++) | |
{ | |
var elementProperty = property.GetArrayElementAtIndex(i); | |
if (CheckWrongProperty(elementProperty)) return elementProperty.objectReferenceValue; | |
} | |
} | |
else | |
{ | |
if (CheckWrongProperty(property)) return property.objectReferenceValue; | |
} | |
return null; | |
} | |
GUIContent ErrorMessage(SerializedProperty property) | |
{ | |
var wrongProperty = FindWrongProperty(property); | |
return wrongProperty == null ? null : new GUIContent($"Object is not {TypeName}, found {wrongProperty.GetType().Name}"); | |
} | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
var errorMessage = ErrorMessage(property); | |
if (errorMessage == null) | |
{ | |
EditorGUI.PropertyField(position, property, label, true); | |
return; | |
} | |
var rect = position; | |
rect.height = base.GetPropertyHeight(property, label); | |
EditorGUI.PropertyField(rect, property, label, true); | |
var messageHeight = EditorStyles.helpBox.CalcHeight(errorMessage, EditorGUIUtility.currentViewWidth); | |
position.yMin = position.yMax - messageHeight; | |
EditorGUI.HelpBox(position, errorMessage.text, MessageType.Error); | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
var errorMessage = ErrorMessage(property); | |
if (errorMessage == null) | |
{ | |
return base.GetPropertyHeight(property, label); | |
} | |
var messageHeight = EditorStyles.helpBox.CalcHeight(ErrorMessage(property), EditorGUIUtility.currentViewWidth); | |
return base.GetPropertyHeight(property, label) + EditorGUIUtility.standardVerticalSpacing + messageHeight; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Header("QvPen_PenManager Component:")] | |
[SerializeField] [LooselyTyped("QvPen_PenManager")] UdonSharpBehaviour[] penManagers; | |
[Header("QvPen_EraserManager Component:")] | |
[SerializeField] [LooselyTyped("QvPen_EraserManager")] UdonSharpBehaviour[] eraserManagers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment