- Home /
Can't access animation after combining meshes.
I'm using a script attached to the parent of an .fbx model to add a script to each child that tells me when they've been clicked. It also combines all the meshes of the children and makes a mesh filter and renderer at the top of the hierarchy, which lets me change the shader on the object when any part of it is clicked. I want to be able to step through the animation using a slider, and I have that working as well. The problem is that they don't work together. I'm not sure why I can't access `animation["Take 001"].time` if I combine the meshes.
Here's the code:
#pragma downcast
#pragma strict
@script RequireComponent(MeshFilter)
@script RequireComponent(MeshRenderer)
var HighlightColor : Color = Color.red;
var startSliderValue : float = 0.0;
var endSliderValue : float = 1.0;
var followStart = true;
var followEnd = false;
var selected = false;
private var shaderNormal : Shader;
private var shaderHighlighted : Shader;
function Start() {
animation["Take 001"].weight = 1;
animation["Take 001"].enabled = true;
animation["Take 001"].speed = 0;
animation["Take 001"].wrapMode = WrapMode.PingPong;
for (var child : Transform in transform) {
child.position += transform.position;
addRecursively(child, "ChildClicked");
}
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
var meshFilters = GetComponentsInChildren.<MeshFilter>();
var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1];
var index = 0;
for (var i = 0; i < meshFilters.Length; i++) {
if (meshFilters[i].sharedMesh == null) continue;
combine[index].mesh = meshFilters[i].sharedMesh;
combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].renderer.enabled = false;
}
GetComponent(MeshFilter).mesh = new Mesh();
GetComponent(MeshFilter).mesh.CombineMeshes(combine);
renderer.material = meshFilters[1].renderer.sharedMaterial;
}
function addRecursively(parent : Transform, script : String) {
for (var child : Transform in parent) {
addRecursively(child, script);
}
var scriptExists : Component = parent.GetComponent(script);
if (!scriptExists) {
parent.gameObject.AddComponent(script);
}
}
function Update() {
if (followStart == true) {
animation["Take 001"].time = startSliderValue;
followEnd = false;
}
else {
followEnd = true;
}
if (followEnd == false) {
followStart = true;
}
else {
animation["Take 001"].time = endSliderValue;
followStart = false;
}
}
function Clicked() {
selected = !selected;
if(renderer.material.shader == shaderNormal) {
renderer.material.shader = shaderHighlighted;
renderer.material.SetColor("_OutlineColor", HighlightColor);
}
else {
renderer.material.shader = shaderNormal;
}
}
$$anonymous$$y Theory: Because the script doesn't convert the animations for the newly created model. It just concatenates the models and throws away animation data.
Easiest way to test this is to check out the newly created model while the game is running and see if it has an Animation behavior in the inspector, and see if it has anything in its Animations variable. $$anonymous$$y guess is that the behavior won't be there, or that the size of Animations will be 0.
Converting the animation across an entire hierarchy, to match with meshes across an entire hierarchy sounds like a very intensive process, and I would be surprised if it did.
That makes sense that that's what might be going on, but the animation behavior is still on the object at the top of the hierarchy. It just doesn't seem to be doing anything. $$anonymous$$aybe it's something along those lines, where the animation is there, but isn't matched with any of the objects anymore?
Answer by Ben Blaut · Sep 13, 2012 at 06:12 PM
I modified the MeshMerger script to work with what I have, and it solved this problem. So WilliamLeu's advice was pretty much correct - you have to match the meshes with all the relevant information for animations across the entire hierarchy.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Update iTween Path at Runtime 1 Answer
Animation within Unity 1 Answer
Animation by Scripting point A to B 1 Answer