一、创建一个代码叫做ButtonBase的脚本,继承Button
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.EventSystems;
public class ButtonBase : Button, IPointerDownHandler, IPointerUpHandler
{
public bool canPlayClickSound;
public override void OnPointerDown(PointerEventData data)
{
transform.transform.DOKill();
transform.transform.DOScale(0.95f, 0.1f).SetEase(Ease.OutCubic).SetUpdate(true);
}
public override void OnPointerUp(PointerEventData data)
{
transform.transform.DOKill();
transform.transform.DOScale(1f, 0.4f).SetEase(Ease.OutElastic).SetUpdate(true);
}
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
if (canPlayClickSound)
SoundManager.Instance.PlayEffect(SoundPath.CLICK);
}
}
二、发现自己添加的canPlayClickSound字段在Inspector面板上不显示 三、自定义面板显示字段
using UnityEditor;
[CustomEditor(typeof(ButtonBase),false)]
public class ButtonBaseInspector : Editor
{
private SerializedProperty canPlaySound;
private void OnEnable()
{
canPlaySound = serializedObject.FindProperty("canPlayClickSound");
}
}
四、现在是Inspector面板
|