173 lines
4.8 KiB
C#
173 lines
4.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Accessibility;
|
|
using DG.Tweening;
|
|
using System;
|
|
using System.Linq;
|
|
using static Kirurobo.FilePanel;
|
|
|
|
/// <summary>
|
|
/// 伺服电机
|
|
/// </summary>
|
|
[ExecuteInEditMode]
|
|
public class HVBUSSERVO : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 电机编号
|
|
/// </summary>
|
|
public int indexNo;
|
|
|
|
/// <summary>
|
|
/// 电机轴
|
|
/// </summary>
|
|
public Vector3 RotAxis;
|
|
|
|
/// <summary>
|
|
/// 电机角度最小范围
|
|
/// </summary>
|
|
public float RotMin = -360;
|
|
/// <summary>
|
|
/// 电机角度最大范围
|
|
/// </summary>
|
|
public float RotMax = 360;
|
|
|
|
public bool PhysicAngLimit;
|
|
public float MinPhysicAng, MaxPhysicAng;
|
|
|
|
/// <summary>
|
|
/// 当前电机角度
|
|
/// </summary>
|
|
[SerializeField]
|
|
public float TransformRotAngle;
|
|
float rotAngs = -360;
|
|
|
|
/// <summary>
|
|
/// 角度与千数值转换
|
|
/// </summary>
|
|
[SerializeField, Range(0, 1000)]
|
|
public float ThousandNormalizedValue;
|
|
float normalizedValues = -1;
|
|
|
|
bool ismirrored;
|
|
/// <summary>
|
|
/// 镜像电机
|
|
/// </summary>
|
|
public static List<int> mirror = new List<int>() { 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新电机值
|
|
/// </summary>
|
|
/// <param name="target_value"></param>
|
|
/// <param name="duration"></param>
|
|
/// <param name="flag">是否更新ui的值</param>
|
|
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();
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
}
|