Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

Organization

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

わかるで?

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

        }
    }
}
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
4
Help us understand the problem. What are the problem?