Skip to content

Instantly share code, notes, and snippets.

@hiroki-o
Last active February 19, 2017 11:36
Show Gist options
  • Save hiroki-o/8d6e92364ba58169539ba82d21771a30 to your computer and use it in GitHub Desktop.
Save hiroki-o/8d6e92364ba58169539ba82d21771a30 to your computer and use it in GitHub Desktop.
Custom Loader Node script that uses TextAsset as source of assets to load
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;
[CustomNode("LoadByAssetList", 11)]
public class LoadByAssetList : Node {
[SerializeField] private TextAsset m_assetListText;
private string m_lastLoadedText;
private List<AssetReference> m_references;
private Vector2 m_scroll;
public override string ActiveStyle {
get {
return "flow node 0 on";
}
}
public override string InactiveStyle {
get {
return "flow node 0";
}
}
public override Model.NodeOutputSemantics NodeInputType {
get {
return Model.NodeOutputSemantics.None;
}
}
public override void Initialize(Model.NodeData data) {
data.AddDefaultOutputPoint();
}
public override Node Clone() {
var newNode = new LoadByAssetList();
newNode.m_assetListText = m_assetListText;
return newNode;
}
public override void OnInspectorGUI(NodeGUI node, AssetReferenceStreamManager streamManager, NodeGUIEditor editor, Action onValueChanged) {
EditorGUILayout.HelpBox("LoadByAssetList: Load assets from given list of asssets defined in assigned TextAsset", MessageType.Info);
editor.UpdateNodeName(node);
GUILayout.Space(10f);
using (new EditorGUILayout.VerticalScope(GUI.skin.box)) {
var newText = EditorGUILayout.ObjectField("Asset Paths", m_assetListText, typeof(TextAsset), false) as TextAsset;
if(newText != m_assetListText) {
using(new RecordUndoScope("Change Text Asset", node, true)) {
m_assetListText = newText;
m_references = null;
onValueChanged();
}
}
}
GUILayout.Space(20f);
if(m_lastLoadedText != null) {
GUILayout.Label("Loaded text", "BoldLabel");
using(var scrollScope = new EditorGUILayout.ScrollViewScope(m_scroll) ) {
m_scroll = scrollScope.scrollPosition;
GUILayout.TextArea(m_lastLoadedText);
}
}
}
public override void Prepare (BuildTarget target,
Model.NodeData node,
IEnumerable<PerformGraph.AssetGroups> incoming,
IEnumerable<Model.ConnectionData> connectionsToOutput,
PerformGraph.Output Output)
{
ValidateTextAssetPaths(
m_assetListText,
() => {
throw new NodeException(node.Name + ": Text Asset is not assigned.", node.Id);
}
);
ParseTextAndRefreshReferences(node);
Load(connectionsToOutput, Output);
}
private void ParseTextAndRefreshReferences(Model.NodeData node) {
var curText= m_assetListText.text;
if(m_lastLoadedText == curText) {
return;
}
var newReferences = new List<AssetReference>();
char[] splitters = new char[] {'\r','\n'};
var paths = curText.Split(splitters);
foreach(var p in paths) {
var path = p.Trim();
if(string.IsNullOrEmpty(path)) {
continue;
}
// handle comments
if(path[0] == '#') {
continue;
}
var guid = AssetDatabase.AssetPathToGUID(path);
if(guid == null) {
throw new NodeException(node.Name + ": Asset not found. Please fix issue in TextAsset: " + path, node.Id);
}
var r = AssetReferenceDatabase.GetReference(path);
if(r == null) {
throw new NodeException(node.Name + ": Failed to acquire asset reference(Did you set wrong path?). Please fix issue in TextAsset: " + path, node.Id);
}
newReferences.Add(r);
}
m_references = newReferences;
m_lastLoadedText = curText;
}
void Load ( IEnumerable<Model.ConnectionData> connectionsToOutput, PerformGraph.Output Output)
{
if(connectionsToOutput == null || Output == null) {
return;
}
var output = new Dictionary<string, List<AssetReference>> {
{"0", m_references}
};
var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
null : connectionsToOutput.First();
Output(dst, output);
}
public static void ValidateTextAssetPaths (TextAsset textAsset, Action NullOrEmpty) {
if (textAsset == null) NullOrEmpty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment