Created
January 20, 2021 19:40
-
-
Save BigHandInSky/e9246384038642ca4b28d3c5ba051718 to your computer and use it in GitHub Desktop.
Ink Knot dropdown editor code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// maintenanceDay | |
// 2021010720:46 | |
using System.Collections; | |
using Sirenix.OdinInspector; | |
using Typewriter.Players; | |
using UnityEngine; | |
namespace _project.writing.Dev | |
{ | |
public class TriggerKnotOnEnable : MonoBehaviour | |
{ | |
[HideInInspector] | |
public string knot; | |
public float delay = 0.5f; | |
private IEnumerator Start() | |
{ | |
yield return new WaitForSeconds( delay ); | |
FindObjectOfType<StoryPlayer>().Trigger( knot ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// maintenanceDay.Editor | |
// 2021010720:47 | |
using System.Collections.Generic; | |
using _project.writing.Dev; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityIO.Classes; | |
using File = System.IO.File; | |
namespace _project | |
{ | |
[CustomEditor( typeof(TriggerKnotOnEnable) )] | |
public class TriggerKnotOnEnableEditor : Editor | |
{ | |
private TriggerKnotOnEnable _component; | |
private Dictionary<string, List<string>> _fileToKnotsLookup = new Dictionary<string, List<string>>(); | |
public override void OnInspectorGUI() | |
{ | |
if(!_component) | |
_component = target as TriggerKnotOnEnable; | |
base.OnInspectorGUI(); | |
if ( _fileToKnotsLookup.Count == 0 ) | |
{ | |
FetchKnots(); | |
} | |
if ( GUILayout.Button( $"Select Knot to trigger\n(Currently \"{_component.knot}\")" ) ) | |
{ | |
FetchKnots(); | |
ConstructThenShowMenu(); | |
} | |
} | |
private void FetchKnots() | |
{ | |
_fileToKnotsLookup.Clear(); | |
var guids = AssetDatabase.FindAssets( "", new []{ "Assets/_content/writing" } ); | |
foreach ( var guid in guids ) | |
{ | |
var path = AssetDatabase.GUIDToAssetPath( guid ); | |
if ( path.EndsWith( ".ink" ) ) | |
{ | |
var splits = path.Split( '/' ); | |
var knots = LoadInkThenParse( path ); | |
//Debug.Log( $"path? {path}, split: {splits[splits.Length - 1]}" ); | |
//Debug.Log( $"knots: [{string.Join( ",", knots )}]" ); | |
_fileToKnotsLookup.Add( splits[splits.Length - 1], knots ); | |
} | |
// ignore .jsons | |
} | |
} | |
private List<string> LoadInkThenParse( string path ) | |
{ | |
List<string> value = new List<string>(); | |
using ( var reader = File.OpenText( path ) ) | |
{ | |
var line = ""; | |
while ( !reader.EndOfStream ) | |
{ | |
line = reader.ReadLine(); | |
if ( line.StartsWith( "==" ) ) | |
{ | |
line = line.Replace( "==", "" ); | |
line = line.Trim(); | |
value.Add( line ); | |
} | |
} | |
} | |
return value; | |
} | |
private void ConstructThenShowMenu() | |
{ | |
GenericMenu menu = new GenericMenu(); | |
foreach ( var pair in _fileToKnotsLookup ) | |
{ | |
string root = pair.Key; | |
foreach ( var s in pair.Value ) | |
{ | |
AddMenuItemForKnot( menu, $"{root}/{s}", s ); | |
} | |
} | |
menu.ShowAsContext(); | |
} | |
void AddMenuItemForKnot(GenericMenu menu, string menuPath, string knot) | |
{ | |
menu.AddItem(new GUIContent(menuPath), _component.knot.Equals(knot), OnStringSelected, knot); | |
} | |
void OnStringSelected(object knot) | |
{ | |
Undo.RecordObject( _component, "knot change" ); | |
_component.knot = (string)knot; | |
EditorUtility.SetDirty( _component ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment