Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Unity中的运行时实例化Oculus grabbable对象?_C#_Unity3d_Scripting_Window_Oculus - Fatal编程技术网

C# 如何在Unity中的运行时实例化Oculus grabbable对象?

C# 如何在Unity中的运行时实例化Oculus grabbable对象?,c#,unity3d,scripting,window,oculus,C#,Unity3d,Scripting,Window,Oculus,现在我在Unity有一个obj文件。我有一个脚本,它向对象添加了一个碰撞器组件、一个刚体组件,最后是一个OVRGrabbable组件。我需要在运行时添加这些组件,因为最终我将在运行时在脚本中生成程序网格,并且我需要这些程序网格是可抓取的 我的问题是,当在运行时添加碰撞器时,OVRGrabbable脚本无法将添加的碰撞器识别为抓取点。我认为在我的脚本中,在OVRGrabbable之前添加碰撞器就足够了,但是没有骰子。我尝试在唤醒函数中连接碰撞器,然后在启动函数中连接OVRGrabbable,但也没

现在我在Unity有一个obj文件。我有一个脚本,它向对象添加了一个碰撞器组件、一个刚体组件,最后是一个OVRGrabbable组件。我需要在运行时添加这些组件,因为最终我将在运行时在脚本中生成程序网格,并且我需要这些程序网格是可抓取的

我的问题是,当在运行时添加碰撞器时,OVRGrabbable脚本无法将添加的碰撞器识别为抓取点。我认为在我的脚本中,在OVRGrabbable之前添加碰撞器就足够了,但是没有骰子。我尝试在唤醒函数中连接碰撞器,然后在启动函数中连接OVRGrabbable,但也没有成功。此外,我无法将其添加到脚本中,因为grabPoints数组是只读的。这是我的密码:

     public class AddVRComponents : MonoBehaviour {
     void Start () {
         public bool freeMoving = false;
         public bool useGravity = false;

         collide = gameObject.AddComponent<BoxCollider>();

         Rigidbody rB = gameObject.AddComponent<Rigidbody>();
         if (!freeMoving)
         {
             rB.drag = Mathf.Infinity;
             rB.angularDrag = Mathf.Infinity;
         }
         if (!useGravity)
         {
             rB.useGravity = false;
         }

         OVRGrabbable grab = gameObject.AddComponent<OVRGrabbable>();
         Collider[] newGrabPoints = new Collider[1];
         newGrabPoints[0] = collide;
         grab.enabled = true;
         grab.grabPoints = newGrabPoints;
     }
 }
公共类AddVRComponents:monobhavior{
无效开始(){
公共布尔自由移动=假;
公共bool useGravity=false;
collide=gameObject.AddComponent();
刚体rB=gameObject.AddComponent();
如果(!自由移动)
{
rB.drag=数学无穷大;
rB.angularDrag=数学无穷大;
}
如果(!使用重力)
{
rB.useGravity=false;
}
ovrgrabbablegrab=gameObject.AddComponent();
Collider[]newGrabPoints=新碰撞器[1];
newgrabboints[0]=碰撞;
grab.enabled=true;
grab.grabboints=新的grabboints;
}
}
这显然不起作用,因为最后一行生成的错误是grab.grabboints是只读的

我知道这是可以做到的,因为如果我运行我的程序,然后在编辑器中手动将碰撞器拖动到OVRGrabbable组件的“抓取点”字段中,可以抓取对象


如何让OVRGrabbable脚本识别我的碰撞器

只读属性是只能通过包含它们的脚本分配的属性。这意味着您只能从OVRGrabbable.cs脚本内部更改Grabboints的值

最好的方法是在OVRGrabbable.cs文件中添加一个自定义函数,以便访问和设置只读变量

我使用此选项,以便还可以设置snapPosition和snapOrientation字段以及抓取点

public void Initialize(bool snapPosition, bool snapOrientation, Collider[] grabPoints)
{
    m_snapPosition = snapPosition;
    m_snapOrientation = snapOrientation;
    m_grabPoints = grabPoints;
}
您只需添加并调用以下内容即可:

public void Initialize(Collider[] grabPoints)
{
    m_grabPoints = grabPoints;
}

你能在所有这些组件都设置好的情况下创建一个预置,然后实例化预置吗?