Why code when button color change can be done on unity editor?

What’s the purpose of changing button color through code when it can be easily done through editor? Is there a real advantage? When do we need to code for button color change and in which situations is using only unity editor the right thing?

This is mostly a personal choice but the advantage of code vs the button is the code is actually simpler. Updating the UI is a lot more complicated and if you have many buttons, this is even more time consuming.

Doing it via code can give you a lot more flexibility.

Oh so that’s good for programmers.
What does this code do? I understand it overrides, but does it override this entire function?
protected override void OnSelectEntered(SelectEnterEventArgs args)

And what is the use of this code?
base.OnSelectEntered(args);

The class inherits from a class which implements the OnSelectEntered - to implement you must create an override of the base method. The second line is related to calling the base method. This is because you want the original functionality to execute (this isn’t always the case but is here)

I hope this makes sense. This is all related to standard Object Oriented Programming.

base.OnSelectEntered(args);
So this code means it will execute the original function first.
and then execute the override function?

protected override void OnSelectEntered(SelectEnterEventArgs args)
I couldn’t understand this code. Which class inherits from which class? And what does SelectEnterEventArgs do? Please explain. I have studies OOP but this seems difficult.

Well, at the top of most unity classes, you inherit from a class MonoDevelop I think it is. (I’m actually an Unreal Dev but also VR)

This class, whatever it’s name is looks like

Class myclass : MonoDevelop {

}

So, whatever appears after the class is what you are inheriting from and when you use base, you are calling the method in that version of the class.

When you use the override in a method declaration, you are overriding something in that same class you inherit from,in this case it is MonoDevelop.

Does this make sense?

Taking the example
Class myclass: MonoDevelop {…}

When I use BASE, I am calling method from myclass?
And when I use OVERRIDE, I am calling method from MonoDevelop?

Base calls from the class you are inheriting from, ie monodevelop. This is the base class for your class. When you use override, you are saying you want to create a method that overrides monodevelops version. You can still call the original using base.

I am confused because in the DRAWER videos, there is a DRAWERUNLOCKED function with SelectEnterEventArgs which is then overridden by another function. But in BUTTON videos, there was direct OVERRIDE function for SelectEnterEventArgs. What is this overriding? Why is this different from the technique followed in Drawer videos.

Below is the code in drawer video and button video

DRAWER CODE
private void OnDrawerUnlocked(SelectEnterEventArgs arg0)
{
isLocked = false;
if(keyIndicatorLight != null)
{
keyIndicatorLight.SetActive(false);
}
Debug.Log("****DRAWER UNLOCKED");
}

protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
if (!isLocked)
{
transform.SetParent(parentTransform);
isGrabbed = true;
}
else
{
ChangeLayerMask(defaultLayer);
}
}

BUTTON CODE
protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
isPressed = true;
buttonImage.color = pressedColor;
}

DrawerUnlocked is not overriding anything.

Yes, DrawerUnlocked is not overriding anything. DrawerUnlocked came first, then it is getting overrided for SelectEnterEventArgs. (For drawer)
protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
if (!isLocked)
{
transform.SetParent(parentTransform);
isGrabbed = true;
}
else
{
ChangeLayerMask(defaultLayer);
}
}

However, in the button interactable videos, there is an override function directly. So what is this overriding? There is no function prior to get overrided.
protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
isPressed = true;
buttonImage.color = pressedColor;
}

There is, you just don’t see it. It is in the base class. So, hopefully this can help. The DrawerInteractible class here inherits from XRGrabInteractable. This class has its own implementation. More below

The XRGrabInteractable itself inherits from XRBaseInteractable

This overrides itself methods whichi s overridden in the DrawerInteractible.

As you can see, this also inherits from a selection of things. C# only permits inheriting from a single class and so the other items after MonoBehaviour (That’s the one!!!) are what are called interfaces - they define methods but do not implement anything and they themselves do nothing, a bit like a contract

This all gets complicated. So, when you call a base in this case, it is basically calling at first XRGrabInteractable.OnSelectEntered. This in turn calls XRBaseInteractable.OnSelectEntered.

It is a little complex but it is important to understand. I hope this helps somewhat

Thank you very much. I understood about the drawer.

What about the button in the button interactable videos - there is an override function directly. So what is this overriding? There is no function prior to get overrided (like DrawerUnlocked in drawer video)

protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
isPressed = true;
buttonImage.color = pressedColor;
}

Again, there is a function, this is in the base class. If you don’t add the function, it still gets called…in the base class. When you override it, your one gets called and it calls the base class first then does the 2 lines of code you have after that call.

Thank you so much

Privacy & Terms