[Unity] Editor Windows 物件編輯器

選擇prefab母物件 會自動讀取子物件

在Asset新增Editor資料夾,把editor windows的程式碼放到此資料夾



using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;



public class Fenetre : EditorWindow {

    Vector2 scrollPosition = Vector2.zero;





    private float scale_X = 1;

    private float scale_Y = 1;

    private float scale_Z = 1;



    private float rotation_X = 0;

    private float rotation_Y = 0;

    private float rotation_Z = 0;



    private float position_X = 0;

    private float position_Y = 0;

    private float position_Z = 0;



    private float[] ScaleArray_X = new float[100];

    private float[] ScaleArray_Y = new float[100];

    private float[] ScaleArray_Z = new float[100];



    private float[] PositionArray_X = new float[100];

    private float[] PositionArray_Y = new float[100];

    private float[] PositionArray_Z = new float[100];



    private float[] RotationArray_X = new float[100];

    private float[] RotationArray_Y = new float[100];

    private float[] RotationArray_Z = new float[100];



    [MenuItem("MonMenu/ObjectEditor")]



    public static void ShowWindow()

    {

        EditorWindow.GetWindow(typeof(Fenetre));

    }



    void OnSelectionChange()

    {

        initialSelection();

    }

    void Awake(){

        for (int i = 0; i < ScaleArray_X.Length; i++)

        {

            ScaleArray_X[i] = 1;

            ScaleArray_Y[i] = 1;

            ScaleArray_Z[i] = 1;

        }



        initialSelection();

    }

    void initialSelection() {

        scale_X = Selection.activeTransform.localScale.x;

        scale_Y = Selection.activeTransform.localScale.y;

        scale_Z = Selection.activeTransform.localScale.z;







        position_X = Selection.activeTransform.localPosition.x;

        position_Y = Selection.activeTransform.localPosition.y;

        position_Z = Selection.activeTransform.localPosition.z;



        rotation_X = Selection.activeTransform.eulerAngles.x;

        rotation_Y = Selection.activeTransform.eulerAngles.y;

        rotation_Z = Selection.activeTransform.eulerAngles.z;



        Transform trans = null;

        for (int i = 0; i < Selection.activeTransform.childCount; i++)

        {

            trans = Selection.activeTransform.GetChild(i);

            ScaleArray_X[i] = trans.localScale.x;

            ScaleArray_Y[i] = trans.localScale.y;

            ScaleArray_Z[i] = trans.localScale.z;

            PositionArray_X[i] = trans.localPosition.x;

            PositionArray_Y[i] = trans.localPosition.y;

            PositionArray_Z[i] = trans.localPosition.z;

            RotationArray_X[i] = trans.eulerAngles.x;

            RotationArray_Y[i] = trans.eulerAngles.y;

            RotationArray_Z[i] = trans.eulerAngles.z;

        }





    }

    void OnGUI() {

        scrollPosition = GUILayout.BeginScrollView(scrollPosition, true, true, GUILayout.Width(500), GUILayout.Height(700));

        //GUILayout.BeginArea(new Rect(0, 0, 300, 300));    //Does not display correctly if this is not commented out!



        //GUILayout.Button("I am a button", GUILayout.MinWidth(150), GUILayout.MinHeight(150));



        //GUILayout.EndArea();

      





        GUILayout.Label("ObjectEditor", EditorStyles.largeLabel);

        //int i = Selection.activeGameObject.transform.GetChildCount();

        if (Selection.activeGameObject != null) {

                Repaint();

                //物件名稱

                EditorGUILayout.TextField("Object selection", Selection.activeGameObject.name);

                //scale X slider

                GUILayout.Label("scale", EditorStyles.label);

                scale_X = EditorGUILayout.Slider(scale_X, 1, 3);

                scale_Y = EditorGUILayout.Slider(scale_Y, 1, 3);

                scale_Z = EditorGUILayout.Slider(scale_Z, 1, 3);



                //position X

                GUILayout.Label("X position", EditorStyles.label);

                position_X = EditorGUILayout.Slider(position_X, -100, 100);



                //position Y

                GUILayout.Label("Y position", EditorStyles.label);

                position_Y = EditorGUILayout.Slider(position_Y, -100, 100);



                //position Z

                GUILayout.Label("Z position", EditorStyles.label);

                position_Z = EditorGUILayout.Slider(position_Z, -100, 100);



                //rotation slider

                GUILayout.Label("X rotate", EditorStyles.label);

                rotation_X = EditorGUILayout.Slider(rotation_X, 0, 360);



                GUILayout.Label("Y rotate", EditorStyles.label);

                rotation_Y = EditorGUILayout.Slider(rotation_Y, 0, 360);



                GUILayout.Label("Z rotate", EditorStyles.label);

                rotation_Z = EditorGUILayout.Slider(rotation_Z, 0, 360);



                if (GUILayout.Button("close"))

                {

                    this.Close();

                }







            Modify(Selection.activeGameObject.transform);

        }

        GUILayout.EndScrollView();



    }

