Unity 5游戏中的Android控件

时间:2017-06-15 08:50:49

标签: c# android unity3d touchpad

嘿伙计们提前感谢帮助我,我是Unity的新手并且正在做一个Captain Blaster教程。唯一的问题是我想使用移动平台而不是Windows。我已将构建设置设置为Android,并插入了Dual Touch控件。我删除了控制触摸板并将两个moveTouchPads并排插入。我想让你在点击屏幕右侧时向右移动,当你点击屏幕左侧时向左移动。船舶控制的代码是

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    namespace UnityStandardAssets.CrossPlatformInput
    {
    public class ShipControl : MonoBehaviour {
        public float playerSpeed = 10f;
        public GameControl gameController;
        public GameObject bulletPrefab;
        public float reloadTime = 0.5f;

        private float elapsedTime = 0;


        void Update ()
        {
            elapsedTime += Time.deltaTime;

            float xMovement = CrossPlatformInputManager.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;
            float xPosition = Mathf.Clamp (xMovement, -7f, 7f);
            transform.Translate (xPosition, 0f, 0f);

            if (Input.GetButtonDown ("Jump") && elapsedTime > reloadTime) {
                Vector3 spawnPos = transform.position;
                spawnPos += new Vector3 (0, 1.2f, 0);
                Instantiate (bulletPrefab, spawnPos, Quaternion.identity);
            }
        }
        void OnTriggerEnter2D (Collider2D other)
        {
                gameController.PlayerDied ();
        }
    }

}

移动触控板的代码是

using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace UnityStandardAssets.CrossPlatformInput
{
    public class AxisTouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        // designed to work in a pair with another axis touch button
        // (typically with one having -1 and one having 1 axisValues)
        public string axisName = "Horizontal"; // The name of the axis
        public float axisValue = 1; // The axis that the value has
        public float responseSpeed = 3; // The speed at which the axis touch button responds
        public float returnToCentreSpeed = 3; // The speed at which the button will return to its centre

        AxisTouchButton m_PairedWith; // Which button this one is paired with
        CrossPlatformInputManager.VirtualAxis m_Axis; // A reference to the virtual axis as it is in the cross platform input

        void OnEnable()
        {
            if (!CrossPlatformInputManager.AxisExists(axisName))
            {
                // if the axis doesnt exist create a new one in cross platform input
                m_Axis = new CrossPlatformInputManager.VirtualAxis(axisName);
                CrossPlatformInputManager.RegisterVirtualAxis(m_Axis);
            }
            else
            {
                m_Axis = CrossPlatformInputManager.VirtualAxisReference(axisName);
            }
            FindPairedButton();
        }

        void FindPairedButton()
        {
            // find the other button witch which this button should be paired
            // (it should have the same axisName)
            var otherAxisButtons = FindObjectsOfType(typeof(AxisTouchButton)) as AxisTouchButton[];

            if (otherAxisButtons != null)
            {
                for (int i = 0; i < otherAxisButtons.Length; i++)
                {
                    if (otherAxisButtons[i].axisName == axisName && otherAxisButtons[i] != this)
                    {
                        m_PairedWith = otherAxisButtons[i];
                    }
                }
            }
        }

        void OnDisable()
        {
            // The object is disabled so remove it from the cross platform input system
            m_Axis.Remove();
        }


        public void OnPointerDown(PointerEventData data)
        {
            if (m_PairedWith == null)
            {
                FindPairedButton();
            }
            // update the axis and record that the button has been pressed this frame
            m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, axisValue, responseSpeed * Time.deltaTime));
        }


        public void OnPointerUp(PointerEventData data)
        {
            m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, 0, responseSpeed * Time.deltaTime));
        }
    }
}

如何使用Android屏幕控制移动?谢谢你们,我感谢任何帮助!我知道触摸板代码可能没有帮助,对不起,我把它放在那里以防万一。

0 个答案:

没有答案