1. 程式人生 > >Unity3D編輯器

Unity3D編輯器

實現類似這樣的配置效果:
這裡寫圖片描述

    bool showorder = EditorGUILayout.Foldout(showorder, "圖片配置");
    if (showorder)
    {
        string v ="";
        EditorGUILayout.TextField("圖片&數值", num); //輸入的文字
         SpriteRenderer sp = (SpriteRenderer)EditorGUILayout.ObjectField("", mp.artSprite[i], typeof(SpriteRenderer), true
);//圖片 }

需要在監視面板重新編輯的類
Test1.cs

public class Test1: MonoBehaviour
{
        [Tooltip("監視面板提示資訊")]
        [SerializeField]
        [HideInInspector]
        public List<int> artValue = new List<int>();
        [Tooltip("監視面板提示資訊")]
        [SerializeField]
        [HideInInspector]
        public
List<SpriteRenderer> artSprite = new List<SpriteRenderer>(); [HideInInspector] [SerializeField] public int count; }

Test1Editor.cs

using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ArtClass))]
public class ArtClassEditor : UnityEditor.Editor
{
    bool showorder = false;
public override void OnInspectorGUI() { base.OnInspectorGUI(); showorder = EditorGUILayout.Foldout(showorder, "圖片配置"); if (showorder) { Test1 mp = (Test1)target; if (mp.count < mp.artValue.Count) { for (int i = mp.artValue.Count - 1; i >= mp.count; i--) { mp.artValue.RemoveAt(i); mp.artSprite.RemoveAt(i); } } else if (mp.count > mp.artValue.Count) { for (int i = mp.artValue.Count; i < mp.count; i++) { mp.artValue.Add(0); mp.artSprite.Add(null); } } mp.count = int.Parse(EditorGUILayout.TextField("數目:", mp.count.ToString())); if ( mp.artValue.Count > 0) { Rect r = (Rect)EditorGUILayout.BeginVertical("Color"); EditorGUILayout.Space(); for (int i = 0; i < mp.count; i++) { string num = mp.artValue[i].ToString(); EditorGUILayout.BeginHorizontal(); num = EditorGUILayout.TextField("圖片&數值", num); SpriteRenderer sp = (SpriteRenderer)EditorGUILayout.ObjectField("", mp.artSprite[i], typeof(SpriteRenderer), true); if (sp != null) { mp.artSprite[i] = sp.GetComponent<SpriteRenderer>(); } EditorGUILayout.EndHorizontal(); mp.artValue[i] = int.Parse(num); } EditorGUILayout.EndVertical(); } } } }