4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

uGUIでボタンなどを押しこんでいるとき処理を行う

Posted at

下図のImageというものを押し込むもとのします。
Hierarchy
hiercky.PNG

Game
gameimg.PNG

Long Pushというスクリプトを作成&アタッチ → Event Triggerをアッタチ → Add New Event TypeでPointer EnterとPointer Upを追加 → それぞれLong Pushで定義したメソッドを入れる
Inspector
InspectorSample.PNG

LongPush.cs
using UnityEngine;

public class LongPush : MonoBehaviour
{
	public GameObject image;
	// 押し込んでいる間はtrue.
	private bool now;

	void Start()
	{
		now = false;
	}
	
	void Update()
	{
		if (now)
		{
			Debug.Log("現在押し込んでいます");
		}
	}

	// 押したとき実行される.
	public void PointerDown()
	{
		now = true;
	}

	// 離したとき実行される.
	public void PointerUp()
	{
		now = false;
	}
}

UniRexとやらを使うともっとエレガントに実装できるらしい。しかしよくわかっていないので勉強せねば!!

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?