当前位置:网站首页>unity编辑器扩展界面使用 List

unity编辑器扩展界面使用 List

2022-08-10 00:41:00 王源骏


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

[CustomEditor(typeof(TeachTool))]
public class TeachTool : EditorWindow
{
    
    [MenuItem("ConfigTool/添加图片或者视频")]
   public static void  ConfigTool()
    {
    
        //创建面板
        EditorWindow.GetWindow(typeof(TeachTool));
    }
    public UnityEngine.Object setting;
    [SerializeField]
    public List<Sprite> spriteLiset=new List<Sprite>();//图片的List
    //序列化对象
    protected SerializedObject _serializedObject;
    protected SerializedObject _serializedObject2;
    //序列化属性
    protected SerializedProperty _assetLstProperty;
    protected SerializedProperty _assetLstProperty2;

    [SerializeField]
    public List<UnityEngine.Object> MediaLiset = new List<UnityEngine.Object>();
    public string tempFiled = "";
    private void OnEnable()
    {
    
       
    }
    private void OnGUI()
    {
    
        GUILayout.Label("面板", EditorStyles.boldLabel);
        //配置文件 这个可以忽略 因为我写的这个工具是修改自己工程内配置文件的
        setting = EditorGUILayout.ObjectField(setting, typeof(TeachPointSetting), true);
        
        //输入框
        tempFiled = EditorGUILayout.TextField("名字:", tempFiled);
        //设置间距
        EditorGUILayout.Space(20);


        //初始化
        _serializedObject = new SerializedObject(this);
        //获取当前类中可序列化的属性
        _assetLstProperty = _serializedObject.FindProperty("spriteLiset");
        //更新
        _serializedObject.Update();
        //开始检查是否有修改
        EditorGUI.BeginChangeCheck();
        //显示属性,第二个参数必须为true 否者无法显示子节点即List内容
        EditorGUILayout.PropertyField(_assetLstProperty, true);
        //结束检查是否有修改
        if (EditorGUI.EndChangeCheck())
        {
    
            _serializedObject.ApplyModifiedProperties();
        }


       
        //设置间距
        EditorGUILayout.Space(10);
        //添加按钮
      if(GUILayout.Button("确认添加图片"))
        {
    
            SetSprsite();
            
         
        }

        //设置间距
        EditorGUILayout.Space(20);
        _serializedObject2 = new SerializedObject(this);
        _assetLstProperty2 = _serializedObject2.FindProperty("MediaLiset");
        //更新
        _serializedObject2.Update();
        //开始检查是否有修改
        EditorGUI.BeginChangeCheck();
        //显示属性,第二个参数必须为true 否者无法显示子节点即List内容
        EditorGUILayout.PropertyField(_assetLstProperty2, true);

       
        //结束检查是否有修改
        if (EditorGUI.EndChangeCheck())
        {
    
            _serializedObject2.ApplyModifiedProperties();
        }

        //设置间距
        EditorGUILayout.Space(10);
        //添加按钮
        if (GUILayout.Button("确认添加视频"))
        {
    
            SetMedia();


        }
    }

    /// <summary>
    /// 修改图片
    /// </summary>
    public void SetSprsite()
    {
    
        //修改图片逻辑

       
    }

    public void SetMedia()
    {
    
       //修改视频逻辑
           
       
    }
}

在这里插入图片描述

参考资料
https://blog.csdn.net/w_mumu_q/article/details/107240127
https://blog.csdn.net/puppet_master/article/details/51012298
https://blog.csdn.net/qq_35361471/article/details/84713382

原网站

版权声明
本文为[王源骏]所创,转载请带上原文链接,感谢
https://blog.csdn.net/o_ojjj/article/details/126100192