avatar label helper
This commit is contained in:
parent
40fff9bba3
commit
6031db6620
7 changed files with 171 additions and 0 deletions
94
AviLabels/AviLabelParent.cs
Normal file
94
AviLabels/AviLabelParent.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VRC.SDK3.Avatars.Components;
|
||||
using VRC.SDK3.Dynamics.Constraint.Components;
|
||||
using TMPro;
|
||||
using VRC.Dynamics;
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace gay.lilyy.AviLabels
|
||||
{
|
||||
|
||||
[AddComponentMenu("LillithRosePup/Avatar Label/Driver")]
|
||||
public class AviLabelParent : MonoBehaviour, VRC.SDKBase.IEditorOnly
|
||||
{
|
||||
public Color defaultColor = Color.white;
|
||||
public TMP_FontAsset defaultFont = null;
|
||||
public void UpdateLabels()
|
||||
{
|
||||
List<Transform> childrenToDestroy = new List<Transform>();
|
||||
foreach (Transform child in transform)
|
||||
{
|
||||
childrenToDestroy.Add(child);
|
||||
}
|
||||
foreach (Transform child in childrenToDestroy)
|
||||
{
|
||||
DestroyImmediate(child.gameObject);
|
||||
}
|
||||
var avatarDescriptors = GameObject.FindObjectsOfType<VRCAvatarDescriptor>();
|
||||
foreach (var avatarDescriptor in avatarDescriptors)
|
||||
{
|
||||
LabelConfig labelConfig = LabelConfig.GetDefault();
|
||||
if (avatarDescriptor.GetComponent<LabelConfig>() != null)
|
||||
{
|
||||
labelConfig = avatarDescriptor.GetComponent<LabelConfig>();
|
||||
}
|
||||
if (labelConfig.font == null)
|
||||
{
|
||||
labelConfig.font = defaultFont;
|
||||
}
|
||||
if (labelConfig.color == Color.white)
|
||||
{
|
||||
labelConfig.color = defaultColor;
|
||||
}
|
||||
var child = new GameObject(avatarDescriptor.name);
|
||||
child.transform.SetParent(transform);
|
||||
child.transform.localPosition = Vector3.zero;
|
||||
child.transform.localRotation = Quaternion.identity;
|
||||
child.transform.localScale = Vector3.one;
|
||||
|
||||
|
||||
var parentConstraint = child.AddComponent<VRCParentConstraint>();
|
||||
parentConstraint.AffectsPositionY = false;
|
||||
parentConstraint.AffectsRotationY = false;
|
||||
parentConstraint.Sources.Add(new VRCConstraintSource {
|
||||
SourceTransform = avatarDescriptor.transform,
|
||||
Weight = 1
|
||||
});
|
||||
parentConstraint.ActivateConstraint();
|
||||
parentConstraint.ZeroConstraint();
|
||||
|
||||
|
||||
var textMeshPro = child.AddComponent<TextMeshPro>();
|
||||
if (labelConfig.font != null)
|
||||
{
|
||||
textMeshPro.font = labelConfig.font;
|
||||
}
|
||||
textMeshPro.color = labelConfig.color;
|
||||
textMeshPro.text = !string.IsNullOrEmpty(labelConfig.overrideName) ? labelConfig.overrideName : avatarDescriptor.name;
|
||||
textMeshPro.alignment = TextAlignmentOptions.Center;
|
||||
textMeshPro.fontSize = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CustomEditor(typeof(AviLabelParent))]
|
||||
public class AviLabelParentInspector : Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
if (GUILayout.Button("Update Labels"))
|
||||
{
|
||||
((AviLabelParent)target).UpdateLabels();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue