【问题标题】:C# Unity - identify which method is affecting a GameObjectC# Unity - 识别影响游戏对象的方法
【发布时间】:2017-02-04 17:30:56
【问题描述】:

我这里有这段代码:

public void Move(Transform from, Transform to, float overTime)
{
    StartCoroutine(_Move(from, to, overTime));
}
IEnumerator _Move(Transform from, Transform to, float overTime)
{
    Vector2 original = from.position;
    float timer = 0.0f;
    while (timer < overTime)
    {
        float step = Vector2.Distance(original, to.position) * (Time.deltaTime / overTime);
        from.position = Vector2.MoveTowards(from.position, to.position, step);
        timer += Time.deltaTime;
        yield return null;
    }
}

这很简单。它所做的只是随着时间的推移将GameObject 移动到目的地。我打算将它用于 UI 动画目的。 但是,当我想改变它的用途时,事情变得有趣了。 例如,我将鼠标悬停在一个按钮上,该按钮开始移动。虽然它仍在移动,但我可能会再次将鼠标悬停在它上面,在这种情况下,我希望它重新启动,因此它会停止影响完全相同按钮的功能并重新启动另一个。我只是不知道如何确定同一函数的哪个调用会影响我给定的组件。还是应该将此脚本添加到每个对象中,以便它只影响那个对象?

【问题讨论】:

  • 你可能想看看gamedev.stackexchange.com
  • 可能不是一个坏主意。不过现在,我的问题清楚了吗?还是我应该编辑?
  • 您的问题在本网站上是明确且恰当的。

标签: c# function unity3d function-call


【解决方案1】:

在这里我会做很多不同的事情。一方面,如果您的 Ui 对象已经受到协程的影响,您不希望另一个协程也开始作用于它,而只是新的协程。为了避免这种情况,请声明一个协程变量:

IEnumerator myCoRoutine

当你第一次鼠标悬停时,像这样启动协程:

myCoRoutine = _Move(from, to, overTime);
Start Coroutine(_Move());

但是,你想检查一个协程是否已经在运行,如果是就停止它,然后启动一个新的,对吧?所以,这样做:

if(myCoRoutine !=null) StopCoroutine(mtCoRoutine);
myCoRoutine = _Move(from, to, overTime);
Start Coroutine(_Move());

您可能希望使用 OnPointerEnter 启动您的协程

【讨论】:

  • 感谢您的帮助,确实有用。现在,我可能会说一个包含其中 2 个的图像,因为我已经写了缩放、旋转、lerpAlpha 等,例如一个按钮略微增长并淡入。但总的来说,是的,我只想要 一个 比例,只需要一个动作等来影响它。我在谷歌上搜索了OnPointerEnter。
  • 注意:在底部,我们应该 StartCoroutine(myCoRoutine) 而不是 Start Coroutine(_Move()) ?还是我理解有误?
  • 如果你想让一个对象发生多件事情,最好将它们组合在一个协程中,所以你的 _Move 函数可能会做缩放、旋转和定位,或者你会有几种方法来做.无论哪种方式,都可以在一个协程中完成。关于 StartCouroutine 是的,您启动了声明为变量的协程,请参见此处docs.unity3d.com/ScriptReference/…
  • 另一件事,我可能想调用此Move() 来一次移动 2 个按钮。我可以用你的解决方案做到这一点吗?或者我应该只将一个脚本放在一个 UI 元素上,这样它就只受那个脚本的影响,而那个脚本只能影响那个元素?
【解决方案2】:

虽然它仍在移动,但我可能会再次将鼠标悬停在它上面,在这种情况下我想要 它重新启动,因此它停止影响完全相同的功能 按钮并重新启动另一个。我只是不知道如何识别 同一函数的哪个调用会影响我给定的组件。或者我应该 将此脚本添加到每个对象,以便它只影响那个对象?

一种解决方案是将包含协程函数的脚本附加到每个游戏对象。如果您不想这样做,那么还有另一种方法可以做到这一点。

首先,仅仅存储IEnumerator 是不够的。您需要一种方法来识别在最后一个协程中运行的 GameObject。

1.获取一个可用于标识对象的值。在这种情况下,GetInstanceID() 的实例 ID 应该没问题。

2. 制作一个简单的struct 来保存旧的协程信息。

3。最后,制作一个字典,其中包含实例 ID(key) 和 #2 中的结构体作为值。

有了这三样东西,就可以启动一个协程,添加到Dictionary。协程完成后,从Dictionary 中删除instance ID。

下面的代码将使一个协程控制多个对象,而无需将协程函数放在它自己的脚本中。最大的区别在于,当在同一函数上调用新的协程时,它仅停止该对象上的旧协程,然后创建一个新协程并启动它。

这是我使用的。它的注释很好,以便您知道发生了什么。

Dictionary<int, MoveInfo> movingObject = new Dictionary<int, MoveInfo>();

public void Move(Transform from, Transform to, float overTime)
{
    MoveInfo moveInfo;

    //Check if the Object exist in the Dictionay
    if (movingObject.TryGetValue(from.GetInstanceID(), out moveInfo))
    {
        //This Object exist and therefore, the coroutine function is already running. Stop it

        //Remove it from the Dictionary
        movingObject.Remove(from.GetInstanceID());

        //Stop the old coroutine
        StopCoroutine(moveInfo.currentCoroutine);
    }

    //Create a new coroutine
    moveInfo = createMoveInfoInstance(from);

    //Add it to the dictionary 
    movingObject.Add(from.GetInstanceID(), moveInfo);

    //Get instance of the new coroutine we are about to start
    moveInfo.currentCoroutine = _Move(moveInfo, from, to, overTime);

    //Modify the dictionary because the Add function does not update the currentCoroutine reference
    movingObject[from.GetInstanceID()] = moveInfo;

    //Start the coroutine
    StartCoroutine(moveInfo.currentCoroutine);
}

MoveInfo createMoveInfoInstance(Transform from)
{
    MoveInfo moveInfo = new MoveInfo();
    moveInfo.instanceID = from.GetInstanceID();
    return moveInfo;
}

IEnumerator _Move(MoveInfo moveInfo, Transform from, Transform to, float overTime)
{
    Vector2 original = from.position;
    float timer = 0.0f;

    Debug.Log("New Coroutine Started");
    while (timer < overTime)
    {
        float step = Vector2.Distance(original, to.position) * (Time.deltaTime / overTime);
        from.position = Vector2.MoveTowards(from.position, to.position, step);
        timer += Time.deltaTime;
        yield return null;
    }

    //Remove it from the Dictionary if it exist
    if (movingObject.ContainsKey(from.GetInstanceID()))
    {
        movingObject.Remove(from.GetInstanceID());
    }
}

public struct MoveInfo
{
    public IEnumerator currentCoroutine;
    public int instanceID;

    public MoveInfo(IEnumerator currentCoroutine, int instanceID)
    {
        this.currentCoroutine = currentCoroutine;
        this.instanceID = instanceID;
    }
}

【讨论】:

  • 高级的东西,对我来说可能有点太高级了,但这是我学习的方式:) 我尝试实现它,但如果失败,我仍然可以将我的函数编译为 .dll 文件并使用它们放在我的每个 UI 元素上的脚本中,其中 UI 元素和脚本具有 1:1 的关系(仅受此影响,仅受此影响)。
  • 这不是真正的进步。如果您复制它并将其替换为问题中的代码,它应该可以编译并正常工作。 此解决方案的目标是让您不必将移动代码附加到非常 UI 上。您只需传递变形和新药水,它就会解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多