Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A way to disable and enable OnTransformChanged events..

Discussion in 'Scripting' started by TheHeftyCoder, Oct 16, 2020.

  1. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Hey, does anyone know if there is a way to disable the OnTransform events?

    Background : Suppose that you have a parent component (meaning, it's attached to a transform parent) and a child component (meaning, it's attached to the transform that is a child of the specified transform parent) that are supposed to interact.

    Argument : There's code in the OnTransformChildrenChanged event. You have a function that you know is going to change ALL of the children, i.e clear the children and then add another list of children. The OnTransformChildrenChanged event procs for EVERY child added or removed. That may be hugely unnecessary. It's much more preferable to disable that function, then call it when all the new children are set and then proceed to enable it again. [This kind of event mostly utilizes GetComponentsInChildren.

    Counter-Argument: Someone could argue that we can do what I'm explaining by utilizing the child instead of the parent, with the OnTransformParentChanged event [This event mostly utilizes the parent.GetComponent]. While that is true, consider these thoughts;

    a) The child invokes that event even when it is unneeded. In other words, you'd have unnecessary parent.GetComponent calls, which in that case are going to be MUCH slower when the component won't exist in the parent.
    b) GetComponentsInChildren is optimized and MAY perform much better than multiple and potentially unnecessary parent.GetComponent for the parent.

    A solution where you make your own TransformChanged event and control it could be acceptable, but it probably won't work as good - fast as the native events. I realize this might be a niche thing to ask, since performance probably won't be affected in most cases, but I found it interesting enough to post about it.

    Some transform methods that add an IEnumerable<Transform> to the transform and invoking the event at the end would be nice. Currently, I haven't checked if transform.DetachChildren() invokes the OnTransformChildrenChanged event once or for every child. In my opinion, having the event be invoked once or twice instead of n-th times should be compelling enough for such small (I assume) changes.​
     
    Last edited: Oct 16, 2020
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Would it be practical to remove the components that contained the OnTransform__ methods, make your changes, then add those components back again?
     
  3. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Not really, for 2 reasons :
    a) Not really so important, but I'm trying to make some systems that work out of the box with Unity and its events. Removing the component would mean to i) know what the component is before-hand; ii) removing it from the generic system I've made. Those two would mean that I would have to create some kind of interface for all of my components to work together nicely.

    b) This one is important. The post was made in an effort to maximize performance. Removing and adding components would result in a similar or potentially greater overhead than simply allowing the OnTransformChanged events to run naturally.
     
  4. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    37
    You might be able to achieve what you want by setting bool flags in OnEnable and OnDisable.
    then on the ontransformchildrenchanged you call a delegate or method, and within that method you check if the flags are enabled. if the flags are enabled, then run the code you want.
     
    TheHeftyCoder likes this.