Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VRM10SpringBoneColliderGroup 改善 #2485

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public class VRM10SpringBoneCollider : MonoBehaviour

public bool IsSelected => GetInstanceID() == SelectedGuid;

public void OnDrawGizmosSelected()
void OnDrawGizmosSelected()
{
DrawGizmos();
}

public void DrawGizmos()
{
Gizmos.matrix = transform.localToWorldMatrix;
switch (ColliderType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace UniVRM10
Expand All @@ -14,5 +15,80 @@ public class VRM10SpringBoneColliderGroup : MonoBehaviour

[SerializeField]
public List<VRM10SpringBoneCollider> Colliders = new List<VRM10SpringBoneCollider>();

string GetName()
{
if (!string.IsNullOrEmpty(Name))
{
return Name;
}
return Colliders[0].name;
}

#if UNITY_EDITOR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本当は ifdef やめて Editor asmdef の方に Editor コードは書きたいところだけど
Gizmo や Menu の周りは結構どうしようもないときはあるんだよなあ。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

editor 側に移動しようかと思ったのだけど、
一個の GameObject に複数の同一 component がささっているときに対象を選択するには、
ContextMenu が楽なのでやむを得ない・・・。

[ContextMenu("Create child and move to that")]
public void Separate()
{
var vrm = GetComponentInParent<Vrm10Instance>();
if (vrm == null)
{
return;
}
if (Colliders == null || Colliders.Count == 0)
{
return;
}

UnityEditor.Undo.IncrementCurrentGroup();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺ビルドするとエラーになりません?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

忘れてたー

UnityEditor.Undo.SetCurrentGroupName("VRM10SpringBoneColliderGroup.Separate");
var undo = UnityEditor.Undo.GetCurrentGroup();

var child = new GameObject($"group: {GetName()}");
UnityEditor.Undo.RegisterCreatedObjectUndo(child, "child");
UnityEditor.Undo.SetTransformParent(child.transform, transform, "setParent");

var group = UnityEditor.Undo.AddComponent<VRM10SpringBoneColliderGroup>(child);
group.Name = Name;
group.Colliders = Colliders.ToList();
for (int i = 0; i < vrm.SpringBone.ColliderGroups.Count; ++i)
{
if (vrm.SpringBone.ColliderGroups[i] == this)
{
vrm.SpringBone.ColliderGroups[i] = group;
}
}
foreach (var spring in vrm.SpringBone.Springs)
{
for (int i = 0; i < spring.ColliderGroups.Count; ++i)
{
if (spring.ColliderGroups[i] == this)
{
spring.ColliderGroups[i] = group;
}
}
}

if (Application.isPlaying)
{
Destroy(this);
}
else
{
UnityEditor.Undo.DestroyObjectImmediate(this);
}

UnityEditor.Undo.RegisterFullObjectHierarchyUndo(vrm.gameObject, "VRM10SpringBoneColliderGroup.Separate");

UnityEditor.Undo.CollapseUndoOperations(undo);
}
#endif

public void OnDrawGizmosSelected()
{
foreach (var collider in Colliders)
{
collider.DrawGizmos();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class VRM10SpringBoneJoint : MonoBehaviour
[SerializeField, Range(0, 1)]
public float m_dragForce = 0.4f;

[SerializeField]
[SerializeField]
public float m_jointRadius = 0.02f;

[SerializeField]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void DrawGizmos()
{
foreach (var collider in group.Colliders)
{
collider.OnDrawGizmosSelected();
collider.DrawGizmos();
}
}
m_drawCollider = 0;
Expand Down