279 lines
8.6 KiB
C#
279 lines
8.6 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>
|
|
public bool isInit = false;
|
|
/// <summary>
|
|
/// 原始高度
|
|
/// </summary>
|
|
public float Originalhight;
|
|
/// <summary>
|
|
/// 移动高度
|
|
/// </summary>
|
|
public float Movehight;
|
|
|
|
public float rotateTarget;
|
|
|
|
private HighlightEffect highlightEffect;
|
|
public BtType btType = BtType.上下;
|
|
|
|
public Color defaultColor = Color.black;
|
|
|
|
public Quaternion startRot;
|
|
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;
|
|
}
|
|
|
|
startRot = transform.localRotation;
|
|
|
|
}
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
public bool Get3DBtnState()
|
|
{
|
|
return isInit;
|
|
}
|
|
|
|
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)
|
|
{
|
|
transform.GetComponent<Collider>().enabled = false;
|
|
ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(-35, 0, 0), .5f);
|
|
if (ModelLight != null)
|
|
{
|
|
ModelLight.material.EnableKeyword("_EMISSION");
|
|
}
|
|
yield return new WaitForSeconds(0.7f);
|
|
transform.GetComponent<Collider>().enabled = true;
|
|
isInit = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
if (!ModelBt.transform.parent.name.Contains("181"))
|
|
{
|
|
ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(35, 0, 0), .5f);
|
|
}
|
|
else
|
|
{
|
|
ModelBt.DOLocalRotateQuaternion(Quaternion.Euler(0, 0, 0), .5f);
|
|
}
|
|
transform.GetComponent<Collider>().enabled = false;
|
|
if (ModelLight != null)
|
|
{
|
|
ModelLight.material.DisableKeyword("_EMISSION");
|
|
}
|
|
yield return new WaitForSeconds(0.7f);
|
|
transform.GetComponent<Collider>().enabled = true;
|
|
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(defaultColor);
|
|
}
|
|
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(defaultColor);
|
|
}
|
|
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;
|
|
case BtType.旋钮:
|
|
if (isInit == false)
|
|
{
|
|
Quaternion q = Quaternion.Euler(transform.localRotation.x, rotateTarget, transform.localRotation.z);
|
|
ModelBt.DOLocalRotateQuaternion(q, .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.DOLocalRotateQuaternion(startRot, .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;
|
|
}
|
|
}
|