Created
March 19, 2019 10:56
-
-
Save esetomo/eb5740dbb5bc7bb9e7bd8319b2a5f26f to your computer and use it in GitHub Desktop.
SkinnedMeshの追従先ボーンを指定オブジェクト以下の同名のものに変えるスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
public class Clothing : EditorWindow { | |
[MenuItem("Window/Clothing")] | |
public static void ShowWindow() | |
{ | |
GetWindow(typeof(Clothing)); | |
} | |
private Transform avatar; | |
private SkinnedMeshRenderer equip; | |
void OnGUI() | |
{ | |
avatar = EditorGUILayout.ObjectField("Avatar", avatar, typeof(Transform), true) as Transform; | |
equip = EditorGUILayout.ObjectField("Clothing", equip, typeof(SkinnedMeshRenderer), true) as SkinnedMeshRenderer; | |
if (avatar != null && equip != null) | |
{ | |
if (GUILayout.Button("Wear")) | |
{ | |
var avatarBoneDic = avatar.GetComponentsInChildren<Transform>().ToDictionary(x => x.name, x => x); | |
var attachedBones = equip.bones.ToArray(); | |
for(int i=0; i<equip.bones.Length; i++) | |
{ | |
var equipBone = equip.bones[i]; | |
var avatarBone = avatarBoneDic[equipBone.name]; | |
attachedBones[i] = avatarBone; | |
} | |
equip.bones = attachedBones; | |
equip.rootBone = avatarBoneDic[equip.rootBone.name]; | |
equip.transform.SetParent(avatar, true); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment