Example #1
0
    /// <summary>
    /// Asset Bundle-like export, except using DataNode's deep serialization.
    /// </summary>

    static public void ExportAssets(DataNode.SaveType type, string path)
    {
        EditorUtility.DisplayCancelableProgressBar("Working", "Collecting references...", 0f);

        ComponentSerialization.ClearReferences();

        var objects    = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        var components = new System.Collections.Generic.HashSet <string>();

        foreach (var obj in objects)
        {
            if (obj is MonoScript)
            {
                var s = obj.name;
                if (!components.Contains(s))
                {
                    components.Add(s);
                }
                continue;
            }

            var go = obj as GameObject;

            if (go)
            {
                go.CollectReferencedPrefabs(true);
                var comps = go.GetComponentsInChildren <MonoBehaviour>(true);

                foreach (var comp in comps)
                {
                    var t = comp.GetType().ToString();
                    if (!components.Contains(t))
                    {
                        components.Add(t);
                    }
                }
            }
            else
            {
                ComponentSerialization.AddReference(obj);
            }
        }

        EditorUtility.DisplayCancelableProgressBar("Working", "Copying scripts...", 0f);

        var dir = Tools.GetDirectoryFromPath(path);

        // Copy the scripts
        foreach (var c in components)
        {
            var fn = c + ".cs";
            var p  = Tools.FindFile(Application.dataPath, fn);
            if (!string.IsNullOrEmpty(p))
            {
                System.IO.File.Copy(p, System.IO.Path.Combine(dir, fn), true);
            }
        }

        EditorUtility.DisplayCancelableProgressBar("Working", "Creating a DataNode...", 0f);

        foreach (var pair in ComponentSerialization.referencedPrefabs)
        {
            pair.Value.CollectReferencedResources();
        }

        var data = ComponentSerialization.SerializeBundle();

        if (data != null && data.children.size > 0)
        {
            Save(data, path, type);
        }
        else
        {
            Debug.LogWarning("No assets found to serialize");
        }

        ComponentSerialization.ClearReferences();
        EditorUtility.ClearProgressBar();
    }