|
| 1 | +#if UNITY_EDITOR |
| 2 | +using UnityEditor; |
| 3 | +using UnityEngine; |
| 4 | +using VRC.SDK3.Avatars.Components; |
| 5 | +using VRC.SDKBase; |
| 6 | + |
| 7 | +[ExecuteInEditMode] |
| 8 | +public class ViewPositionConstraint : MonoBehaviour, IEditorOnly |
| 9 | +{ |
| 10 | + public VRCAvatarDescriptor TargetAvatar |
| 11 | + { |
| 12 | + get; |
| 13 | + private set; |
| 14 | + } |
| 15 | + |
| 16 | + public Vector3 Offset; |
| 17 | + public bool Active; |
| 18 | + |
| 19 | + private Vector3 ViewPosition; |
| 20 | + private Vector3 PreviousViewPosition; |
| 21 | + |
| 22 | + void Update() |
| 23 | + { |
| 24 | + TargetAvatar = GetComponentInParent<VRCAvatarDescriptor>(); |
| 25 | + |
| 26 | + if (TargetAvatar != null && Active) |
| 27 | + { |
| 28 | + ViewPosition = transform.position + Vector3.Scale(Offset, transform.lossyScale); |
| 29 | + TargetAvatar.ViewPosition = ViewPosition; |
| 30 | + |
| 31 | + if (PreviousViewPosition != ViewPosition) |
| 32 | + { |
| 33 | + EditorUtility.SetDirty(TargetAvatar); |
| 34 | + PreviousViewPosition = ViewPosition; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +[CustomEditor(typeof(ViewPositionConstraint))] |
| 41 | +public class ViewPositionConstraintEditor : Editor |
| 42 | +{ |
| 43 | + ViewPositionConstraint ViewPositionConstraint; |
| 44 | + |
| 45 | + SerializedProperty active; |
| 46 | + SerializedProperty offset; |
| 47 | + |
| 48 | + private void OnEnable() |
| 49 | + { |
| 50 | + EditorApplication.update += UpdateCallback; |
| 51 | + } |
| 52 | + |
| 53 | + private void OnDisable() |
| 54 | + { |
| 55 | + EditorApplication.update -= UpdateCallback; |
| 56 | + } |
| 57 | + |
| 58 | + void UpdateCallback() |
| 59 | + { |
| 60 | + Repaint(); |
| 61 | + } |
| 62 | + |
| 63 | + public override void OnInspectorGUI() |
| 64 | + { |
| 65 | + ViewPositionConstraint = (ViewPositionConstraint)target; |
| 66 | + |
| 67 | + EditorGUI.BeginDisabledGroup(true); |
| 68 | + EditorGUILayout.ObjectField(new GUIContent("Detected Avatar"), ViewPositionConstraint.TargetAvatar, typeof(VRCAvatarDescriptor), true); |
| 69 | + EditorGUI.EndDisabledGroup(); |
| 70 | + |
| 71 | + active = serializedObject.FindProperty("Active"); |
| 72 | + EditorGUILayout.PropertyField(active, new GUIContent("Active")); |
| 73 | + |
| 74 | + offset = serializedObject.FindProperty("Offset"); |
| 75 | + EditorGUILayout.PropertyField(offset, new GUIContent("Offset")); |
| 76 | + |
| 77 | + GUILayout.BeginHorizontal(); |
| 78 | + |
| 79 | + if (GUILayout.Button("Activate")) |
| 80 | + { |
| 81 | + offset.vector3Value = ViewPositionConstraint.TargetAvatar.ViewPosition - ViewPositionConstraint.transform.position; |
| 82 | + active.boolValue = true; |
| 83 | + } |
| 84 | + |
| 85 | + if (GUILayout.Button("Zero")) |
| 86 | + { |
| 87 | + offset.vector3Value = Vector3.zero; |
| 88 | + active.boolValue = true; |
| 89 | + } |
| 90 | + |
| 91 | + GUILayout.EndHorizontal(); |
| 92 | + |
| 93 | + serializedObject.ApplyModifiedProperties(); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#endif |
0 commit comments