Created
October 10, 2018 21:52
-
-
Save dmitry1100/b1aa92fdb9b08d31544df811e02901c6 to your computer and use it in GitHub Desktop.
TouchPreloadedAssets is a workaround for Unity Editor to load Preloaded Assets as Unity Player does
This file contains hidden or 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
using System.Reflection; | |
using UnityEditor; | |
namespace Fiftytwo | |
{ | |
[InitializeOnLoad] | |
public class TouchPreloadedAssets | |
{ | |
static TouchPreloadedAssets() | |
{ | |
GetPreloadedAssets(); | |
} | |
private static UnityEngine.Object[] GetPreloadedAssets() | |
{ | |
#if UNITY_2018_2_OR_NEWER | |
return PlayerSettings.GetPreloadedAssets(); | |
#else | |
var findProperty = typeof(PlayerSettings).GetMethod("FindProperty", | |
BindingFlags.NonPublic | BindingFlags.Static); | |
var preloadedAssetsProp = findProperty.Invoke(null, | |
new object[] { "preloadedAssets" }) as SerializedProperty; | |
var assets = new UnityEngine.Object[preloadedAssetsProp.arraySize]; | |
for (int i = assets.Length; --i >= 0;) | |
{ | |
var assetProp = preloadedAssetsProp.GetArrayElementAtIndex(i); | |
// Property access forces Unity Editor to load asset | |
assets[i] = assetProp.objectReferenceValue; | |
} | |
return assets; | |
#endif | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment