/// <summary> /// Called when a new Model is loaded. /// </summary> /// <param name="sender">The Sender/CubismViewer.</param> /// <param name="model">The new Model.</param> private void OnNewModel(CubismViewer sender, CubismModel model) { // Check if old model is currently loaded. if (CubismPartsInfo != null) { // Destroy all old UI elements if they exist. foreach (CubismPartInfo part in CubismPartsInfo) { GameObject.Destroy(part.Slider.gameObject.transform.parent.gameObject); } } // Get template for part entries (find over parent because it's not enabled) GameObject partEntryTemplate = GameObject.Find("partScroll").transform.Find("PartEntryTemplate").gameObject; // Get scroll view content box. Part sliders are instantiated inside of this. GameObject partScrollContent = GameObject.Find("partScrollContent"); CubismPartsInfo = new List <CubismPartInfo>(); // Populate part UI scroll view. foreach (CubismPart p in model.Parts) { // Instantiate from template. GameObject newPart = (GameObject)Instantiate(partEntryTemplate); newPart.transform.SetParent(partScrollContent.transform); newPart.SetActive(true); newPart.name = p.Id; // Set slider values. Slider s = newPart.GetComponentInChildren <Slider>(); s.maxValue = 1; s.minValue = 0; s.value = p.Opacity; // Set text fields. Text t = newPart.GetComponentsInChildren <Text>()[3]; newPart.GetComponentsInChildren <Text>()[0].text = p.Id; t.text = p.Opacity.ToString(); Toggle to = newPart.GetComponentInChildren <Toggle>(); Image img = newPart.GetComponent <Image>(); // Create list of all CubismParts and their respective UI elements/override state. CubismPartInfo part = new CubismPartInfo(p, to, t, img, s, false, false, p.Opacity, p.Opacity); CubismPartsInfo.Add(part); // Listeners for slider and toggle button. to.onValueChanged.AddListener(delegate(bool newValue) { PartActiveStatusChanged(newValue, part); }); s.onValueChanged.AddListener(delegate(float newValue) { PartOpacityChanged(newValue, part); }); } // HACK Manually set scroll content height to height of children. Correct way to do this? int partEntryHeight = (int)((RectTransform)partEntryTemplate.transform).rect.height * model.Parts.Length; ((RectTransform)partScrollContent.transform).sizeDelta = new Vector2(0, partEntryHeight); }
/// <summary> /// Called when the user or the animation changes the slider value. /// </summary> /// <param name="newValue">New value of the slider.</param> /// <param name="part">The associated CubismPartInfo.</param> private void PartOpacityChanged(float newValue, CubismPartInfo part) { // Check if the call came from LateUpdate(). if (!part.ValueSetByAnimation) { // If not, the part is now considered in override mode. part.Active = true; part.OverrideVal = newValue; part.Toggle.isOn = true; part.BackgroundTint.enabled = true; } else { part.ValueSetByAnimation = false; } }
/// <summary> /// Called when override button is toggled. /// Also called when user presses the reset button or manually moves a slider. /// </summary> /// <param name="newValue">Checked or unchecked.</param> /// <param name="part">The associated CubismPartInfo.</param> private void PartActiveStatusChanged(bool newValue, CubismPartInfo part) { part.Active = newValue; part.BackgroundTint.enabled = newValue; part.OverrideVal = part.Part.Opacity; }