using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Trolleydrawing : MonoBehaviour { public Transform oneself; public SkinnedMeshRenderer trolley; public Button Hoibutton; public Button Geidbutton; public float rotationSpeed = 10f; // 旋转速度 private Vector3 lastMousePosition; // 上一次的鼠标位置 /// /// 动画移动时间 /// public float duration = 1.5f; /// /// 管理协程 /// private Coroutine blendShapeCoroutine; private Coroutine blendShapeCoroutine2; /// /// 放大缩小的速度 /// public float zoomSpeed = 1f; public float minScale = 0.8f; // 最小缩放比例 public float maxScale = 4f; // 最大缩放比例 void Start() { oneself = GetComponent(); //Hoibutton.onClick.AddListener(Unfold); //Geidbutton.onClick.AddListener(Restore); } void Update() { //if (Input.GetMouseButtonDown(0)) //{ // // 记录当前鼠标位置 // lastMousePosition = Input.mousePosition; //} //// 检测鼠标左键是否按住 //if (Input.GetMouseButton(0)) //{ // // 计算鼠标移动的距离 // Vector3 mouseDelta = Input.mousePosition - lastMousePosition; // lastMousePosition = Input.mousePosition; // // 计算旋转的角度 // //float rotationX = -mouseDelta.y * rotationSpeed * Time.deltaTime; // float rotationY = mouseDelta.x * rotationSpeed * Time.deltaTime; // // 应用旋转到模型 // oneself.Rotate(Vector3.up, -rotationY, Space.World); // 绕世界坐标系的 Y 轴旋转 // // oneself.Rotate(Vector3.right, -rotationX, Space.World); // 绕世界坐标系的 X 轴旋转 //} // 获取鼠标滚轮的输入值 float scrollInput = Input.GetAxis("Mouse ScrollWheel"); // 计算缩放增量 Vector3 scaleChange = Vector3.one * scrollInput * zoomSpeed; // 应用缩放增量 transform.localScale += scaleChange; // 确保缩放值在规定范围内 transform.localScale = new Vector3( Mathf.Clamp(transform.localScale.x, minScale, maxScale), Mathf.Clamp(transform.localScale.y, minScale, maxScale), Mathf.Clamp(transform.localScale.z, minScale, maxScale) ); } /// /// 模型展开 /// public void Unfold() { if (blendShapeCoroutine2 != null) { StopCoroutine(BlendShapeTransition(100)); } blendShapeCoroutine2 = StartCoroutine(BlendShapeTransition(100)); } /// /// 模型还原 /// public void Restore() { //如果已有协程开启就先关闭 if (blendShapeCoroutine != null) { StopCoroutine(blendShapeCoroutine); } blendShapeCoroutine = StartCoroutine(BlendShapeToZeroTransition()); } // 协程实现 BlendShape 权重的平滑过渡 private IEnumerator BlendShapeTransition(float targetWeight) { float startWeight = trolley.GetBlendShapeWeight(0); float elapsedTime = 0f; while (elapsedTime < duration) { elapsedTime += Time.deltaTime; float t = Mathf.Clamp01(elapsedTime / duration); float currentWeight = Mathf.Lerp(startWeight, targetWeight, t); trolley.SetBlendShapeWeight(0, currentWeight); yield return null; } // 确保最终值被设置 trolley.SetBlendShapeWeight(0, targetWeight); } private IEnumerator BlendShapeToZeroTransition() { float startWeight = trolley.GetBlendShapeWeight(0); float elapsedTime = 0f; while (elapsedTime < duration) { elapsedTime += Time.deltaTime; float t = Mathf.Clamp01(elapsedTime / duration); float currentWeight = Mathf.Lerp(startWeight, 0f, t); trolley.SetBlendShapeWeight(0, currentWeight); yield return null; } // 确保最终值被设置为零 trolley.SetBlendShapeWeight(0, 0f); } }