c# - Unity3d 如何从另一个脚本引用滑动控件?

标签 c# android unity-game-engine swipe

我已经尝试了所有可能的方法来让它工作但没有成功。我有一个滑动脚本和一个游戏/控制脚本。我试图在我的控制脚本中引用滑动控件作为控制我的角色的方法。到目前为止,我在 Unity 中只收到一个错误。

Assets/RuinRunStarterKit/Scripts/csTempleRun.cs(307,25):错误 CS1501:方法UpdatePlayer 没有重载0 个参数

我已经尝试在 UpdatePlayer 的括号内添加 TouchDetector.enTouchType,但这给了我三个错误,而不是一个。我联系了很多人并访问了很多网站寻找答案,但都没有产生有效的结果。我没有受过编程方面的教育。我只是在网上进行了研究,以找出就编程而言如何做某事。我这里的第二个脚本来 self 在 Unity Assets 商店购买的 Assets 。我已经联系他们以获得对添加滑动控件的支持,他们通过电子邮件将触摸脚本发送给我,但没有告诉我如何实现它。我只想在我的手机上玩这个游戏,滑 Action 为控件。这是供个人使用。如果有人能帮助我达到预期的结果,我将非常感激。同样,我希望能够使用滑动来控制角色。谢谢。这是我的脚本:

触摸检测器.cs;

    using UnityEngine;
    using System.Collections;

    public class TouchDetector : MonoBehaviour {
public delegate void deTouchEvent
    (enTouchType touchType);

public static event
    deTouchEvent
    evTouchEvent;

public enum enTouchType
{
    SwipeLeft,
    SwipeRight,
    SwipeDown,
    SwipeUp,
}   

void Start ()
{   
}   

void Update ()
{   
    if (evTouchEvent == null)
        return;

    if (Input.GetKeyDown(KeyCode.UpArrow   )) evTouchEvent(enTouchType.SwipeUp   );
    if (Input.GetKeyDown(KeyCode.DownArrow )) evTouchEvent(enTouchType.SwipeDown );
    if (Input.GetKeyDown(KeyCode.LeftArrow )) evTouchEvent(enTouchType.SwipeLeft );
    if (Input.GetKeyDown(KeyCode.RightArrow)) evTouchEvent(enTouchType.SwipeRight);

    if (Input.touchCount > 0)
    {
        foreach (Touch t in Input.touches)
        {
            Vector3 swipe = t.deltaPosition * t.deltaTime;

            if (swipe.y >  0.5f) evTouchEvent(enTouchType.SwipeUp   );
            if (swipe.y < -0.5f) evTouchEvent(enTouchType.SwipeDown );
            if (swipe.x >  0.5f) evTouchEvent(enTouchType.SwipeRight);
            if (swipe.x < -0.5f) evTouchEvent(enTouchType.SwipeLeft );
        }
    }
}
    }

csTempleRun.cs;

    void Update () 
    {   
        UpdatePlayer();

        if (m_player != null)
        {
            // Make the camera follow the player (if active)
            SmoothFollow sF = (SmoothFollow)Camera.mainCamera.GetComponent(typeof(SmoothFollow));
            sF.target = m_player.transform;         

            // Check for collisions with interactive objects
            UpdateCoins();
            UpdateMines();

            // Dynamically update the track
            CreateNewCellsIfNeeded(false);
        }       
    }


    private void UpdatePlayer(TouchDetector.enTouchType T)
{
    // if the player is dead (replaced with ragdoll) then exit since none of this code should fire.
    if (m_player == null) 
    {
        return;     
    }



    // Gradually increase the players' running speed, and update the animation to match.
    m_playerRunSpeed += Time.deltaTime * 0.005f;
    m_playerRunSpeed = Mathf.Clamp(m_playerRunSpeed, 0.5f, 3.0f);
    m_player.animation["run"].speed = m_playerRunSpeed * 2.0f;

    // ****************************************************************************************
    // INPUT

    // Player can only turn if they are not already sliding / jumping.  
    // Equally, sliding / jumping are mutually exclusive.

    if (Input.GetKeyDown (KeyCode.LeftArrow)&& m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West;
        if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North;
        if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East;
        if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South;
    }

    if (Input.GetKeyDown(KeyCode.RightArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.East;
        if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.South;
        if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.West;
        if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.North;
    }   

    if (T==TouchDetector.enTouchType.SwipeDown && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        m_playerSlide = 1.0f;
        m_player.animation.Play("slide_fake");
    }            

    if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        AudioSource.PlayClipAtPoint(m_jumpAudio, m_player.transform.position);
        m_playerJump = 1.0f;
        m_playerYvel = 0.0f;
        m_player.animation.Play("jump");
    }

我希望发布此脚本没有做错任何事,但我觉得我需要发布此脚本才能获得我需要的帮助。您会注意到在 csTempleRun.cs 脚本中,我用 TouchDetector.enTouchType.SwipeDown 替换了其中一个 KeyCode 调用。但是我仍然遇到错误。预先感谢您的帮助。也感谢您的宝贵时间。

最佳答案

查看您的错误 - 方法 UpdatePlayer 没有重载'takes0' arguments。这意味着您需要为您的方法提供额外的数据。您需要向智能感知提供哪些数据可以告诉您。

关于c# - Unity3d 如何从另一个脚本引用滑动控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15538433/

相关文章:

c# - 并行处理一个密集的IO函数

android - 有没有办法让 Vuforia 和 ARCore 在同一个应用程序中?

C# (Visual studio) : Correlation between database, 数据集,绑定(bind)源

c# - 在泛型方法中实例化 Wrapper 对象

android - 如何使用Android webview下载一个文本文件

android - 如何在 Android 30 中打开位置权限设置?

android - 如何在自定义listView上设置CHOICE_MODE_MULTIPLE

c# - 如何将实例化的对象传递给相机

unity-game-engine - Google Play 商店不再支持 .APK?

c# - 从数据库中检索更新的数据