Unity深入讲解Unity技术分享征服Unity3d

Unity 缺省资源编辑器

2016-03-15  本文已影响227人  2b75747cf703
using UnityEngine;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(DefaultAsset))]
public class DefaultAssetEditor : Editor
{
    private FolderAssetInspector folderAssetInspector;
    private AssetBundleAssetInspector assetBundleAssetInspector;


    void Awake()
    {
        folderAssetInspector = new FolderAssetInspector(targets);
        assetBundleAssetInspector = new AssetBundleAssetInspector(targets);
    }

    void OnDestroy()
    {

    }

    public override void OnInspectorGUI()
    {
        GUI.enabled = true;

        if(folderAssetInspector != null)
            folderAssetInspector.OnInspectorGUI();

        if(assetBundleAssetInspector != null)
            assetBundleAssetInspector.OnInspectorGUI();
    }
}
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

public class FolderAssetInspector
{
    private Object[] targets;

    private string filter;
    private string[] searchInFolders;

    private string[] assets;

    public FolderAssetInspector(Object[] targets)
    {
        this.targets = targets;

        var folders = new List<string>();

        for (int i = 0; i < targets.Length; i++)
        {
            string assetPath = AssetDatabase.GetAssetPath(targets[i]);
            if(Directory.Exists(assetPath))
                folders.Add(assetPath);
        }

        searchInFolders = folders.ToArray();
    }

    void OnDestroy()
    {

    }

    public void OnInspectorGUI()
    {
        if(searchInFolders.Length != targets.Length)
            return;

        filter = EditorGUILayout.TextField(filter);

        if(GUILayout.Button("查找"))
        {
            assets = AssetDatabase.FindAssets(filter, searchInFolders);
        }

        for (int i = 0; assets != null && i < assets.Length; i++)
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(assets[i]);
            if(GUILayout.Button(new GUIContent(assetPath, AssetDatabase.GetCachedIcon(assetPath)), "Label"))
            {
                var asset = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
                Selection.activeObject = asset;
            }
        }
    }
}
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class AssetBundleAssetInspector
{
    private Object[] targets;

    private List<string> assetPaths = new List<string>();

    public AssetBundleAssetInspector(Object[] targets)
    {
        this.targets = targets;

        var paths = new List<string>();

        for (int i = 0; i < targets.Length; i++)
        {
            string assetPath = AssetDatabase.GetAssetPath(targets[i]);
            if(File.Exists(assetPath) && assetPath.EndsWith(".unity3d"))
                paths.Add(assetPath);
        }

        if (paths.Count == targets.Length)
        {
            foreach (var path in paths)
            {
                var assetBundle = AssetBundle.LoadFromFile(path);
                if (assetBundle != null)
                {
                    assetPaths.AddRange(assetBundle.GetAllAssetNames());
                    assetPaths.AddRange(assetBundle.GetAllScenePaths());
                    assetBundle.Unload(true);
                }
            }
        }
    }

    ~AssetBundleAssetInspector()
    {

    }

    public void OnInspectorGUI()
    {
        for (int i = 0; i < assetPaths.Count; i++)
        {
            var assetPath = assetPaths[i];
            if (GUILayout.Button(new GUIContent(assetPath, AssetDatabase.GetCachedIcon(assetPath)), "Label"))
            {
                var asset = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
                Selection.activeObject = asset;
            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读