     void Modify(Transform parent)

    {

        Transform trans = null;

        for (int i = 0; i < parent.childCount; i++)

        {

            trans = parent.GetChild(i);

            //EditorGUILayout.TextField("Object selection", trans.name);

            GUILayout.Label("---------------------------------------------------------------------" , EditorStyles.boldLabel);

            GUILayout.Label("Object Name: "+ trans.name, EditorStyles.largeLabel);



            GUILayout.Label("scale", EditorStyles.label);

            ScaleArray_X[i] = EditorGUILayout.Slider(ScaleArray_X[i], 1, 3);

            ScaleArray_Y[i] = EditorGUILayout.Slider(ScaleArray_Y[i], 1, 3);

            ScaleArray_Z[i] = EditorGUILayout.Slider(ScaleArray_Z[i], 1, 3);



            GUILayout.Label("Position", EditorStyles.label);

            PositionArray_X[i] = EditorGUILayout.Slider(PositionArray_X[i], -100, 100);

            PositionArray_Y[i] = EditorGUILayout.Slider(PositionArray_Y[i], -100, 100);

            PositionArray_Z[i] = EditorGUILayout.Slider(PositionArray_Z[i], -100, 100);



            GUILayout.Label("Rotation", EditorStyles.label);

            RotationArray_X[i] = EditorGUILayout.Slider(RotationArray_X[i], 0, 360);

            RotationArray_Y[i] = EditorGUILayout.Slider(RotationArray_Y[i], 0, 360);

            RotationArray_Z[i] = EditorGUILayout.Slider(RotationArray_Z[i], 0, 360);





        }



      

        //EditorGUILayout.TextField("Object selection", trans.name);

        //GUILayout.Label("scale", EditorStyles.label);

        //scale_X = EditorGUILayout.Slider(scale_X, 1, 3);

        //scale_Y = EditorGUILayout.Slider(scale_Y, 1, 3);

        //scale_Z = EditorGUILayout.Slider(scale_Z, 1, 3);

    }





    void OnInspectorUpdate() {

        if (Selection.activeTransform) {

            Selection.activeTransform.localScale = new Vector3(scale_X, scale_Y, scale_Z);

            //Selection.activeTransform.localRotation = new Quaternion(rotation_X, rotation_Y, rotation_Z,10.0f);

            //Selection.activeTransform.localRotation.eulerAngles = new Quaternion

            Selection.activeTransform.localPosition = new Vector3(position_X, position_Y, position_Z);

            //Selection.activeTransform.localRotation = new Quaternion(rotation_X, rotation_Y, rotation_Z,10);

            Selection.activeTransform.localEulerAngles = new Vector3(rotation_X, rotation_Y, rotation_Z);



            Transform trans = null;

            for (int i = 0; i < Selection.activeTransform.childCount; i++)

            {

                trans = Selection.activeTransform.GetChild(i);

                trans.localScale = new Vector3(ScaleArray_X[i], ScaleArray_Y[i], ScaleArray_Z[i]);

                trans.localPosition = new Vector3(PositionArray_X[i], PositionArray_Y[i], PositionArray_Z[i]);

                trans.localEulerAngles = new Vector3(RotationArray_X[i], RotationArray_Y[i], RotationArray_Z[i]);

            }



        }

    }

    void Rotate() {

        Quaternion rotation = Random.rotation;

        Selection.activeTransform.localRotation = Quaternion.Slerp(Selection.activeTransform.localRotation, new Quaternion(60.0f,0.0f,0.0f,1), 0);

    }



 // Use this for initialization

 void Start () {

  

 }

 

 // Update is called once per frame

 void Update () {

  

 }

}











reference:
https://www.youtube.com/watch?v=hu8MkGMRZW8
http://www.arkaistudio.com/blog/2016/07/08/%E5%BF%AB%E9%80%9F%E9%96%8B%E7%99%BC%E5%B0%8F%E8%A8%A3%E7%AB%85%E4%BA%8C%EF%BC%9A%E5%BD%88%E6%80%A7%E4%BF%AE%E6%94%B9%E5%A4%9A%E5%80%8B%E7%89%A9%E9%AB%94/

留言

這個網誌中的熱門文章

Bundle Adjustment 光束平差法

Structure From Motion