工作休息---unity中UnityEditor的使用

本人菜鸟一只,最近工作要求需要自己搭个服务器,然后unity资源从服务器中加载,再让资源在客户端动态加载出来,本人曾经接触过把自己的电脑做成服务器,然后局域网中可以访问你的文件夹中的资源, 没有涉及数据库(感觉自己好Low)。

还是进入正题吧,使用UnityEditor,在unity菜单栏中创建出一个菜单键,并且进入可以看见窗口。

仔细看可以看见多出来的Tools,然后就是点击进入出现的窗口,后面也就是将这些东西导出来;

接下来,我们就来聊聊怎么样建立出这个菜单项,举一个unity属性的栗子吧:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class ExportTools : EditorWindow{

 [MenuItem("Tools/Export")]
    static void Execute()
    {
       Debug.Log("=====");
    }

 void OnGUI()
    {

    }

}

需要注意的是MenuItem一定得写在方法前面,否则会报错哦!!还有方法必须是静态的。

接下来就是使用OnGUI写窗口,写button(其实本菜鸟OnGUI也不是很懂,但是还是看的懂滴)

    private string savePath;
    private GameObject exportObject;
    private int optionalCount = 0;
    private Texture2D[] optionalTexture = new Texture2D[0];

 void OnGUI()
    {
        exportObject = EditorGUILayout.ObjectField("Export Object", exportObject, typeof(GameObject), true) as GameObject;
        EditorGUILayout.Space();
        optionalCount = EditorGUILayout.IntField("Optional Count", optionalCount);
        for (int i = 0; i < optionalCount; i++)
        {
            if (optionalTexture.Length != optionalCount)
            {
                optionalTexture = new Texture2D[optionalCount];
            }
            EditorGUILayout.Space();
            optionalTexture[i] = EditorGUILayout.ObjectField("Optional Textures" + i, optionalTexture[i], typeof(Texture2D), false) as Texture2D;
        }
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.Space();

        if (GUILayout.Button("EXPORT", GUILayout.Width(100), GUILayout.Height(20)))
        {
         
        }
        EditorGUILayout.EndHorizontal();

    }

这样就实现简单的窗口了,但是还没有实现互动的哟

所以关键一步来了,实现导出按钮的功能:

 

 if (GUILayout.Button("EXPORT", GUILayout.Width(100), GUILayout.Height(20)))
        {
            if (Validate())
            {
                ExportAndSave(exportObject);
                Clear();
            }
            else
            {
                EditorUtility.DisplayDialog("error","back check","ok");
            }
        }

 

 

以下代码是在导出之前对资源的一些处理:

 private void ExportAndSave(GameObject go)//保存路径
    {
        savePath = EditorUtility.SaveFilePanel("Save",@"D:\",go.name,"unity3d");
        Debug.Log(savePath);
        Export(go,savePath);
    }

    private void Export(GameObject go, string filePath)
    {
        if (!EditorUtility.IsPersistent(go))//判断是否在project视图下
        {
            GameObject tmp = GameObject.Instantiate(go) as GameObject;
            go = GetPrefab(tmp,go.name) as GameObject;
        }
        Object[] asset = optionalTexture;
        if(File.Exists(filePath))File.Delete(filePath);
        BuildPipeline.BuildAssetBundle(go,asset,filePath,BuildAssetBundleOptions.CollectDependencies,BuildTarget.StandaloneWindows);
        AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(go));
    }

    private object GetPrefab(GameObject go,string name)
    {
        Object result = PrefabUtility.CreateEmptyPrefab("Assets/"+name+".prefab");
        result = PrefabUtility.ReplacePrefab(go,result);
        Object.DestroyImmediate(go);
        return result;
    }

    private bool Validate()//判断资源是否为空
    {
        bool b1 = (exportObject == null);
        bool b2 = false;

        foreach (Texture2D t in optionalTexture)
        {
            b2 = b2 || (t == null);
        }
        return !(b1||b2);
    }

最后

private void Clear()
    {
        exportObject = null;
        optionalCount = 0;
    }

全部重置

结束:菜鸟一名,也是从其他网站获取的知识,也是希望扩散扩散自己觉得有用的东西吧。

posted @ 2017-04-11 15:34  取个昵称真难  阅读(1526)  评论(0编辑  收藏  举报