2017年4月29日 星期六

隱藏 Inspector 腳本數值

-------物件腳本屬性 C#-------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Biochemical_Single : ObjectClass {

    // 物件模式
    public enum SelectObj
    {
        roteObj,
        moveObj,
    }
    public SelectObj objsType;

    // 物件行為方向
    public enum Direction
    {
        forward,
        up,
        down,
        left,
        right,
    }
    public Direction direction;

    public Transform tramsform;

    public float moveSpeed, roteSpeed; // 物件行為速度
  
    // Update is called once per frame
    void Update () {

        switch (objsType)
        {
            case SelectObj.moveObj:
                
                    Move(tramsform, driectionFunction(direction), moveSpeed);

                break;

            case SelectObj.roteObj:

                
                    Rote(tramsform, driectionFunction(direction), roteSpeed);
                break;
        }
    }

    /// 
    /// 方向
    /// 
    /// 
    /// 
    Vector3 driectionFunction(Direction direction)
    {
        Vector3 vector3 = Vector3.zero;

        switch (direction)
        {
            case Direction.forward:
                vector3 = Vector3.forward;
                break;
            case Direction.up:
                vector3 = Vector3.up;
                break;
            case Direction.down:
                vector3 = Vector3.down;
                break;
            case Direction.left:
                vector3 = Vector3.left;
                break;
            case Direction.right:
                vector3 = Vector3.right;
                break;
        }
        return vector3;
    }
}

---更改Inspector 腳本GUI顯示(C#)--------


using UnityEngine;
using UnityEditor;
using System;

[CustomEditor(typeof(Biochemical_Single))]  // 更改 Inspector 腳本
public class MyScriptEditor : Editor {

    override public void OnInspectorGUI()
    {
        var script = target as Biochemical_Single;

        script.objsType = (Biochemical_Single.SelectObj)EditorGUILayout.EnumPopup("select Obj", script.objsType);
        script.direction = (Biochemical_Single.Direction)EditorGUILayout.EnumPopup("direction", script.direction);
        script.tramsform = EditorGUILayout.ObjectField(script.tramsform ,typeof(Transform), true) as Transform;

        if (script.objsType == Biochemical_Single.SelectObj.roteObj)
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.PrefixLabel("roteSpeed");
            script.roteSpeed = EditorGUILayout.Slider(script.roteSpeed, script.roteSpeed, 999);
            EditorGUI.indentLevel--;
        }
    }
}
--------執行結果---------
 Select Obj 選擇項目 : Rote Obj














 Select Obj 選擇項目 : Move Obj