浏览代码

fix async promise, to load image at first frame.

/main
kg 6 年前
当前提交
4e976a76
共有 9 个文件被更改,包括 88 次插入51 次删除
  1. 19
      Runtime/async/coroutine.cs
  2. 3
      Runtime/material/button_theme.cs
  3. 15
      Runtime/painting/image_provider.cs
  4. 40
      Runtime/painting/image_resolution.cs
  5. 12
      Runtime/promise/Promise.cs
  6. 6
      Runtime/promise/Promise_NonGeneric.cs
  7. 4
      Samples/UIWidgetsGallery/GalleryMain.cs
  8. 3
      Samples/UIWidgetsGallery/gallery/example_code_parser.cs
  9. 37
      Tests/Resources/ali_landscape.png.meta

19
Runtime/async/coroutine.cs


internal volatile Exception lastError;
internal bool isDone;
readonly Promise<object> _promise = new Promise<object>();
readonly Promise<object> _promise = new Promise<object>(isSync: true);
public IPromise<object> promise {
get { return this._promise; }

this._routine = routine;
this._window = window;
this._isBackground = isBackground;
if (isBackground && BackgroundCallbacks.getInstance() != null) {
this._unhook = BackgroundCallbacks.getInstance().addCallback(this._moveNext);

this._moveNext(true); // try to run the first enumeration in the current loop.
this._isBackground = isBackground;
this._moveNext(false);
}
void _moveNext(bool firstTime) {
D.assert(!this.isDone);
var lastError = this.lastError;

return;
}
bool hasNext;
bool hasNext = true;
hasNext = this._processIEnumeratorRecursive(this._routine);
if (firstTime) {
hasNext = this._routine.MoveNext();
}
if (hasNext) {
hasNext = this._processIEnumeratorRecursive(this._routine);
}
}
catch (Exception ex) {
this.stop(ex);

3
Runtime/material/button_theme.cs


return 0.0f;
}
if (button is OutlineButton)
if (button is OutlineButton) {
}
return 8.0f;
}

15
Runtime/painting/image_provider.cs


IEnumerator _loadAssetAsync(AssetBundleImageKey key) {
if (key.bundle == null) {
ResourceRequest request = Resources.LoadAsync(key.name);
yield return request;
yield return request.asset;
if (request.asset) {
yield return request.asset;
} else {
yield return request;
yield return request.asset;
}
yield return request;
yield return request.asset;
if (request.asset) {
yield return request.asset;
} else {
yield return request.asset;
}
}
}
}

40
Runtime/painting/image_resolution.cs


using System;
using System.Collections;
using Unity.UIWidgets.async;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using UnityEngine;

