1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Unity] RequireComponentAttributeの or 版を作りたかった

Posted at

概要

UIの基盤を実装する時、たまに↓みたいなことをしたくなることがあった

[RequireComponent( typeof(VerticalLayoutGroup) or typeof(HorizontalLayoutGroup) )]

そろそろ重い腰を上げて調べたところ、割と簡単にできそうだったので備忘録として要点をまとめる

実装

// コンポーネントを追加した時が取れるので、ここで検証を行う
ObjectChangeEvents.changesPublished += (ref ObjectChangeEventStream stream) =>
{
    for (int i = 0; i < stream.length; i++)
    {
        switch (stream.GetEventType(i))
        {
            case ObjectChangeKind.ChangeGameObjectStructure:
                stream.GetChangeGameObjectStructureEvent(i, out ChangeGameObjectStructureEventArgs args);
                var go = EditorUtility.InstanceIDToObject(args.instanceId) as GameObject;
                // 具体的に何が起こったかは分からないので、持ってるComponentを全て洗う
                break;

            // こっちは子に変更が入った時に呼ばれてそう(蛇足)
            case ObjectChangeKind.ChangeGameObjectStructureHierarchy:
                break;
        }
    }
};

実装の全容はここから
手抜きでエラー出すだけだが、個人的には良い感じ

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?