using DG.Tweening; using HighlightPlus; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; /// /// 按钮类型 /// public enum BtType { None, 上下, 按钮, 普通按钮, 键盘 } public class Control3DModelBt : MonoBehaviour { /// /// 模型按钮 /// public Transform ModelBt; /// /// 模型灯 /// public Renderer ModelLight; /// /// 是否已经改变初始状态 /// bool isInit = false; /// /// 原始高度 /// public float Originalhight; /// /// 移动高度 /// public float Movehight; private HighlightEffect highlightEffect; public BtType btType = BtType.上下; private void Awake() { ModelBt = transform; transform.AddComponent(); transform.AddComponent(); highlightEffect = transform.GetComponent(); highlightEffect.outlineColor = Color.yellow; if (transform.parent.childCount > 1) { ModelLight = transform.parent.GetChild(1).GetComponent(); } if (btType == BtType.键盘) { highlightEffect.outlineWidth = .05f; } else { highlightEffect.outlineWidth = .1f; } } void Start() { } private void OnMouseEnter() { highlightEffect.highlighted = true; } private void OnMouseExit() { highlightEffect.highlighted = false; } private void OnMouseDown() { if (ModelBt != null) { StartCoroutine(WaitDoTween()); } } IEnumerator WaitDoTween() { switch (btType) { case BtType.None: break; case BtType.上下: if (isInit == false) { ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(-35, 0, 0), .5f); if (ModelLight != null) { ModelLight.material.EnableKeyword("_EMISSION"); } yield return new WaitForSeconds(0.7f); isInit = true; } else { ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(35, 0, 0), .5f); if (ModelLight != null) { ModelLight.material.DisableKeyword("_EMISSION"); } yield return new WaitForSeconds(0.7f); isInit = false; } break; case BtType.按钮: if (isInit == false) { ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(0, -90, 0), .5f); if (ModelLight != null) { ModelLight.material.EnableKeyword("_EMISSION"); } yield return new WaitForSeconds(0.7f); isInit = true; } else { ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(0, 0, 0), .5f); if (ModelLight != null) { ModelLight.material.DisableKeyword("_EMISSION"); } yield return new WaitForSeconds(0.7f); isInit = false; } break; case BtType.普通按钮: float yPos= ModelBt.localPosition.y; if (isInit == false) { ModelBt.DOLocalMoveY(yPos+0.004f, .1f); if (ModelLight != null) { ModelLight.material.EnableKeyword("_EMISSION"); } SetKeyBoadColor(new Color(0.37f, 0.02f, 0.02f)); yield return new WaitForSeconds(.2f); isInit = true; ModelBt.DOLocalMoveY(yPos, .2f); SetKeyBoadColor(Color.black); } else { ModelBt.DOLocalMoveY(yPos + 0.004f, .1f); if (ModelLight != null) { ModelLight.material.DisableKeyword("_EMISSION"); } SetKeyBoadColor(new Color(0.37f, 0.02f, 0.02f)); yield return new WaitForSeconds(.2f); isInit = false; ModelBt.DOLocalMoveY(yPos, .1f); SetKeyBoadColor(Color.black); } break; case BtType.键盘: if (isInit == false) { ModelBt.DOLocalMoveY(Movehight, .1f); SetKeyBoadColor(new Color(0.47f, 0.47f, 0.47f)); yield return new WaitForSeconds(.2f); isInit = true; ModelBt.DOLocalMoveY(Originalhight, .1f); if (transform.parent.name.Contains("181")) { SetKeyBoadColor(new Color(0, 0, 0)); } else { SetKeyBoadColor(new Color(0.09f, 0.18f, 0.09f)); } } else { ModelBt.DOLocalMoveY(Movehight, .1f); SetKeyBoadColor(new Color(0.47f, 0.47f, 0.47f)); yield return new WaitForSeconds(.2f); isInit = false; ModelBt.DOLocalMoveY(Originalhight, .1f); if (transform.parent.name.Contains("181")) { SetKeyBoadColor(new Color(0, 0, 0)); } else { SetKeyBoadColor(new Color(0.09f, 0.18f, 0.09f)); } } break; } } /// /// 设置键盘颜色 /// public void SetKeyBoadColor(Color color) { transform.GetComponent().material.color = color; } }