Skip to content

Commit

Permalink
✨ feat(assets): Load text from assets on build
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Jan 20, 2023
1 parent 48cb65f commit a65eae9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Packages/com.nekometer.esnya.inari-udon/Assets.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions Packages/com.nekometer.esnya.inari-udon/Assets/MultiTextLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

using UnityEngine;
using TMPro;

#if UNITY_EDITOR
using System.Linq;
using UnityEditor;
using VRC.SDKBase.Editor.BuildPipeline;
#endif

namespace InariUdon
{
public class MultiTextLoader : MonoBehaviour
{
public string textPath;

#if UNITY_EDITOR
private void Load()
{
var textAssets = AssetDatabase
.FindAssets("t:TextAsset", new [] { textPath })
.Select(AssetDatabase.GUIDToAssetPath)
.Select(AssetDatabase.LoadAssetAtPath<TextAsset>)
.ToDictionary(t => t.name);
foreach (var tmp in GetComponentsInChildren<TMP_Text>(true))
{
var name = tmp.gameObject.name;
if (!textAssets.ContainsKey(name)) continue;

tmp.text = textAssets[name].text;
}

DestroyImmediate(this);
}

private static void LoadAllInScene()
{
foreach (var o in FindObjectsOfType(typeof(MultiTextLoader)))
{
(o as MultiTextLoader)?.Load();
}
}

[InitializeOnLoadMethod]
public static void Initialize()
{
EditorApplication.playModeStateChanged += (state) => {
if (state == PlayModeStateChange.EnteredPlayMode) LoadAllInScene();
};
}

public class BuildCallback : Editor, IVRCSDKBuildRequestedCallback
{
public int callbackOrder => 0;

public bool OnBuildRequested(VRCSDKRequestedBuildType requestedBuildType)
{
LoadAllInScene();
return true;
}
}
#endif
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a65eae9

Please sign in to comment.