2022-08-28 04:38:52 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
#if UNITY_EDITOR
|
2022-08-28 04:38:52 +08:00
|
|
|
|
using UnityEditor;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
#endif
|
2022-08-28 04:38:52 +08:00
|
|
|
|
using UnityEngine;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
using Object = UnityEngine.Object;
|
2022-08-28 04:38:52 +08:00
|
|
|
|
|
|
|
|
|
namespace net.fushizen.modular_avatar.core
|
|
|
|
|
{
|
2022-08-30 06:13:26 +08:00
|
|
|
|
[ExecuteInEditMode]
|
2022-08-28 04:38:52 +08:00
|
|
|
|
public class ModularAvatarMergeArmature : AvatarTagComponent
|
|
|
|
|
{
|
|
|
|
|
public GameObject mergeTarget;
|
|
|
|
|
public string mergeTargetPath;
|
|
|
|
|
public string prefix;
|
|
|
|
|
public string suffix;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
public bool locked;
|
|
|
|
|
|
|
|
|
|
private bool wasLocked;
|
2022-08-28 07:54:59 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2022-08-28 04:38:52 +08:00
|
|
|
|
void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.delayCall += () =>
|
|
|
|
|
{
|
2022-08-30 05:00:01 +08:00
|
|
|
|
if (this == null) return;
|
2022-08-28 04:38:52 +08:00
|
|
|
|
if (mergeTarget == null && !string.IsNullOrWhiteSpace(mergeTargetPath))
|
|
|
|
|
{
|
|
|
|
|
var avatar = RuntimeUtil.FindAvatarInParents(transform);
|
|
|
|
|
if (avatar != null)
|
|
|
|
|
{
|
|
|
|
|
mergeTarget = avatar.transform.Find(mergeTargetPath)?.gameObject;
|
|
|
|
|
}
|
2022-08-30 05:00:01 +08:00
|
|
|
|
if (mergeTarget != null) {
|
|
|
|
|
RuntimeUtil.MarkDirty(this);
|
|
|
|
|
}
|
2022-08-28 04:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (mergeTarget != null && mergeTargetPath != RuntimeUtil.AvatarRootPath(mergeTarget))
|
|
|
|
|
{
|
|
|
|
|
mergeTargetPath = RuntimeUtil.AvatarRootPath(mergeTarget);
|
|
|
|
|
int insetPos = gameObject.name.IndexOf(mergeTarget.name, StringComparison.Ordinal);
|
|
|
|
|
if (insetPos != -1)
|
|
|
|
|
{
|
|
|
|
|
prefix = gameObject.name.Substring(0, insetPos);
|
|
|
|
|
suffix = gameObject.name.Substring(insetPos + mergeTarget.name.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
|
|
|
|
CheckLock();
|
2022-08-28 04:38:52 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2022-08-28 07:54:59 +08:00
|
|
|
|
#endif
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
|
|
|
|
void CheckLock()
|
|
|
|
|
{
|
|
|
|
|
if (RuntimeUtil.isPlaying) return;
|
|
|
|
|
|
|
|
|
|
if (locked != wasLocked)
|
|
|
|
|
{
|
|
|
|
|
if (!locked)
|
|
|
|
|
{
|
|
|
|
|
foreach (var comp in GetComponentsInChildren<MAInternalOffsetMarker>())
|
|
|
|
|
{
|
|
|
|
|
DestroyImmediate(comp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasLocked = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (mergeTarget == null) return;
|
|
|
|
|
foreach (var xform in GetComponentsInChildren<Transform>(true))
|
|
|
|
|
{
|
|
|
|
|
Transform baseObject = FindCorresponding(xform);
|
|
|
|
|
if (baseObject != null && xform.gameObject.GetComponent<MAInternalOffsetMarker>() == null)
|
|
|
|
|
{
|
|
|
|
|
var comp = xform.gameObject.AddComponent<MAInternalOffsetMarker>();
|
|
|
|
|
comp.correspondingObject = baseObject;
|
|
|
|
|
comp.lockBasePosition = baseObject.gameObject == mergeTarget;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasLocked = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Transform FindCorresponding(Transform xform)
|
|
|
|
|
{
|
|
|
|
|
if (xform == null) return null;
|
|
|
|
|
if (xform == transform) return mergeTarget.transform;
|
|
|
|
|
|
|
|
|
|
var correspondingParent = FindCorresponding(xform.parent);
|
|
|
|
|
if (correspondingParent == null) return null;
|
|
|
|
|
|
|
|
|
|
return correspondingParent.Find(prefix + xform.name + suffix);
|
|
|
|
|
}
|
2022-08-28 04:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|