Help us understand the problem. What is going on with this article?

UIPanel以下のUIWidgetのdepthを全部まとめて操作したい!!!

More than 5 years have passed since last update.

わかるで?

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class UIPanelDepthEditor: EditorWindow {

    [MenuItem("Window/UIPanelDepthEditor")]
    public static void ShowWindow() {
        EditorWindow win = EditorWindow.GetWindow<UIPanelDepthEditor>();
        win.title = "UIPanelDepthEditor";

    }

    void OnInspectorUpdate() {
        // reset collection
    }

    UnityEngine.Object target = null;

    void OnGUI() {

        bool isPanel = false;
        GUILayout.BeginVertical ();

        var replaceObj = EditorGUILayout.ObjectField( "ObjectField", target, typeof( Transform ), true );
        if (replaceObj != null)
        {
            target = replaceObj;
        }

        GUILayout.BeginHorizontal();
        {

            EditorGUILayout.PrefixLabel("Depth");

            if (GUILayout.Button("-10", GUILayout.MinWidth(46f)))
            {
                DepthControll((Transform)replaceObj,-10);
            }
            if (GUILayout.Button("Back", GUILayout.MinWidth(46f)))
            {
                DepthControll((Transform)replaceObj,-1);
            }

            var text = "";
            if (replaceObj != null)
            {
                var panel = ((Transform)replaceObj).GetComponent<UIPanel>();
                if (panel != null)
                {
                    text = panel.depth.ToString();
                    isPanel = true;
                }
                else
                {
                    var widget = ((Transform)replaceObj).GetComponent<UIWidget>();
                    text = widget.depth.ToString();
                }

            }

            EditorGUILayout.TextArea(text);

            if (GUILayout.Button("Forward", GUILayout.MinWidth(40f)))
            {
                DepthControll((Transform)replaceObj,1);
            }

            if (GUILayout.Button("+10", GUILayout.MinWidth(40f)))
            {
                DepthControll((Transform)replaceObj,10);
            }
        }
        GUILayout.EndHorizontal();
        if (!isPanel)
            EditorGUILayout.HelpBox ("これはUIPanelじゃないです", MessageType.Warning);
        EditorGUILayout.HelpBox ("UIPanel以下のUIWidgetをまとめてDepth操作する。\n ちょっと重たい", MessageType.Info);
        GUILayout.EndVertical();
    }


    void DepthControll(Transform obj,int depth)
    {
        foreach(var child in obj.gameObject.transform)
        {
            var item = child as Transform;
            var widget = item.GetComponent<UIWidget>();
            if (widget!= null)
                widget.depth += depth;
            else
            {
                var panel = item.GetComponent<UIPanel>();
                if(panel != null)
                    panel.depth += depth;
            }
            DepthControll(item,depth);

        }
    }
}
enpel
unity-game-dev-guild
趣味・仕事問わずUnityでゲームを作っている開発者のみで構成されるオンラインコミュニティです。Unityでゲームを開発・運用するにあたって必要なあらゆる知見を共有することを目的とします。
https://unity-game-dev-guild.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away