IEnumerator LoadAsyncRoutine <T>(GenericAssetAddress <T> assetAddress, System.Action <string, T> callback) where T : UnityEngine.Object { var v_request = assetAddress.LoadAssetAsync(); while (!v_request.isDone) { yield return(null); } if (callback != null) { callback(assetAddress.Name, v_request.asset as T); } if (Application.isPlaying) { GameObject.Destroy(gameObject); } else { GameObject.DestroyImmediate(gameObject); } }
public static void LoadAsync <T>(GenericAssetAddress <T> assetAddress, System.Action <string, T> callback) where T : UnityEngine.Object { var loader = new GameObject().AddComponent <ResourcesLoader>(); loader.StartCoroutine(loader.LoadAsyncRoutine <T>(assetAddress, callback)); }
// Draw the property inside the given rect public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { GenericAssetAddress <T> v_loader = AssetAddressEditorInternalUtils.GetFieldValue(property) as GenericAssetAddress <T>; if (v_loader == null) { base.OnGUI(position, property, label); } else { _validate = _validate || (v_loader.IsResources() && v_loader.Asset == null); //Pre-Load asset inside resources folder if (_validate) { _validate = true; v_loader.Validate(); } var v_asset = v_loader.Asset; var v_rect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight); var v_oldShowMixed = EditorGUI.showMixedValue; EditorGUI.showMixedValue = property.hasMultipleDifferentValues; var v_otherAsset = EditorGUI.ObjectField(v_rect, label, v_asset, typeof(T), true) as T; EditorGUI.showMixedValue = v_oldShowMixed; //Draw Minilabel to make easy to see if asset is inside resources folder if (!string.IsNullOrEmpty(v_loader.AssetPath)) { var v_size = 40; var v_miniRect = new Rect(v_rect.xMax - v_size - (15 * EditorGUI.indentLevel), v_rect.y, v_size, v_rect.height); var v_oldGuiColor = GUI.color; GUI.color = EditorGUIUtility.isProSkin ? new Color(0, 0.7f, 0) : new Color(0, 0.5f, 0); if (v_loader.KeepLoaded) { GUI.color = new Color(GUI.color.g, GUI.color.g, GUI.color.b); } EditorGUI.LabelField(v_miniRect, "res", EditorStyles.whiteMiniLabel); GUI.color = v_oldGuiColor; } //Commit Changes if (v_asset != v_otherAsset) { Undo.RecordObjects(property.serializedObject.targetObjects, "Changed Asset Address"); v_loader.Asset = v_otherAsset; v_loader.Validate(); AssetAddressEditorInternalUtils.SetFieldValue(property, v_loader); } //Draw foldOut internal values v_rect.x -= 3; EditorGUI.PropertyField(v_rect, property, new GUIContent(""), false); //property.isExpanded = EditorGUI.Foldout(v_rect, property.isExpanded, ""); v_rect.x += 3; //Enter self if (property.isExpanded) { var v_endProperty = property.GetEndProperty(); property.NextVisible(true); var v_oldGuiChanged = GUI.changed; //Draw Childrens v_rect = EditorGUI.IndentedRect(v_rect); do { if (SerializedProperty.EqualContents(property, v_endProperty)) { break; } //Prevent Draw Asset Again if (property.name.Equals("m_asset")) { continue; } var v_oldGuiEnabled = GUI.enabled; //Disable AutoManaged Fields if (property.name.Equals("m_assetPath")) { GUI.enabled = false; } if (property.name.Equals("m_keepLoaded") && string.IsNullOrEmpty(v_loader.AssetPath)) { continue; } v_rect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(v_rect, property, true); GUI.enabled = v_oldGuiEnabled; }while (property.NextVisible(false)); //Validate Name if (GUI.changed && !v_oldGuiChanged && v_loader.Name != null) { } } } }