Skip to content

Instantly share code, notes, and snippets.

@seldomU
Created October 26, 2016 21:54
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 seldomU/37e9d5a07f4272b73d233da61547fe8c to your computer and use it in GitHub Desktop.
Save seldomU/37e9d5a07f4272b73d233da61547fe8c to your computer and use it in GitHub Desktop.
Backend for AssetBundle Graph Tool
using UnityEngine;
using RelationsInspector.Backend;
using AssetBundleGraph;
using RelationsInspector;
using System.Collections.Generic;
using UnityEditor;
using System.Linq;
[AcceptTargets(typeof(SaveData))]
public class BundleBuildBackend : MinimalBackend<NodeData, string>
{
// colors
static readonly Color LightGreen = new Color( 0.25f, 1f, 0.25f, 1f );
static readonly Color Orange = new Color( 1f, 0.75f, 0.25f, 1f );
static readonly Color LightBlue = new Color( 0.25f, 0.5f, 1f, 1f );
static readonly Color LightRed = new Color( 1f, 0.25f, 0.25f, 1f );
static readonly Color LightGrey = new Color( 0.75f, 0.75f, 0.75f, 1f );
// mapping node kind to color
public static Dictionary<NodeKind, ColorLegendEntry> NodeKindColor = new Dictionary<NodeKind, ColorLegendEntry>()
{
{ NodeKind.LOADER_GUI, new ColorLegendEntry() {text = "Loader" , color = LightGrey } },
{ NodeKind.FILTER_GUI, new ColorLegendEntry() {text = "Filter" , color = LightBlue } },
{ NodeKind.IMPORTSETTING_GUI, new ColorLegendEntry() {text = "Importsetting" , color = Color.blue } },
{ NodeKind.MODIFIER_GUI, new ColorLegendEntry() {text = "Modifier" , color = Color.cyan } },
{ NodeKind.GROUPING_GUI, new ColorLegendEntry() { text = "Grouping" , color = LightGreen} },
{ NodeKind.PREFABBUILDER_GUI, new ColorLegendEntry() {text = "Prefab builder" , color = Color.yellow } },
{ NodeKind.BUNDLECONFIG_GUI, new ColorLegendEntry() {text = "Bundleconfig" , color = Orange } },
{ NodeKind.BUNDLEBUILDER_GUI, new ColorLegendEntry() {text = "Bundle builder" , color = LightRed } },
{ NodeKind.EXPORTER_GUI, new ColorLegendEntry() {text = "Exporter" , color = LightGrey } }
};
static ColorLegendEntry[] colorLegend = NodeKindColor.Values.ToArray();
// backend instance state
SaveData saveData;
private bool showLegend;
public override IEnumerable<NodeData> Init( object target )
{
saveData = target as SaveData;
if ( saveData == null )
yield break;
foreach ( NodeData data in saveData.Nodes )
yield return data;
}
public override IEnumerable<Relation<NodeData, string>> GetRelations( NodeData entity )
{
var leavingConnections = saveData.Connections.Where( c => c.FromNodeId == entity.Id );
foreach ( var leavingConnection in leavingConnections )
{
var other = saveData.Nodes.Find( n => n.Id == leavingConnection.ToNodeId );
yield return new Relation<NodeData, string>( entity, other, string.Empty );
}
var enteringConnections = saveData.Connections.Where( c => c.ToNodeId == entity.Id );
foreach ( var enteringConnection in enteringConnections )
{
var other = saveData.Nodes.Find( n => n.Id == enteringConnection.FromNodeId );
yield return new Relation<NodeData, string>( other, entity, string.Empty );
}
}
public override Rect OnGUI()
{
GUILayout.BeginHorizontal(EditorStyles.toolbar);
if ( GUILayout.Button( "Start", EditorStyles.toolbarButton ) )
{
var newSaveData = SaveData.LoadFromDisk();
api.ResetTargets( new object[] { newSaveData }, typeof(BundleBuildBackend) );
}
GUILayout.FlexibleSpace();
showLegend = GUILayout.Toggle( showLegend, "Legend", EditorStyles.toolbarButton, GUILayout.ExpandWidth( false ) );
if ( showLegend )
{
string title = "Relation types";
var boxSize = ColorLegendBox.GetSize( title, colorLegend );
float boxPosX = EditorGUIUtility.currentViewWidth - boxSize.x - 10;
float boxPosY = 42;
ColorLegendBox.Draw( new Rect( boxPosX, boxPosY, boxSize.x, boxSize.y ), title, colorLegend );
}
GUILayout.EndHorizontal();
return base.OnGUI();
}
public override void OnEntitySelectionChange( NodeData[] selection )
{
if ( selection.Length == 1 )
{
// spawn an inspectorhelper for the node and select it
var inspectorHelper = ScriptableObject.CreateInstance<NodeGUIInspectorHelper>();
inspectorHelper.hideFlags = HideFlags.DontSave;
inspectorHelper.UpdateNode( new NodeGUI( selection[ 0 ] ) );
Selection.activeObject = inspectorHelper;
}
base.OnEntitySelectionChange( selection );
}
public override Rect DrawContent( NodeData entity, EntityDrawContext drawContext )
{
drawContext.style.backgroundColor =
drawContext.style.targetBackgroundColor = GetNodeKindColor( entity.Kind );
return DrawUtil.DrawContent( new GUIContent( entity.Name ), drawContext );
//return base.DrawContent( entity, drawContext );
}
public override string GetEntityTooltip( NodeData entity )
{
return string.Empty;
}
public static Color GetNodeKindColor( NodeKind nodeKind )
{
ColorLegendEntry colorEntry;
bool kindColorExists = NodeKindColor.TryGetValue( nodeKind, out colorEntry );
return kindColorExists ? colorEntry.color : Color.grey;
}
}
@seldomU
Copy link
Author

seldomU commented Oct 26, 2016

Put this file into an editor folder. There should appear a new backend in RelationsInspector, called BundleBuildBackend. Select it and press its Start button. The AssetBundle Graph Tool has be installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment