简体   繁体   English

C#如何在不使用参数的情况下从另一个方法中使用该方法的类中获取数据

[英]C# how do I get data from the class that I used the method in another class without using a parameter

I have a static Player class that I have a Jump method in it. 我有一个静态Player类,其中有一个Jump方法。 It takes a Rigidbody2D parameter so I have to call it by typing 它需要一个Rigidbody2D参数,所以我必须通过键入来调用它

Player.Jump(GetComponent<Rigidbody2D>);

but I want to get the Rigidbody2D component in the Jump method and call it by typing 但我想在Jump方法中获取Rigidbody2D组件,并通过键入以下内容进行调用

Player.Jump();

Is there any way to do this? 有什么办法吗? Can I get the Rigidbody2D component from the Jump method? 我可以从Jump方法获取Rigidbody2D组件吗?

My Jump code: 我的跳转代码:

    /// <summary>
    /// Makes the given Rigidbody2D jump.
    /// </summary>
    public static void Jump(Rigidbody2D rb)
    {
        rb.velocity = new Vector2(rb.velocity.x, 0);
        rb.AddForce(new Vector2(0, JumpHeight));
    }

Class that I'm using the Jump method 我正在使用Jump方法的类

if (Player.CanMove)
    {
        Player.Move(rb);

        if (Input.GetKeyDown(KeyCode.Space) && canJump)
        {
            if (tempMaxJumps > 0)
            {
                Player.Jump(rb);
                tempMaxJumps--;
            }
            else
            {
                canJump = false;
            }
        }
    }

If you really need the player class to stay a static class, i suggest simply adding a 如果您真的需要播放器类来保持静态类,我建议您只需添加一个

public static RigidBody2D Rigidbody;

to which you can assign to once in the Start of the class calling Jump 您可以在调用Jump的类的Start将其分配给该对象一次

However this whole question seems like bad practice, a Player script should generally be inherit from MonoBehaviour and be attached to the player GameObject in your scene. 但是,整个问题似乎是不明智的做法, Player脚本通常应继承自MonoBehaviour ,并附加到场景中的玩家GameObject。 Then inside this Player script you can check for input and have a Jump-method 然后,在此Player脚本中,您可以检查输入并具有跳转方法

You could create a static member which would have a reference to the rigidbody and set the reference at some point prior to calling Jump, but that's a bad idea. 可以创建一个静态成员,该成员将具有对刚体的引用,并在调用Jump之前的某个时刻设置引用,但这是一个坏主意。

By using the static class to make the player object jump, you are breaking abstraction. 通过使用静态类使播放器对象跳转,您正在破坏抽象。 Now you have code outside of your player object that is influencing the player, leading to code spaghetti. 现在,您的代码超出了影响播放器的播放器对象,从而导致了意大利面条。

Like this? 像这样?

public static class Player
{
    public static Rigidbody2D Jump()
    {
        return new Rigidbody2D();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C#中的另一个类引用方法? - How do i reference a method from another class in C#? C#如何在不使方法静态的情况下从界面内的另一个类调用方法? - C# How do I call a method from another class inside of my interface without making the method static? 如何从同一 class C# 中的另一种方法访问一种方法的变量? - How do I access the variable of one method from another method within the same class C#? 如何使用 C# 中另一个类中的方法返回的变量? - How do I use the returned variable from a method in another class in c#? 如何在C#中的另一个类中找到表单 - How do I find a Form from another class in c# 如何在 C# 中替换 base class 中的方法? - How do I replace a method from base class in C#? Selenium C#如何从另一个类调用方法,两个类都从基类继承 - Selenium C# How do I call a method from another class both classes inherit from a base class C#.NET WinForms如何从一个列表复制类的实例 <class> 到另一个列表 <class> 使用剪贴板 - C# .NET WinForms How do I copy instance of class from one List<class> to another List<class> using Clipboard 如何从基类执行方法时从某个类中获取变量? - How do I get a variable from a dervied class to be used when executing a method from a base class? 如何在不创建该类的新对象的情况下调用其他类的方法? C# - How do I call a method of an other class without creating a new object of the class? c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM