- Hi, so I searched how to make BlendShapes with code using the two methods GetBlendShapewieight and setBlendShapeweight. I tried to attach the slider values to the blend shape value, but somehow it’s not showing up. I made my Debug Log of the slider Value and the blendshapeweight value, and they both change to the values 0 to 100. Here is my code (it’s very similiar as the documentation): (I’m Using 5.4 beta)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BlendShapeManager : MonoBehaviour {
SkinnedMeshRenderer skinnedMeshRenderer;
Mesh skinnedMesh;
void Start () {
skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();
skinnedMesh = GetComponent<SkinnedMeshRenderer> ().sharedMesh;
}
// Update is called once per frame
void Update () {
}
public void ChangeBlendShape(){
GameObject slider1 = GameObject.Find ("HUDCanvas/RightPanel/DetailPanel/Slider_Elf");
if (slider1 != null) {
float horizontalSliderPos = skinnedMeshRenderer.GetBlendShapeWeight (0);
skinnedMeshRenderer.SetBlendShapeWeight (0, slider1.GetComponent<Slider>().value );
skinnedMeshRenderer.updateWhenOffscreen = true;
Debug.Log ("BlendShape value" + skinnedMeshRenderer.GetBlendShapeWeight (0));
Debug.Log ("Slider value" + slider1.GetComponent<Slider>().value);
}
return;
}
}
The Blend shape must show elf ears but nothing happens, any clue?
Here is with a value of 100
My BlendShape was imported correctly:
Any help? I really need this working
Out of curiosity, what version of Unity are you using? I haven’t been able to replicate the issue you were having in version 5.3.5f1. I did however, put together an example of how I have handled this type of situation. If you have any questions about the code feel free to ask, but I think I put enough comments in there:
using UnityEngine;
using UnityEngine.UI;
public class BlendShapeManager : MonoBehaviour
{
// Assign these in the inspector
// instead of using GameObject.Find()
// since it does not have good performance,
// which adds up quickly if you use it a lot.
[SerializeField] private Slider sliderElf;
[SerializeField] private Slider sliderNoseWide;
[SerializeField] private SkinnedMeshRenderer skinnedMeshRenderer;
private void Start()
{
// These methods can be assigned in Slider's
// inspector if you make them public. I would
// highly recommend using the dynamic float capablity.
// However, if you want to assign them through
// code just add the listen to the slider. This
// should be better than manually syncing values
// as we let Unity do the work.
sliderElf.onValueChanged.AddListener(SetElfEarsWeight);
sliderNoseWide.onValueChanged.AddListener(SetNoseWideWeight);
}
// These methods are used to manipulate the
// weight of the SkinnedMeshRenderer's
// BlendShapes. These are assigned to the
// a slider's OnValueChanged() UnityEvent
// during the start method. If you make
// these methods public, you can also assign
// them through the slider's inspector, but either
// way should give you identical behaviors.
private void SetElfEarsWeight(float weight)
{
skinnedMeshRenderer.SetBlendShapeWeight(0, weight);
}
private void SetNoseWideWeight(float weight)
{
skinnedMeshRenderer.SetBlendShapeWeight(1, weight);
}
// Add appropriate methods for all of the BlendShapes
// the only limitation is that they can only have the
// float parameter for the weight. This is due to how
// the OnValueChanged() UnityEvent is defined.
}
1 Like
I’m using 5.4 beta as I said in my first post. Thanks for your version of the code, and the comments you wrote on it. I should use more the [SerializeField].
EDIT: I just tried the code and I get the same results, nothing changes. I made some Debug Log and in this part of the code:
public void SetElfEarsWeight(float weight){
//Debug.Log (weight);
Debug.Log (skinnedMeshRenderer.GetBlendShapeWeight(0));
skinnedMeshRenderer.SetBlendShapeWeight(0, weight);
Debug.Log (skinnedMeshRenderer.GetBlendShapeWeight(0));
}
The first Debug always prints 0
The second Debug always prints the same value as the slider.
Doesn’t first debug should show the las value changed that was stored on the Blend shape? Does this means that is always reset to zero? Isn’t this odd?
Is there any specific reason not to use the Animation window to create your blendshape animations?
Sounds alot easier to do it that way. Good luck!
1 Like
I am sorry I overlooked the version in your original post…I guess I got a bit excited to jump into code :). I have verified that BlendShape weights can be manipulated via multiple means in the latest Beta (5.4b21) and an earlier version (5.4b19). If you can see the change when manually manipulating the BlendShape weight in the inspector (without any scripts overriding the weight…like the slider), then the issue may be in the set up of your UI.Slider object. By default the range of a Slider is between 0 and 1, but the weight range of a BlendShape is between 0 and 100. This means that the default value of the slider will result in a change that is really hard to see. Make sure your Max Value is set up properly for your slider like this:
Hope that helps out
My slider goes from 0 to 100 I think that is not the problem. (In my first post you can se the slider value and the Blendshape value go to 100 on the logs)
@Manimor The reason I dont use animation is because this is a character editor and I want to change the influenfe of the BlendShape with the slider.