Skip to content

Instantly share code, notes, and snippets.

@hiroki-o
Last active February 25, 2017 09:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroki-o/bad235d324918aa6972e29b0d00bd7be to your computer and use it in GitHub Desktop.
Save hiroki-o/bad235d324918aa6972e29b0d00bd7be to your computer and use it in GitHub Desktop.
[ABGT 1.2] Find shared dependencies between bundles and automatically create new shared asset bundles
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.AssetBundles.GraphTool;
using Model=UnityEngine.AssetBundles.GraphTool.DataModel.Version2;
/**
ImportSetting is the class for apply specific setting to already imported files.
*/
[CustomNode("Extract Shared Assets", 100)]
public class ExtractSharedAssets : Node {
public override string ActiveStyle {
get {
return "flow node 5 on";
}
}
public override string InactiveStyle {
get {
return "flow node 5";
}
}
public override Model.NodeOutputSemantics NodeInputType {
get {
return Model.NodeOutputSemantics.AssetBundleConfigurations;
}
}
public override Model.NodeOutputSemantics NodeOutputType {
get {
return Model.NodeOutputSemantics.AssetBundleConfigurations;
}
}
public override void Initialize(Model.NodeData data) {
data.AddDefaultInputPoint();
data.AddDefaultOutputPoint();
}
public override Node Clone(Model.NodeData newData) {
var newNode = new ExtractSharedAssets();
newData.AddDefaultInputPoint();
newData.AddDefaultOutputPoint();
return newNode;
}
public override void OnInspectorGUI(NodeGUI node, AssetReferenceStreamManager streamManager, NodeGUIEditor editor, Action onValueChanged) {
EditorGUILayout.HelpBox("My Custom Node: Implement your own Inspector.", MessageType.Info);
editor.UpdateNodeName(node);
}
/**
* Prepare is called whenever graph needs update.
*/
public override void Prepare (BuildTarget target,
Model.NodeData node,
IEnumerable<PerformGraph.AssetGroups> incoming,
IEnumerable<Model.ConnectionData> connectionsToOutput,
PerformGraph.Output Output)
{
// Pass incoming assets straight to Output
if(Output != null) {
var destination = (connectionsToOutput == null || !connectionsToOutput.Any())?
null : connectionsToOutput.First();
if(incoming != null) {
var dependencyCollector = new Dictionary<string, List<string>>(); // [asset path:group name]
var sharedDependency = new Dictionary<string, List<AssetReference>>();
// build dependency map
foreach(var ag in incoming) {
foreach (var key in ag.assetGroups.Keys) {
var assets = ag.assetGroups[key];
foreach(var a in assets) {
var dependencies = AssetDatabase.GetDependencies(new string[] { a.importFrom } );
foreach(var d in dependencies) {
if(!dependencyCollector.ContainsKey(d)) {
dependencyCollector[d] = new List<string>();
}
dependencyCollector[d].Add(key);
dependencyCollector[d].Sort();
}
}
}
}
foreach(var entry in dependencyCollector) {
if(entry.Value.Count > 1) {
var key = string.Join("-", entry.Value.ToArray());
if(!sharedDependency.ContainsKey(key)) {
sharedDependency[key] = new List<AssetReference>();
}
sharedDependency[key].Add( AssetReference.CreateReference(entry.Key) );
}
}
if(sharedDependency.Keys.Count > 0) {
foreach(var ag in incoming) {
Output(destination, new Dictionary<string, List<AssetReference>>(ag.assetGroups));
}
Output(destination, sharedDependency);
} else {
foreach(var ag in incoming) {
Output(destination, ag.assetGroups);
}
}
} else {
// Overwrite output with empty Dictionary when no there is incoming asset
Output(destination, new Dictionary<string, List<AssetReference>>());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment