using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Accessibility; using DG.Tweening; using System; using System.Linq; using static Kirurobo.FilePanel; /// /// 伺服电机 /// [ExecuteInEditMode] public class HVBUSSERVO : MonoBehaviour { /// /// 电机编号 /// public int indexNo; /// /// 电机轴 /// public Vector3 RotAxis; /// /// 电机角度最小范围 /// public float RotMin = -360; /// /// 电机角度最大范围 /// public float RotMax = 360; public bool PhysicAngLimit; public float MinPhysicAng, MaxPhysicAng; /// /// 当前电机角度 /// [SerializeField] public float TransformRotAngle; float rotAngs = -360; /// /// 角度与千数值转换 /// [SerializeField, Range(0, 1000)] public float ThousandNormalizedValue; float normalizedValues = -1; bool ismirrored; /// /// 镜像电机 /// public static List mirror = new List() { 4, 5, 10, 11, 16 }; private void OnEnable() { var no = int.Parse(name.Split('_')[name.Split('_').Length - 1]); if (no != indexNo) { indexNo = no; } ismirrored = mirror.Contains(indexNo); } // Start is called before the first frame update void Start() { if (Test()) { Debug.Log("startssss"); normalizedValues = ThousandNormalizedValue; rotAngs = TransformRotAngle; } } public bool Test() { return true; return indexNo == 5; } // Update is called once per frame void Update() { //if (TransformRotAngle == TransformRotAngle) //{ // transform.localEulerAngles = RotAxis * TransformRotAngle; //} if (Test()) { //实时更新角度 if (TransformRotAngle == TransformRotAngle) { transform.localEulerAngles = RotAxis * (PhysicAngLimit ? Mathf.Clamp(TransformRotAngle, MinPhysicAng, MaxPhysicAng) : TransformRotAngle); } if (!ismirrored) { //角度转换为归一化数值 if (rotAngs != TransformRotAngle) { normalizedValues = ThousandNormalizedValue = (TransformRotAngle - RotMin) / (RotMax - RotMin) * 1000; rotAngs = TransformRotAngle; } //归一化数值转换为角度 if (normalizedValues != ThousandNormalizedValue) { rotAngs = TransformRotAngle = ThousandNormalizedValue / 1000 * (RotMax - RotMin) + RotMin; normalizedValues = ThousandNormalizedValue; } } else //镜像操作 { //归一化数值转换为角度 if (normalizedValues != ThousandNormalizedValue) { rotAngs = TransformRotAngle = (1000 - ThousandNormalizedValue) / 1000 * (RotMax - RotMin) + RotMin; normalizedValues = ThousandNormalizedValue; } //角度转换为归一化数值 if (rotAngs != TransformRotAngle) { normalizedValues = ThousandNormalizedValue = 1000 - ((TransformRotAngle - RotMin) / (RotMax - RotMin) * 1000); rotAngs = TransformRotAngle; } } } } /// /// 更新电机值 /// /// /// /// 是否更新ui的值 public void UpdateValue(int target_value, float duration, bool flag = false,Action callback=null) { if (flag) { DOTween.To(() => ThousandNormalizedValue, x => ThousandNormalizedValue = x, target_value, duration).OnUpdate(() => { for (int i = 0; i < CustomPanel.instance.items.Count; i++) { if (CustomPanel.instance.items[i].id == indexNo && CustomPanel.instance.items[i].sliderOne.value != target_value) { CustomPanel.instance.items[i].ValueChange(ThousandNormalizedValue, target_value); } } }); } else { DOTween.To(() => ThousandNormalizedValue, x => ThousandNormalizedValue = x, target_value, duration).OnComplete(() => { if(callback!=null) { callback.Invoke(); } }); } } }