Towards the bottom of this thread a Unity dev gives instructions for overwriting a base class by copy pasting base code then changing variable names from field name to property name.
My understanding here goes as far as simply knowing what a field and what a property is.
Can anyone help me out? Where do I find the base class code to copy and paste? Why/how do I swap field and property names?
The concept is called overriding, and is part of some of the core concepts of Object Oriented Programming (OOP). When defining the method, you use the keyword override to tell the compiler to ignore the code from the base class and use this instead (when referring to the new object).
Usually in an IDE you can just right click the method and click “Go to Definition” and it should just bring you to where it is defined as long as you actually have the package installed. I went through the trouble of actually finding it, it’s located at
I ended up also looking through the code too, but it looks like it has since been updated, so following the exact advice probably won’t work anymore, and it might even be solved already, but I don’t use XR so I wouldn’t know.
My guess to this was that it was because some of the fields are private, and you cannot access private fields from an inherited class, but the current code doesn’t use any so it’s hard to say.
Kurt, I appreciate the help but if you look into the post I linked you can see that chris-massie talks about overriding the method InitializeDynamicAttachPose and copy-pasting some code from a base class to remove a check. This is not my base class.
Also, What he is discussing in that post is what I’m trying to do. Retain force grab functionality while also using dynamic attach with ray interactors.
Following their instructions you would create your own custom script and inherit from that class:
public class MyCustomXRGrabInteractable : XRGrabInteractable
{
}
Then you’d override the InitializeDynamicAttachPose:
public class MyCustomXRGrabInteractable : XRGrabInteractable
{
protected override void InitializeDynamicAttachPose(IXRSelectInteractor interactor, Transform dynamicAttachTransform)
{
}
}
Then they say to copy the code from the method and paste it into your code. So I’m copying this method’s code:
So if I copied it’d look like this:
public class MyCustomXRGrabInteractable : XRGrabInteractable
{
protected override void InitializeDynamicAttachPose(IXRSelectInteractor interactor, Transform dynamicAttachTransform)
{
var matchPosition = ShouldMatchAttachPosition(interactor);
var matchRotation = ShouldMatchAttachRotation(interactor);
if (!matchPosition && !matchRotation)
return;
// Copy the pose of the interactor's attach transform
var interactorAttachTransform = interactor.GetAttachTransform(this);
var position = interactorAttachTransform.position;
var rotation = interactorAttachTransform.rotation;
// Optionally constrain the position to within the Collider(s) of this Interactable
if (matchPosition && ShouldSnapToColliderVolume(interactor) &&
XRInteractableUtility.TryGetClosestPointOnCollider(this, position, out var distanceInfo))
{
position = distanceInfo.point;
}
if (matchPosition && matchRotation)
dynamicAttachTransform.SetPositionAndRotation(position, rotation);
else if (matchPosition)
dynamicAttachTransform.position = position;
else
dynamicAttachTransform.rotation = rotation;
}
}
Now… I’m not going to dig that far into it cause I don’t feel like reading this massive class. But 1 or 2 of the variables in here may be private to the class we inherited from. We’d have to change it to the public property if that was so. My quick review says there isn’t… but maybe there used to be a version of the code that did. With that said this leads me to the next issue following the post you’re referring to…
So unfortunately from here the code has changed since whenever the post is from so the code is not the same (the referenced line no longer exists). They refer to removing a line that checks “interactor is XRRayInteractor rayInteractor && rayInteractor.useForceGrab”… which as you can see is not in the code I copied from that source (of course I’m just using the first github project I found that matched. I didn’t put a lot of effort into finding the project YOU’RE specifically looking for because well… I ain’t got time for that).
But lets just pretend it did. Lets say we want to remove one of the “early returns”… so we do that:
public class MyCustomXRGrabInteractable : XRGrabInteractable
{
protected override void InitializeDynamicAttachPose(IXRSelectInteractor interactor, Transform dynamicAttachTransform)
{
var matchPosition = ShouldMatchAttachPosition(interactor);
var matchRotation = ShouldMatchAttachRotation(interactor);
//SNIPPED RETURN CHECK
// Copy the pose of the interactor's attach transform
var interactorAttachTransform = interactor.GetAttachTransform(this);
var position = interactorAttachTransform.position;
var rotation = interactorAttachTransform.rotation;
// Optionally constrain the position to within the Collider(s) of this Interactable
if (matchPosition && ShouldSnapToColliderVolume(interactor) &&
XRInteractableUtility.TryGetClosestPointOnCollider(this, position, out var distanceInfo))
{
position = distanceInfo.point;
}
if (matchPosition && matchRotation)
dynamicAttachTransform.SetPositionAndRotation(position, rotation);
else if (matchPosition)
dynamicAttachTransform.position = position;
else
dynamicAttachTransform.rotation = rotation;
}
}
Now becuase I’ve overriden the method and I do not call base.InitlaizeDynamicAttachPose. That means anywhere I use this script in place of XRGrabInteractable, this logic here that we’ve altered will be what gets ran.
If you’re trying to do this for something unrelated to this XR library… well… it really pertains to how the person who wrote the library in question wrote it. If it didn’t adequately define their methods as virtual, you may not be able to do a similar thing. Or you’ll have to do it a different way.