在Unity C中的2D UI元素上获取PointerDown#

时间:2017-02-18 23:55:25

标签: c# unity3d click raycasting

我已经就这个主题得到了很多答案,但似乎没有什么能适用于我的案例。我正在尝试使用画布渲染器检测UI元素上的mouseDown,该渲染器位于具有画布的对象层次结构内。我是新手,因此我不确定画布是否需要链接到此画布渲染器,或者它们是否必须位于同一对象上,但以下代码不会导致OnPointerDown方法被激活。 / p>

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class Knob : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

    public Slider slider;
    public GameObject pivot;

    public bool selected;
    public float z;
    public Image image;
    public UtilityClass the;
    public float min;
    public float max;

    void Start () {
        z = 90;

        Quaternion rotation = Quaternion.Euler (0, 0, z);
        pivot.transform.rotation = rotation;
    }


    void Update() {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

        if (selected) {

            float pivotAngle = the.AngleFrom (the.Heading (pivot.transform.position, mousePosition));
            if (pivotAngle >= min && pivotAngle <= max)
                z = pivotAngle;

            Quaternion rotation = Quaternion.Euler (0, 0, z);
            pivot.transform.rotation = rotation;
        }
    }

    public void OnPointerDown(PointerEventData eventData) {
        selected = true;
    }

    public void OnPointerUp(PointerEventData eventData) {
        selected = false;
    }
}

目前我没有对象上的Collider 2D,但我在Image脚本中选择了“Raycast Target”。任何帮助将不胜感激。

0 个答案:

没有答案