Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

ScrollRect.onValueChanged

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public var onValueChanged: UI.ScrollRect.ScrollRectEvent;
public UI.ScrollRect.ScrollRectEvent onValueChanged;

Description

Callback executed when the position of the child changes.

onValueChanged is used to watch for changes in the ScrollRect object. The onValueChanged call will use the UnityEvent.AddListener API to watch for changes. When changes happen script code provided by the user will be called. The UnityEvent.AddListener API for ScrollRect.onValueChanged takes a Vector2.

Note: The editor allows the onValueChanged value to be set up manually. For example the value can be set to run only a runtime. The object and script function to call are also provided here.

The onValueChanged variable can be alternatively set-up at runtime. The script example below shows how this can be done. The script is attached to the ScrollRect object.

#pragma strict
public class ExampleScript extends MonoBehaviour {
	static var scrollRect: ScrollRect;
	function Start() {
		scrollRect = GetComponent.<ScrollRect>();
		scrollRect.onValueChanged.AddListener(ListenerMethod);
	}
	public function ListenerMethod(value: Vector2) {
		Debug.Log("ListenerMethod: " + value);
	}
}
using UnityEngine;
using UnityEngine.UI;

public class ExampleScript : MonoBehaviour { static ScrollRect scrollRect;

void Start() { scrollRect = GetComponent<ScrollRect>(); scrollRect.onValueChanged.AddListener(ListenerMethod); }

public void ListenerMethod(Vector2 value) { Debug.Log("ListenerMethod: " + value); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答