public static Bone Create() { GameObject b = new GameObject("Bone"); Undo.RegisterCreatedObjectUndo(b, "Add child bone"); b.AddComponent <Bone>(); if (Selection.activeGameObject != null) { GameObject sel = Selection.activeGameObject; b.transform.parent = sel.transform; if (sel.GetComponent <Bone>() != null) { Bone p = sel.GetComponent <Bone>(); b.transform.position = p.Head; b.transform.localRotation = Quaternion.Euler(0, 0, 0); } } Skeleton skel = b.transform.root.GetComponentInChildren <Skeleton>(); if (skel != null) { Bone[] bones = skel.GetComponentsInChildren <Bone>(); int index = bones.Max(bn => bn.index) + 1; b.GetComponent <Bone>().index = index; skel.CalculateWeights(); } Selection.activeGameObject = b; return(b.GetComponent <Bone>()); }
public void RecalculateBoneWeights() { Skeleton skeleton = gameObject.transform.root.GetComponent <Skeleton>(); if (skeleton != null) { skeleton.CalculateWeights(true); // Debug.Log("Calculated weights for " + gameObject.name); } }
public override void OnInspectorGUI() { DrawDefaultInspector(); EditorGUILayout.Separator(); if (GUILayout.Button("Flip")) { skeleton.flip = !skeleton.flip; } if (GUILayout.Button("Use Shadows")) { skeleton.useShadows = !skeleton.useShadows; } EditorGUILayout.LabelField("Poses", EditorStyles.boldLabel); poseFileName = EditorGUILayout.TextField("Pose Filename", poseFileName); GUILayout.BeginHorizontal(); if (GUILayout.Button("Save pose")) { skeleton.SavePose(poseFileName); } if (GUILayout.Button("Reset pose")) { if (skeleton.basePose != null) { skeleton.RestorePose(skeleton.basePose); } } GUILayout.EndHorizontal(); if (GUILayout.Button("Calculate weights")) { skeleton.CalculateWeights(); } if (skeleton.basePose == null) { EditorGUILayout.HelpBox("You have not selected a base pose.", MessageType.Error); } }
public void RecalculateBoneWeights() { Skeleton[] skeletons = transform.root.gameObject.GetComponentsInChildren <Skeleton>(true); Skeleton skeleton = null; foreach (Skeleton s in skeletons) { if (transform.IsChildOf(s.transform)) { skeleton = s; } } if (skeleton != null) { skeleton.CalculateWeights(true); // Debug.Log("Calculated weights for " + gameObject.name); } }
public override void OnInspectorGUI() { DrawDefaultInspector(); EditorGUILayout.Separator(); if (GUILayout.Button("FlipY")) { skeleton.flipY = !skeleton.flipY; } if (GUILayout.Button("FlipX")) { skeleton.flipX = !skeleton.flipX; } EditorGUILayout.Separator(); if (GUILayout.Button("Use Shadows")) { skeleton.useShadows = !skeleton.useShadows; } if (skeleton.useShadows) { shadows = "On"; } else { shadows = "Off"; } EditorGUILayout.LabelField("Shadows:", shadows, EditorStyles.whiteLabel); EditorGUILayout.Separator(); if (GUILayout.Button("Use Z Sorting")) { skeleton.useZSorting = !skeleton.useZSorting; } if (skeleton.useZSorting) { zsorting = "On"; } else { zsorting = "Off"; } EditorGUILayout.LabelField("Z Sorting:", zsorting, EditorStyles.whiteLabel); EditorGUILayout.Separator(); EditorGUILayout.LabelField("Poses", EditorStyles.boldLabel); poseFileName = EditorGUILayout.TextField("Pose Filename", poseFileName); GUILayout.BeginHorizontal(); if (GUILayout.Button("Save pose")) { skeleton.SavePose(poseFileName); } if (GUILayout.Button("Reset pose")) { if (skeleton.basePose != null) { skeleton.RestorePose(skeleton.basePose); } } GUILayout.EndHorizontal(); if (GUILayout.Button("Calculate weights")) { skeleton.CalculateWeights(); } if (skeleton.basePose == null) { EditorGUILayout.HelpBox("You have not selected a base pose.", MessageType.Error); } EditorGUILayout.Separator(); EditorGUILayout.LabelField("Save/Load Children Positions", EditorStyles.boldLabel); if (!skeleton.editMode) { EditorGUILayout.HelpBox("Skeleton Needs to be in Edit Mode.", MessageType.Error); } if (GUILayout.Button("Save Children Positions") && skeleton.editMode) { Bone[] bones = skeleton.gameObject.GetComponentsInChildren <Bone>(true); foreach (Bone bone in bones) { bone.SaveChildPosRot(); if (bone.HasChildPositionsSaved()) { skeleton.hasChildPositionsSaved = true; } } Debug.Log("Saved Children Positions and Rotations in Skeleton."); } if (!skeleton.hasChildPositionsSaved) { EditorGUILayout.HelpBox("You have not saved children positions.", MessageType.Error); } if (GUILayout.Button("Load Children Positions") && skeleton.hasChildPositionsSaved && skeleton.editMode) { Bone[] bones = skeleton.gameObject.GetComponentsInChildren <Bone>(true); foreach (Bone bone in bones) { bone.LoadChildPosRot(); } Debug.Log("Loaded Children Positions and Rotations in Skeleton."); } EditorGUILayout.Separator(); EditorGUILayout.LabelField("Create Skin2D Prefabs from Children", EditorStyles.boldLabel); if (GUILayout.Button("Create Skin2D Prefabs")) { Skin2D[] skins = skeleton.gameObject.GetComponentsInChildren <Skin2D>(true); foreach (Skin2D skin in skins) { bool skinActive = skin.gameObject.activeSelf; skin.gameObject.SetActive(true); skin.SaveAsPrefab(); skin.gameObject.SetActive(skinActive); } Debug.Log("Saved all Skins as Prefabs."); } EditorGUILayout.Separator(); EditorGUILayout.LabelField("Disconnect All Skin2D Prefabs", EditorStyles.boldLabel); if (GUILayout.Button("Disconnect Skin2D Prefabs")) { Skin2D[] skins = skeleton.gameObject.GetComponentsInChildren <Skin2D>(true); foreach (Skin2D skin in skins) { bool skinActive = skin.gameObject.activeSelf; skin.gameObject.SetActive(true); PrefabUtility.DisconnectPrefabInstance(skin.gameObject); skin.gameObject.SetActive(skinActive); } Debug.Log("Disconnected Skin2D Prefabs."); } EditorGUILayout.Separator(); EditorGUILayout.LabelField("Reset Skins' Control Point Names", EditorStyles.boldLabel); if (GUILayout.Button("Reset Control Point Names")) { ControlPoint[] cps = skeleton.gameObject.GetComponentsInChildren <ControlPoint>(true); foreach (ControlPoint cp in cps) { cp.Rename(); } Debug.Log("Reset all control point names."); } }
public static void Create() { if (Selection.activeGameObject != null) { GameObject o = Selection.activeGameObject; SkinnedMeshRenderer skin = o.GetComponent <SkinnedMeshRenderer>(); SpriteRenderer spriteRenderer = o.GetComponent <SpriteRenderer>(); if (skin == null && spriteRenderer != null) { Sprite thisSprite = spriteRenderer.sprite; SpriteMesh.CreateSpriteMeshAsset(spriteRenderer.transform, thisSprite); Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(spriteRenderer.sprite, false); Material spriteMaterial = new Material(spriteRenderer.sharedMaterial); spriteMaterial.CopyPropertiesFromMaterial(spriteRenderer.sharedMaterial); spriteMaterial.mainTexture = spriteTexture; DestroyImmediate(spriteRenderer); Skin2D skin2D = o.AddComponent <Skin2D>(); skin2D.sprite = thisSprite; skin = o.GetComponent <SkinnedMeshRenderer>(); MeshFilter filter = o.GetComponent <MeshFilter>(); skin.material = spriteMaterial; filter.mesh = (Mesh)Selection.activeObject; if (filter.sharedMesh != null && skin.sharedMesh == null) { skin.sharedMesh = filter.sharedMesh; } Skeleton skeleton = o.transform.root.GetComponent <Skeleton>(); if (skeleton != null) { // Generate the mesh and calculate the weights if the root transform has a skeleton skeleton.CalculateWeights(true); // Debug.Log("Calculated weights for " + o.name); // Try to initialize the parent bone to this skin Bone bone = o.transform.parent.GetComponent <Bone>(); if (bone != null) { Mesh m = skin.sharedMesh.Clone(); List <BoneWeight> weights = m.boneWeights.ToList(); for (int i = 0; i < m.vertices.Length; i++) { BoneWeight bw = weights[i]; bw = bw.SetWeight(bone.index, 1); weights[i] = bw.Clone(); } skin.sharedMesh.boneWeights = weights.ToArray(); EditorUtility.SetDirty(skin.gameObject); if (PrefabUtility.GetPrefabType(skin.gameObject) != PrefabType.None) { AssetDatabase.SaveAssets(); } } } } else { o = new GameObject("Skin2D"); Undo.RegisterCreatedObjectUndo(o, "Create Skin2D"); o.AddComponent <Skin2D> (); } } else { GameObject o = new GameObject("Skin2D"); Undo.RegisterCreatedObjectUndo(o, "Create Skin2D"); o.AddComponent <Skin2D> (); } }