2024-02-18 19:21:26 +08:00
|
|
|
|
#region
|
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
using System;
|
2024-02-18 19:21:26 +08:00
|
|
|
|
using UnityEngine;
|
2024-03-05 16:19:54 +08:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2024-02-18 19:21:26 +08:00
|
|
|
|
using UnityEngine.Serialization;
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace nadena.dev.modular_avatar.core
|
|
|
|
|
{
|
|
|
|
|
[ExecuteInEditMode]
|
|
|
|
|
[DisallowMultipleComponent]
|
|
|
|
|
[AddComponentMenu("Modular Avatar/MA Scale Adjuster")]
|
|
|
|
|
[HelpURL("https://modular-avatar.nadena.dev/docs/reference/scale-adjuster?lang=auto")]
|
|
|
|
|
public sealed class ModularAvatarScaleAdjuster : AvatarTagComponent
|
|
|
|
|
{
|
|
|
|
|
private const string ADJUSTER_OBJECT = "MA Scale Adjuster Proxy Renderer";
|
|
|
|
|
[SerializeField] private Vector3 m_Scale = Vector3.one;
|
|
|
|
|
|
|
|
|
|
public Vector3 Scale
|
|
|
|
|
{
|
|
|
|
|
get => m_Scale;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_Scale = value;
|
2024-03-05 16:19:54 +08:00
|
|
|
|
PreCull();
|
2024-02-18 19:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[SerializeField] [FormerlySerializedAs("scaleProxy")]
|
|
|
|
|
internal Transform legacyScaleProxy;
|
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
internal Transform scaleProxyParent, scaleProxyChild;
|
2024-02-18 19:21:26 +08:00
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
[NonSerialized]
|
2024-02-18 19:21:26 +08:00
|
|
|
|
private bool initialized = false;
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
2024-03-05 16:19:54 +08:00
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
ProxyManager.RegisterAdjuster(this);
|
|
|
|
|
initialized = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:21:26 +08:00
|
|
|
|
void OnValidate()
|
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
ProxyManager.RegisterAdjuster(this);
|
2024-02-18 19:21:26 +08:00
|
|
|
|
initialized = false;
|
|
|
|
|
}
|
2024-03-05 16:19:54 +08:00
|
|
|
|
|
|
|
|
|
internal void PreCull()
|
2024-02-18 19:21:26 +08:00
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
if (PrefabUtility.IsPartOfPrefabAsset(this)) return;
|
|
|
|
|
|
|
|
|
|
if (scaleProxyParent == null || initialized == false)
|
2024-02-18 19:21:26 +08:00
|
|
|
|
{
|
|
|
|
|
InitializeProxy();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
var xform = transform;
|
|
|
|
|
scaleProxyParent.position = transform.position;
|
|
|
|
|
scaleProxyParent.rotation = transform.rotation;
|
|
|
|
|
scaleProxyParent.localScale = transform.localScale;
|
|
|
|
|
scaleProxyChild.localScale = m_Scale;
|
|
|
|
|
|
|
|
|
|
ProxyManager.RegisterBone(xform, scaleProxyChild);
|
|
|
|
|
|
2024-02-18 19:21:26 +08:00
|
|
|
|
if (legacyScaleProxy != null && !PrefabUtility.IsPartOfPrefabAsset(legacyScaleProxy))
|
|
|
|
|
{
|
|
|
|
|
DestroyImmediate(legacyScaleProxy.gameObject);
|
|
|
|
|
legacyScaleProxy = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeProxy()
|
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
if (scaleProxyParent == null)
|
2024-02-18 19:21:26 +08:00
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
scaleProxyParent = new GameObject(gameObject.name + " (Scale Proxy)").transform;
|
|
|
|
|
scaleProxyChild = new GameObject("Child").transform;
|
|
|
|
|
|
|
|
|
|
scaleProxyChild.transform.SetParent(scaleProxyParent, false);
|
2024-02-18 19:21:26 +08:00
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
#if MODULAR_AVATAR_DEBUG_HIDDEN
|
|
|
|
|
scaleProxyParent.gameObject.hideFlags = HideFlags.DontSave;
|
|
|
|
|
scaleProxyChild.gameObject.hideFlags = HideFlags.DontSave;
|
|
|
|
|
#else
|
|
|
|
|
scaleProxyParent.gameObject.hideFlags = HideFlags.HideAndDontSave;
|
|
|
|
|
scaleProxyChild.gameObject.hideFlags = HideFlags.HideAndDontSave;
|
|
|
|
|
#endif
|
2024-02-18 19:21:26 +08:00
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
if (scaleProxyParent.gameObject.scene != gameObject.scene && gameObject.scene.IsValid())
|
|
|
|
|
{
|
|
|
|
|
SceneManager.MoveGameObjectToScene(scaleProxyParent.gameObject, gameObject.scene);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:21:26 +08:00
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
ProxyManager.UnregisterAdjuster(this);
|
|
|
|
|
|
|
|
|
|
if (scaleProxyParent != null)
|
2024-02-18 19:21:26 +08:00
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
DestroyImmediate(scaleProxyParent.gameObject);
|
2024-02-18 19:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
if (transform != null)
|
2024-02-18 19:21:26 +08:00
|
|
|
|
{
|
2024-03-05 16:19:54 +08:00
|
|
|
|
ProxyManager.UnregisterBone(transform);
|
2024-02-18 19:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 16:19:54 +08:00
|
|
|
|
base.OnDestroy();
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
internal void PreCull() {
|
|
|
|
|
// build time stub
|
2024-02-18 19:21:26 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|