Unity:由于其保护级别,无法访问“ Jostick.inputVector”

时间:2019-01-10 05:29:13

标签: c# unity3d

我是使用C#并尝试创建触摸控制器的初学者,但是在编译时会出现以下错误:

  

Jostick.inputVector由于其保护级别而无法访问。

以下是脚本:

using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class CalleAll : MonoBehaviour 
{
    public FixedJoystick MoveJoystick;
    public FixedTouchField TouchField;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
        var fps = GetComponent<RigidbodyFirstPersonController>();

        fps.RunAxis = MoveJoystick.inputVector;
        fps.mouseLook.LookAxis=TouchField.TouchDist;
    }
}

这是脚本文件。

using UnityEngine;
using UnityEngine.EventSystems;

public class FixedJoystick : Joystick
{
    public Vector2 joystickPosition = Vector2.zero;
    private Camera cam = new Camera();

    void Start()
    {
        joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
    }

    public override void OnDrag(PointerEventData eventData)
    {
        Vector2 direction = eventData.position - joystickPosition;
        ///here it is
        inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
        ClampJoystick();
        handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        OnDrag(eventData);
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        inputVector = Vector2.zero;
        handle.anchoredPosition = Vector2.zero;
    }
}

1 个答案:

答案 0 :(得分:2)

错误是因为类std::ostringstream ss; for (int i = 0; i < 10; ++i) ss << "\x1b[38;2;" << 5 * i << ";" << 255 - 10 * i << ";220m" << "ANSI Escape Sequence " << i << std::endl; std::cout << ss.str(); 有一个已经命名为Joystick的{​​{1}}成员-您不能在其子类之外的其他类型中使用它(这就是protected的原因可以使用它,但不能使用inputVector,因为FixedJoystick并非源自CalleAll)。

您可以通过创建一个公开数据的只读属性来使数据可供消费者使用:

CalleAll