public readonly string assetName;
public readonly AssetBundle bundle;
protected override
IPromise<AssetBundleImageKey> obtainKey(ImageConfiguration configuration) {
protected override IPromise<AssetBundleImageKey> obtainKey(ImageConfiguration configuration) {
AssetImageConfiguration assetConfig = new AssetImageConfiguration(configuration, this.assetName);
AssetBundleImageKey key;
var cache = AssetBundleCache.instance.get(configuration.bundle);

AssetBundle chosenBundle = this.bundle ? this.bundle : configuration.bundle;
var devicePixelRatio = configuration.devicePixelRatio ?? Window.instance.devicePixelRatio;
var coroutine = Window.instance.startCoroutine(this._loadAssetAsync(chosenBundle, devicePixelRatio));
return coroutine.promise.Then(result => {
D.assert(result != null);
key = (AssetBundleImageKey) result;
cache[assetConfig] = key;
return key;
});
key = this._loadAsset(chosenBundle, devicePixelRatio);
cache[assetConfig] = key;
return Promise<AssetBundleImageKey>.Resolved(key);
IEnumerator _loadAssetAsync(AssetBundle bundle, float devicePixelRatio) {
AssetBundleImageKey _loadAsset(AssetBundle bundle, float devicePixelRatio) {
var upper = devicePixelRatio.ceil();
var upper = Mathf.Min(3, devicePixelRatio.ceil());
ResourceRequest request = Resources.LoadAsync(assetName);
yield return request;
asset = request.asset;
}
else {
AssetBundleRequest request = bundle.LoadAssetAsync(assetName);
yield return request;
asset = request.asset;
asset = Resources.Load(assetName);
} else {
asset = bundle.LoadAsset(assetName);
}
else {
} else {
yield return new AssetBundleImageKey(
return new AssetBundleImageKey(
yield break;
yield return new AssetBundleImageKey(
return new AssetBundleImageKey(
bundle,
this.assetName,
scale: 1.0f

12
Runtime/promise/Promise.cs


/// Handle errors for the promise.
/// </summary>
public IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected) {
var resultPromise = new Promise<PromisedT>();
var resultPromise = new Promise<PromisedT>(isSync: true);
resultPromise.WithName(this.Name);
Action<PromisedT> resolveHandler = v => resultPromise.Resolve(v);

// Otherwise there is now way to get the converted value to pass to the resulting promise.
// Argument.NotNull(() => onResolved);
var resultPromise = new Promise<ConvertedT>();
var resultPromise = new Promise<ConvertedT>(isSync: true);
resultPromise.WithName(this.Name);
Action<PromisedT> resolveHandler = v => {

var remainingCount = promisesArray.Length;
var results = new PromisedT[remainingCount];
var progress = new float[remainingCount];
var resultPromise = new Promise<IEnumerable<PromisedT>>();
var resultPromise = new Promise<IEnumerable<PromisedT>>(isSync: true);
resultPromise.WithName("All");
promisesArray.Each((promise, index) => {

);
}
var resultPromise = new Promise<PromisedT>();
var resultPromise = new Promise<PromisedT>(isSync: true);
resultPromise.WithName("Race");
var progress = new float[promisesArray.Length];

public static IPromise<PromisedT> Rejected(Exception ex) {
// Argument.NotNull(() => ex);
var promise = new Promise<PromisedT>();
var promise = new Promise<PromisedT>(isSync: true);
var promise = new Promise<PromisedT>();
var promise = new Promise<PromisedT>(isSync: true);
promise.WithName(this.Name);
this.Then(x => promise.Resolve(x));

6
Runtime/promise/Promise_NonGeneric.cs


// Otherwise there is now way to get the converted value to pass to the resulting promise.
// Argument.NotNull(() => onResolved);
var resultPromise = new Promise<ConvertedT>();
var resultPromise = new Promise<ConvertedT>(isSync: true);
resultPromise.WithName(this.Name);
Action resolveHandler = () => {

public static IPromise Rejected(Exception ex) {
// Argument.NotNull(() => ex);
var promise = new Promise();
var promise = new Promise(isSync: true);
var promise = new Promise();
var promise = new Promise(isSync: true);
Window.instance.run(duration, () => { promise.Resolve(); });
return promise;

4
Samples/UIWidgetsGallery/GalleryMain.cs


return new GalleryApp();
}
protected override void Start() {
base.Start();
protected override void OnEnable() {
base.OnEnable();
FontManager.instance.addFont(Resources.Load<Font>("MaterialIcons-Regular"));
FontManager.instance.addFont(Resources.Load<Font>("GalleryIcons"));
}

3
Samples/UIWidgetsGallery/gallery/example_code_parser.cs


Dictionary<string, string> _exampleCode;
public string getExampleCode(string tag, AssetBundle bundle) {
if (this._exampleCode == null)
if (this._exampleCode == null) {
}
return this._exampleCode.getOrDefault(tag);
}

37
Tests/Resources/ali_landscape.png.meta


TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}
serializedVersion: 9
serializedVersion: 7
mipmaps:
mipMapMode: 0
enableMipMap: 1

wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 0

platformSettings:
- serializedVersion: 2
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
- serializedVersion: 2
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1

正在加载...
取消
保存