HKMBFZ/Assets/Scripts/Model/Control3DModelBt.cs

214 lines
6.3 KiB
C#

using DG.Tweening;
using HighlightPlus;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
/// <summary>
/// 按钮类型
/// </summary>
public enum BtType
{
None,
,
,
,
}
public class Control3DModelBt : MonoBehaviour
{
/// <summary>
/// 模型按钮
/// </summary>
public Transform ModelBt;
/// <summary>
/// 模型灯
/// </summary>
public Renderer ModelLight;
/// <summary>
/// 是否已经改变初始状态
/// </summary>
bool isInit = false;
/// <summary>
/// 原始高度
/// </summary>
public float Originalhight;
/// <summary>
/// 移动高度
/// </summary>
public float Movehight;
private HighlightEffect highlightEffect;
public BtType btType = BtType.;
private void Awake()
{
ModelBt = transform;
transform.AddComponent<MeshCollider>();
transform.AddComponent<HighlightEffect>();
highlightEffect = transform.GetComponent<HighlightEffect>();
highlightEffect.outlineColor = Color.yellow;
if (transform.parent.childCount > 1)
{
ModelLight = transform.parent.GetChild(1).GetComponent<Renderer>();
}
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;
}
}
/// <summary>
/// 设置键盘颜色
/// </summary>
public void SetKeyBoadColor(Color color)
{
transform.GetComponent<Renderer>().material.color = color;
}
}