Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit d19f46f

Browse files
Added quaternion drawer
1 parent ddb0b87 commit d19f46f

5 files changed

Lines changed: 179 additions & 0 deletions

File tree

Assets/NewBehaviourScript.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using ScriptableObjectArchitecture;
5+
6+
public class NewBehaviourScript : MonoBehaviour
7+
{
8+
//[SerializeField]
9+
//private AnimationCurveReference animationCurveReference;
10+
//[SerializeField]
11+
//private AudioClipReference audioClipReference;
12+
//[SerializeField]
13+
//private BoolReference boolReference;
14+
//[SerializeField]
15+
//private ByteReference byteReference;
16+
//[SerializeField]
17+
//private CharReference charReference;
18+
//[SerializeField]
19+
//private Color32Reference color32Reference;
20+
//[SerializeField]
21+
//private ColorReference colorReference;
22+
//[SerializeField]
23+
//private DoubleReference doubleReference;
24+
//[SerializeField]
25+
//private FloatReference floatReference;
26+
//[SerializeField]
27+
//private GameObjectReference gameObjectReference;
28+
//[SerializeField]
29+
//private IntReference intReference;
30+
//[SerializeField]
31+
//private LayerMaskReference layerMaskReference;
32+
//[SerializeField]
33+
//private LongReference longReference;
34+
//[SerializeField]
35+
//private ObjectReference objectReference;
36+
[SerializeField]
37+
private QuaternionReference quaternionReference;
38+
//[SerializeField]
39+
//private SByteReference sByteReference;
40+
41+
//[SerializeField]
42+
//private SceneReference sceneReference;
43+
44+
//[SerializeField]
45+
//private ShortReference shortReference;
46+
//[SerializeField]
47+
//private StringReference stringReference;
48+
//[SerializeField]
49+
//private UIntReference uIntReference;
50+
//[SerializeField]
51+
//private ULongReference uLongReference;
52+
//[SerializeField]
53+
//private UShortReference uShortReference;
54+
55+
//[SerializeField]
56+
//private Vector2Reference vector2Reference;
57+
[SerializeField]
58+
private Vector3Reference vector3Reference;
59+
//[SerializeField]
60+
//private Vector4Reference vector4Reference;
61+
62+
//[SerializeField]
63+
//private Quaternion quaternion;
64+
//[SerializeField]
65+
//private Vector3 vector;
66+
}

Assets/NewBehaviourScript.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace ScriptableObjectArchitecture.Editor
7+
{
8+
[CustomPropertyDrawer(typeof(Quaternion))]
9+
public class QuaternionDrawer : PropertyDrawer
10+
{
11+
private const int ElementsInQuaternion = 4;
12+
private const float ElementLabelWidth = 13;
13+
private const float ElementSpacing = 2;
14+
15+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
16+
{
17+
if (HasLabel(label))
18+
{
19+
Rect labelRect = GetLabelRect(position);
20+
Rect valueRect = GetValueRect(position);
21+
22+
EditorGUI.LabelField(labelRect, label);
23+
DrawValue(valueRect, property);
24+
}
25+
else
26+
{
27+
DrawValue(position, property);
28+
}
29+
}
30+
private void DrawValue(Rect rect, SerializedProperty property)
31+
{
32+
float oldLabelWidth = EditorGUIUtility.labelWidth;
33+
EditorGUIUtility.labelWidth = ElementLabelWidth;
34+
35+
Quaternion quaternion = property.quaternionValue;
36+
37+
quaternion.x = DrawElement(GetElementRect(rect, 0), quaternion.x, new GUIContent("X"));
38+
quaternion.y = DrawElement(GetElementRect(rect, 1), quaternion.y, new GUIContent("Y"));
39+
quaternion.z = DrawElement(GetElementRect(rect, 2), quaternion.z, new GUIContent("Z"));
40+
quaternion.w = DrawElement(GetElementRect(rect, 3), quaternion.w, new GUIContent("W"));
41+
42+
property.quaternionValue = quaternion;
43+
EditorGUIUtility.labelWidth = oldLabelWidth;
44+
}
45+
private float DrawElement(Rect rect, float value, GUIContent label)
46+
{
47+
return EditorGUI.FloatField(rect, label, value);
48+
}
49+
private Rect GetElementRect(Rect parentRect, int index)
50+
{
51+
float widthPerElement = parentRect.width / ElementsInQuaternion;
52+
53+
return new Rect()
54+
{
55+
x = parentRect.x + widthPerElement * index,
56+
y = parentRect.y,
57+
width = widthPerElement - ElementSpacing,
58+
height = parentRect.height,
59+
};
60+
}
61+
private Rect GetLabelRect(Rect parentRect)
62+
{
63+
return new Rect()
64+
{
65+
x = parentRect.x,
66+
y = parentRect.y,
67+
width = EditorGUIUtility.labelWidth,
68+
height = parentRect.height,
69+
};
70+
}
71+
private Rect GetValueRect(Rect parentRect)
72+
{
73+
return new Rect()
74+
{
75+
x = parentRect.x + EditorGUIUtility.labelWidth + ElementSpacing,
76+
y = parentRect.y,
77+
width = parentRect.width - EditorGUIUtility.labelWidth,
78+
height = parentRect.height,
79+
};
80+
}
81+
private bool HasLabel(GUIContent label)
82+
{
83+
return label != GUIContent.none;
84+
}
85+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
86+
{
87+
return EditorGUIUtility.singleLineHeight;
88+
}
89+
}
90+
}

Assets/SO Architecture/Editor/Drawers/QuaternionDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/SO Architecture/Editor/Inspectors/BaseVariableEditor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public override void OnInspectorGUI()
4747
}
4848
protected virtual void DrawValue()
4949
{
50+
//EditorGUILayout.PropertyField(_valueProperty);
5051
GenericPropertyDrawer.DrawPropertyDrawerLayoutNew(_valueProperty, Target.Type);
5152
//GenericPropertyDrawer.DrawPropertyDrawerLayoutNew(serializedObject.GetIterator(), Target.Type);
5253

0 commit comments

Comments
 (0)