Skip to content

Instantly share code, notes, and snippets.

@hiroki-o
Created February 24, 2017 22:09
Show Gist options
  • Save hiroki-o/68edafd571100a82ff303daef04f8e4b to your computer and use it in GitHub Desktop.
Save hiroki-o/68edafd571100a82ff303daef04f8e4b to your computer and use it in GitHub Desktop.
[ABGT 1.2] Check if unwanted dependencies are included into asset bundle
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("Check Unwanted Assets", 100)]
public class CheckUnwantedAssetsInDependency : Node {
[SerializeField] private SerializableMultiTargetString m_path;
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) {
m_path = new SerializableMultiTargetString();
data.AddDefaultInputPoint();
data.AddDefaultOutputPoint();
}
public override Node Clone(Model.NodeData newData) {
var newNode = new CheckUnwantedAssetsInDependency();
newNode.m_path = new SerializableMultiTargetString(m_path);
newData.AddDefaultInputPoint();
newData.AddDefaultOutputPoint();
return newNode;
}
public override void OnInspectorGUI(NodeGUI node, AssetReferenceStreamManager streamManager, NodeGUIEditor editor, Action onValueChanged) {
EditorGUILayout.HelpBox("Check Unwanted Assets: Checks if unwanted assets are in dependency.", MessageType.Info);
editor.UpdateNodeName(node);
GUILayout.Space(10f);
//Show target configuration tab
editor.DrawPlatformSelector(node);
using (new EditorGUILayout.VerticalScope(GUI.skin.box)) {
// Draw Platform selector tab.
var disabledScope = editor.DrawOverrideTargetToggle(node, m_path.ContainsValueOf(editor.CurrentEditingGroup), (bool b) => {
using(new RecordUndoScope("Remove Target Platform Settings", node, true)) {
if(b) {
m_path[editor.CurrentEditingGroup] = m_path.DefaultValue;
} else {
m_path.Remove(editor.CurrentEditingGroup);
}
onValueChanged();
}
});
// Draw tab contents
using (disabledScope) {
var val = m_path[editor.CurrentEditingGroup];
var newValue = EditorGUILayout.TextField("Path:", val);
if (newValue != val) {
using(new RecordUndoScope("Path Change", node, true)){
m_path[editor.CurrentEditingGroup] = newValue;
onValueChanged();
}
}
}
}
}
/**
* 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 checkPath = m_path[target];
foreach(var ag in incoming) {
foreach (var assets in ag.assetGroups.Values) {
foreach(var a in assets) {
var dependencies = AssetDatabase.GetDependencies(new string[] { a.importFrom } );
foreach(var d in dependencies) {
if(!string.IsNullOrEmpty(checkPath) && d.Contains(checkPath)) {
throw new NodeException(node.Name + ":Unwanted dependency found '" + d
+ "' for asset:" + a.importFrom, node.Id);
}
}
}
}
Output(destination, ag.assetGroups);
}
} else {
// Overwrite output with empty Dictionary when no there is incoming asset
Output(destination, new Dictionary<string, List<AssetReference>>());
}
}
}
/**
* Build is called when Unity builds assets with AssetBundle Graph.
*/
public override void Build (BuildTarget target,
Model.NodeData nodeData,
IEnumerable<PerformGraph.AssetGroups> incoming,
IEnumerable<Model.ConnectionData> connectionsToOutput,
PerformGraph.Output outputFunc,
Action<Model.NodeData, string, float> progressFunc)
{
// Do nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment