如何使按钮事件触发调用我的实例化对象中的方法?

huangapple go评论99阅读模式
英文:

How to make Button event trigger call methods from my instantiated objects?

问题

在我的手机游戏(2D横向卷轴游戏)中,我的玩家角色的移动(左、右和跳跃)是通过按钮来控制的,通过利用按钮的事件触发来实现指针释放(Pointer Up)。问题在于,在实际游戏之前,我有一个场景,让玩家选择不同的角色对象,然后它将在实际游戏场景中实例化。

我知道按钮需要一个对象引用槽字段,它可以从我的角色脚本中调用移动方法,但由于可能有其他依赖该按钮进行移动的角色对象,我只能引用一个。有没有办法编写一个脚本,当我按下移动按钮时,这些按钮可以识别并调用从我的实例化角色游戏对象中调用我的移动脚本,而不需要在按钮事件触发引用槽上引用角色对象?谢谢!

以下是我迄今为止所做的:

public class TestPlayerMovement : MonoBehaviour
{
    [SerializeField] public float movespeed;
    Rigidbody2D rb;
    float xAxisMove;
    bool moveleft;
    bool moveright;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        MovePlayerXAxis();
    }

    public void PointerUpleft()
    {
        moveleft = false;
    }

    public void PointerDownleft()
    {
        moveleft = true;
    }

    public void PointerDownRight()
    {
        moveright = true;
    }

    public void PointerUpRight()
    {
        moveright = false;
    }

    //检查玩家是否会向左或向右移动
    void MovePlayerXAxis()
    {
        if (moveleft)
        {
            xAxisMove = -1;
        }
        else if (moveright)
        {
            xAxisMove = 1;
        }
        else
        {
            xAxisMove = 0;
        }

        rb.velocity = new Vector2(xAxisMove * movespeed, rb.velocity.y);
    }
}

这是我希望得到的结果:如何使按钮事件触发调用我的实例化对象中的方法?

希望这样的流程能让您理解我的帖子,感谢您花时间阅读。

英文:

In my mobile game (2d side scroller), my player character movements (left , right and jump) are manipulated through buttons with event triggers by utilizing its pointer up (). the catch is, I have a scene before the actual game that lets the player select different character objects then it will instantiate on the actual game scene.

I'm aware that a button needs an object reference slot field where it can call the method of movements from my character script but I can only reference one since there are other possible character objects that relies on that button for movements. Is there a way where I can write a script that when I press my movement buttons, those buttons can recognize and call my movement script from my instantiated character gameObjects without referencing character object on the button event trigger reference slot? Thank you

Here is What I have done so far:

 public class TestPlayerMovement : MonoBehaviour
{

[SerializeField] public float movespeed;
Rigidbody2D rb;
float xAxisMove;
bool moveleft;
bool moveright;

private void Start()
{
    rb = GetComponent&lt;Rigidbody2D&gt;();

}


private void Update()
{
 MovePlayerXAxis();
}


public void PointerUpleft() {
    moveleft = false;
}

public void PointerDownleft()
{
    moveleft = true;
}

public void PointerDownRight()
{
    moveright = true;
}

public void PointerUpRight()
{
    moveright = false;
}

//Checks if player will move left or right
void MovePlayerXAxis() {
    if (moveleft)
    {
        xAxisMove = -1;
    }
    else if (moveright)
    {
        xAxisMove = 1;
    }
    else
    {
        xAxisMove = 0;
    }

    rb.velocity = new Vector2(xAxisMove * movespeed, rb.velocity.y);
}

}

This is the outcome I would liek to have: 如何使按钮事件触发调用我的实例化对象中的方法?

I hope this flow will you understand my post, thank you for taking your time.

答案1

得分: 1

一种解决方法是使用单例对象,并让它存储一个引用列表或缓存。

您的单例类:

public class SomeSingletonClass : MonoBehaviour
{
    private static SomeSingletonClass instance;

    // 可以存储游戏对象的列表以供以后引用
    private List<GameObject> refList = new List<GameObject>();

    public static SomeSingletonClass Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<SomeSingletonClass>();

                if (instance == null)
                {
                    GameObject singletonObject = new GameObject("SomeSingletonClass");
                    instance = singletonObject.AddComponent<SomeSingletonClass>();
                }
            }

            return instance;
        }
    }

    public void AddToRefList(GameObject obj)
    {
        if (!refList.Contains(obj))
        {
            refList.Add(obj);
        }
    }

    public List<GameObject> GetRefList()
    {
        // 理想情况下,为了最佳实践,您不希望任何人直接访问此列表。
        return refList;
    }
}

创建类

private void CreateObject()
{
    GameObject yourNewObject = Instantiate(/*...*/);
    // 将新实例化的对象添加到全局列表中。
    SomeSingletonClass.Instance.AddToRefList(yourNewObject);
}

创建类

private void OnClickStuff()
{
    // 现在您有一个可以调用方法的游戏对象列表。
    List<GameObject> callMethodsOn = SomeSingletonClass.Instance.GetRefList();
}

以下是了解单例模式更多信息的链接。它应该相当直观,因为它只是一个静态对象,没有静态属性。

英文:

One way to solve this is to use a Singleton object, and have it store a reference list or cache.

Your Singleton Class:

public class SomeSingletonClass : MonoBehaviour
{
    private static SomeSingletonClass instance;

    // List of game objects you can store to reference it later
    private List&lt;GameObject&gt; refList = new List&lt;GameObject&gt;();

    public static SomeSingletonClass Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType&lt;SomeSingletonClass&gt;();

                if (instance == null)
                {
                    GameObject singletonObject = new GameObject(&quot;SomeSingletonClass&quot;);
                    instance = singletonObject.AddComponent&lt;SomeSingletonClass&gt;();
                }
            }

            return instance;
        }
    }

    public void AddToRefList(GameObject obj)
    {
        if (!refList.Contains(obj))
        {
            refList.Add(obj);
        }
    }

    public List&lt;GameObject&gt; GetRefList()
    {
        // Ideally for best practices, you wouldn&#39;t want anyone to directly access this list.
        return refList;
    }
}

Creation Class

private void CreateObject()
{
    GameObject yourNewObject = Instantiate(/*...*/);
    // Add the newly instantiated object to the global list.
    SomeSingletonClass.Instance.AddToRefList(yourNewObject);
}

Creation Class

private void OnClickStuff()
{
    // Now you have a list of gameobjects that you can call methods on.
    List&lt;GameObject&gt; callMethodsOn = SomeSingletonClass.Instance.GetRefList();
}

> Here are a few different links to learn more about singleton. It should be quite straight forward as it is just a static object, without it being static.

答案2

得分: 0

听起来你想要使用按钮事件触发来调用从你实例化的对象中的方法。根据你项目的具体需求,有几种方法可以做到这一点。

一种方法是在Unity中使用EventTrigger脚本,它允许你在触发事件时调用子组件的方法,例如按钮点击或鼠标悬停。你可以在Unity文档中找到关于EventTrigger脚本的更多信息。

另一种方法是使用按钮的onClick事件来调用引用你实例化对象的方法。然后,这个方法可以调用这些对象上的适当方法。你可以在这个Unity论坛帖子中找到有关如何使用onClick事件的更多信息。

英文:

It sounds like you want to use a button event trigger to call methods from your instantiated objects. There are a few ways you can do this, depending on the specific requirements of your project.

One approach is to use the EventTrigger script in Unity, which allows you to call a method of a child component when an event is triggered, such as a button click or mouseover. You can find more information about the EventTrigger script in the Unity documentation.

Another approach is to use the onClick event of the button to call a method that references your instantiated objects. This method could then call the appropriate methods on those objects. You can find more information about using the onClick event in this Unity forum post.

huangapple
  • 本文由 发表于 2023年8月5日 16:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定