Skip to content

Instantly share code, notes, and snippets.

@olegknyazev
Created October 28, 2018 14:38

Revisions

  1. olegknyazev created this gist Oct 28, 2018.
    29 changes: 29 additions & 0 deletions formatting-find-or-die.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    bool FindMaskOrDie() {
    if (_destroyed)
    return false;
    mask = NearestMask(transform, out _affectedByMask)
    ?? NearestMask(transform, out _affectedByMask, enabledOnly: false);
    if (mask == null) {
    _destroyed = true;
    DestroyImmediate(this);
    return false;
    }
    return true;
    }

    static ISoftMask NearestMask(Transform transform, out bool processedByThisMask, bool enabledOnly = true) {
    processedByThisMask = true;
    var current = transform;
    while (true) {
    if (!current)
    return null;
    if (current != transform) { // Masks do not mask themselves
    var mask = GetISoftMask(current, shouldBeEnabled: enabledOnly);
    if (mask != null)
    return mask;
    }
    if (IsOverridingSortingCanvas(current))
    processedByThisMask = false;
    current = current.parent;
    }
    }