1
3

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.

unity > mouse wheelでscrollbarをスクロールする

Last updated at Posted at 2015-08-13
動作確認
Unity 5.1.1f-1 on Mac OS X 10.8.5

マウスのwheelを使って、Scrollbarのvalueを動かす。

準備

  1. UI > Scrollbarを追加する。名前をScrollbar1にする (スクリプトと合わせる)
  2. Scrollbar1 > Scrollbar (Script) > Directionを "Top to Bottom"にする
  3. 以下のScrollHandle.csをScrollbar1に関連付ける

code

ScrollHandle.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // for Scrollbar

public class ScrollHandle : MonoBehaviour {

	private void changeScrollBarValue(string scrollBarName, bool isUp) {
		GameObject work = GameObject.Find (scrollBarName);
		if (work) {
			float aRatio = work.GetComponent<Scrollbar>().value;
			if (isUp) {
				aRatio -= 0.05f; // for from top to bottom direction
			} else {
				aRatio += 0.05f; // for from top to bottom direction
			}
			work.GetComponent<Scrollbar>().value = aRatio;
		}
	}

	void OnGUI() {
		float val = Input.GetAxis ("Mouse ScrollWheel");
		if (val > 0.0f) {
			changeScrollBarValue("Scrollbar1", /* isUp=*/true);
			Debug.Log ("up");
		} else if (val < 0.0f) {
			changeScrollBarValue("Scrollbar1", /* isUp=*/false);
			Debug.Log ("down");
		} else {
			// do nothing
		}
	}
}

Untitled_-150814_scrollwheel-PC__Mac___Linux_Standalone__Personal.jpg

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?