UnityUI のマウスホバー、画像ズーム効果
方法 1: 透明な画像マスクを作成し、コードで画像のスケーリングを制御する
- pic2の下に選択したサブ画像を作成し、画像と選択したRect Transformコンポーネントのパラメータ値を統一します
サブイメージを作成する
Rect Transform パラメータの値をコピー&ペーストして統一します。
注: スケールは 1:1:1 であることが望ましいです。
- ModeHighLighting スクリプトを作成し、pic2 にハングし、選択した画像をスクリプト変数に割り当てます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ModeHighLighting : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler //IPointerEnterHandler用于检测鼠标何时开始悬停在某个游戏对象上;IPointerExitHandler用于检测鼠标何时停止悬停在游戏对象上
{
public GameObject beSelected; //遮罩
public float size=1.2f; //放大倍数
public float normalSize = 1f; //原缩放值scale(1,1,1)
public void OnPointerEnter(PointerEventData eventData) //检测鼠标悬停在物体上时运行
{
beSelected.SetActive(true); //激活遮罩
transform.localScale = new Vector3(size, size, size); //放大图像
//transform.localScale += Vector3.one * size; //Vector3.one是Vector3(1, 1, 1)的简写
}
public void OnPointerExit(PointerEventData eventData) //检测鼠标离开物体时运行
{
transform.localScale = new Vector3(normalSize,normalSize,normalSize); //还原图像大小
//transform.localScale = Vector3.one;
beSelected.SetActive(false) //禁用遮罩
}
void OnEnable() //只在程序启动时执行一次
{
transform.localScale = new Vector3(normalSize, normalSize, normalSize); //初始化图像大小
//transform.localScale = Vector3.one;
beSelected.SetActive(false) //禁用遮罩
}
}
- 選択した画像の透明度を調整し、非アクティブに設定します
画像の透明度を調整する
非アクティブに設定
方法 2: ボタン コンポーネントを使用してマスク効果を作成し、コードで画像のズームを制御します
- pic2にボタンコンポーネントを追加し、ハイライトカラーを設定します
- ModeHighLighting スクリプトを作成し、pic2 にもハングします
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ModeHighLighting : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
//public GameObject beSelected;
public float size=1.2f;
public float normalSize = 1f;
public void OnPointerEnter(PointerEventData eventData)
{
//beSelected.SetActive(true);
transform.localScale = new Vector3(size, size, size);
}
public void OnPointerExit(PointerEventData eventData)
{
transform.localScale = new Vector3(normalSize,normalSize,normalSize);
//beSelected.SetActive(false)
}
void OnEnable()
{
transform.localScale = new Vector3(normalSize, normalSize, normalSize);
//beSelected.SetActive(false)
}
}