Skip to content

Commit

Permalink
Fixed the rootbone in the character creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément Couture committed Oct 21, 2021
1 parent 8a91f10 commit 375333a
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions Assets/CharacterPrefabCreator/CharacterCreationWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -62,7 +63,9 @@ void OnGUI() {

renderer.sharedMaterials = materialsClone;

renderer.receiveShadows = false;
if (renderer is SkinnedMeshRenderer skinnedMeshRenderer) {
skinnedMeshRenderer.rootBone = FindRecursive(prefab.transform, skinnedMeshRenderer.rootBone.name);
}
});

}
Expand Down Expand Up @@ -120,21 +123,42 @@ private void EditPrefabValue(GameObject prefab, Action<GameObject> valuesModifie
//DestroyImmediate(instance);
}

// source: https://forum.unity.com/threads/custom-editor-new-prefabs-how-to-setup-overrides.675310/#post-4574266
private void SetPrefabDirty(GameObject prefab) {
EditorUtility.SetDirty(prefab);
PrefabUtility.RecordPrefabInstancePropertyModifications(prefab);
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(prefab.scene);
}

private bool IsCharacterCreationPossible() =>
characterRig != null
&& characterModel != null
&& characterAvatar != null
&& characterBasePrefab != null;


private T ObjectField<T>(string fieldLabel, T obj, bool allowSceneObjects) where T : UnityEngine.Object {
/// <summary>
/// Works just like <see cref="Transform.Find">Transform.Find(string)</see>, but recursively.
/// </summary>
/// <param name="parent">The transform to start from.</param>
/// <param name="targetName">the name of the transform to find, as child of parent.</param>
/// <returns>A child transform of parent with the name targetName, or null if none can be found.</returns>
private Transform FindRecursive(Transform parent, string targetName) {
List<Transform> transformsToLookAt = GetAllDirectChildrenOf(parent);

while (transformsToLookAt.Count > 0) {
foreach (Transform child in transformsToLookAt) {
if (child.name == targetName)
return child;
}

List<Transform> nextLayer = new List<Transform>();
foreach (Transform child in transformsToLookAt) {
nextLayer.AddRange(GetAllDirectChildrenOf(child));
}

transformsToLookAt = nextLayer;
}

return null;
}

private List<Transform> GetAllDirectChildrenOf(Transform parent) =>
Enumerable.Range(0, parent.childCount).Select(parent.GetChild).ToList();

private T ObjectField<T>(string fieldLabel, T obj, bool allowSceneObjects) where T : UnityEngine.Object {
return (T) EditorGUILayout.ObjectField(fieldLabel, obj, typeof(T), allowSceneObjects);
}
}

0 comments on commit 375333a

Please sign in to comment.