LoginSignup
2
4

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-04-02

わかるで?

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);

        }
    }
}
2
4
2

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
2
4