【问题标题】:Call function from a premade script in Unity从 Unity 中的预制脚本调用函数
【发布时间】:2020-08-02 00:03:11
【问题描述】:

如何从附加到光线交互器的 Unity(XR 交互工具包)脚本中调用函数?我所拥有的正是一个作为 XR 射线交互器制作的游戏对象,并附加了 XR 直接交互器脚本。我想从该脚本调用函数 GetValidTargets 以查看它的目标游戏对象。如何在另一个脚本中调用此函数?我已经指定了 GameObject 并尝试了不同的方法来实现它,例如 GetComponent()。

这是函数在脚本中所说的,所以在我调用它之后会出现。

        /// Retrieve the list of interactables that this interactor could possibly interact with this frame.
        /// This list is sorted by priority (in this case distance).
        /// <param name="validTargets">Populated List of interactables that are valid for selection or hover.</param>
        public override void GetValidTargets(List<XRBaseInteractable> validTargets)

如何调用这个函数?

【问题讨论】:

    标签: unity3d virtual-reality


    【解决方案1】:

    如果您想调用该函数,请将XRBaseInteractable 组件附加到您希望能够悬停或与之交互的每个游戏对象。创建一个 List(位于 System.Collections.Generic 命名空间中),将每个 XRBaseInteractable 实例添加到其中并将其传递给 GetValidTargets() 函数。

    GameObject[] interactableObjects; // Set in inspector or somewhere in code
    List<XRBaseInteractable> interactables = new List<XRBaseInteractable>();
    foreach(GameObject interactableObject in interactableObjects)
    {
        interactables.Add(interactableObject.GetComponent<XRBaseInteractable>());
    }
    GetComponent<XRDirectInteractor>().GetValidTargets(interactables);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多