Wow, I think I did something right) Thanks!
Thanks for the hint with ComponentLookup.
And one more question, if I add links to Authoring body parts, will it work, or will it be perceived as separate entities rather than child parts of a common body?
public class BodyAuthoring : MonoBehaviour
{
[Tooltip("Префаб тела")]
public GameObject PrefabBodyMain;
private GameObject PrefabHead;
private GameObject PrefabBody;
private GameObject PrefabHand_L_1;
private GameObject PrefabHand_L_2;
private GameObject PrefabHand_R_1;
private GameObject PrefabHand_R_2;
private GameObject PrefabLeg_L_1;
private GameObject PrefabLeg_L_2;
private GameObject PrefabLeg_R_1;
private GameObject PrefabLeg_R_2;
public float3 GetPos(BodyTypes type)
{
if (PrefabBodyMain == null) Debug.LogError($"[{nameof(PrefabBodyMain)}] is null");
var part = FindObjectByName(PrefabBodyMain.transform, type);
if (part == null) Debug.LogError($"[{type}] not found in [{PrefabBodyMain.name}]");
return part.transform.position; // Возвращает нулевой вектор, если объект не найден
}
public GameObject FindObjectByName(Transform parent, BodyTypes types)
{
string name = types.ToString();
if (parent.name == name)
{
return parent.gameObject;
}
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
GameObject result = FindObjectByName(child, types);
if (result != null)
{
return result;
}
}
return null;
}
}
public class PrefabSimpleDetailedBaker : Baker<BodyAuthoring>
{
public override void Bake(BodyAuthoring authoring)
{
var entityBodyAuthoring = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entityBodyAuthoring, new BodyAuthoringData
{
EntityPrefab = GetEntity(authoring.PrefabBodyMain, TransformUsageFlags.Dynamic),
PosBody = authoring.GetPos(BodyTypes.Body),
PosHead = authoring.GetPos(BodyTypes.Head),
PosHand_L_1 = authoring.GetPos(BodyTypes.Hand_L_1),
PosHand_L_2 = authoring.GetPos(BodyTypes.Hand_L_2),
PosHand_R_1 = authoring.GetPos(BodyTypes.Hand_R_1),
PosHand_R_2 = authoring.GetPos(BodyTypes.Hand_R_2),
PosLeg_L_1 = authoring.GetPos(BodyTypes.Leg_L_1),
PosLeg_L_2 = authoring.GetPos(BodyTypes.Leg_L_2),
PosLeg_R_1 = authoring.GetPos(BodyTypes.Leg_R_1),
PosLeg_R_2 = authoring.GetPos(BodyTypes.Leg_R_2),
});
AddComponent(entityBodyAuthoring, new BodyPartsData
{
EntityBody = GetEntity(authoring.FindObjectByName(
authoring.PrefabBodyMain.transform, BodyTypes.Body), TransformUsageFlags.Dynamic),
EntityHand_L_1 = GetEntity(authoring.FindObjectByName(
authoring.PrefabBodyMain.transform, BodyTypes.Hand_L_1), TransformUsageFlags.Dynamic),
EntityHand_L_2 = GetEntity(authoring.FindObjectByName(
authoring.PrefabBodyMain.transform, BodyTypes.Hand_L_2), TransformUsageFlags.Dynamic),
EntityHand_R_1 = GetEntity(authoring.FindObjectByName(
authoring.PrefabBodyMain.transform, BodyTypes.Hand_R_1), TransformUsageFlags.Dynamic),
EntityHand_R_2 = GetEntity(authoring.FindObjectByName(
authoring.PrefabBodyMain.transform, BodyTypes.Hand_R_2), TransformUsageFlags.Dynamic),
});
}
}
I’m trying to specify the body components during baking, but it looks like the prefab elements are specified in the entity when baking, inside the onUpdate() system, not the body parts of the instance are updated, but the prefab itself.
Is there any way to specify these links to body parts during baking, or is searching for body parts by global position the only option after spawning an instance?