/// <summary>
			/// Constructor for convenience
			/// </summary>
			internal LoadAsyncParams(string name, AssetLoaded loadedMethod) {
				ItemLoadedMethods.Add(loadedMethod);
				Tracker = new AssetTracker();
				Tracker.AssetName = name;
				Tracker.RefCount = 1;
				Tracker.Status = EAssetStatus.Loading;
			}
		/// <summary>
		/// Asyncronously loads the specified asset
		/// </summary>
		/// <param name="assetName">Name of asset to laod</param>
		/// <param name="itemLoadedMethod">Method to call once load is completed</param>
		/// <returns>AssetTracker of asset to be loaded. Allows to poll the asset status if desired</returns>
		public AssetTracker LoadAsync(string assetName, AssetLoaded itemLoadedMethod, AssetLoadedHandler Callback) {
			AssetTracker tracker = null;

			// Check if asset is already loaded
			if (mCache.ContainsKey(assetName)) {
				tracker = mCache[assetName];

				// Increment reference count
				tracker.RefCount++;

				// Call the specified item loaded method
				if (itemLoadedMethod != null)
					itemLoadedMethod(tracker.Asset);
			} else {
				if (mLoadThread == null) {
					// First time LoadAsync has been called so 
					// initialise thread, reset event and queue
					mLoadThread = new Thread(new ThreadStart(LoadingThreadWorker));
					mLoadItemsQueue = new Queue<LoadAsyncParams>();
					mLoadResetEvent = new AutoResetEvent(false);

					// Start thread. It will wait once queue is empty
					mLoadThread.Start();
				}

				// Create the async argument structure and enqueue it for async load.
				lock (mLoadItemsQueue) {
					// first check if this item is already enqueued
					Queue<LoadAsyncParams>.Enumerator enumer = mLoadItemsQueue.GetEnumerator();
					while (enumer.MoveNext()) {
						if (enumer.Current.Tracker.AssetName == assetName) {
							// Register the itemLoaded method
							enumer.Current.ItemLoadedMethods.Add(itemLoadedMethod);
							tracker = enumer.Current.Tracker;
							tracker.RefCount++;
							break;
						}
					}

					// Item not already queued for loading
					if (tracker == null) {
						LoadAsyncParams args = new LoadAsyncParams(assetName, itemLoadedMethod);
						tracker = args.Tracker;
						mLoadItemsQueue.Enqueue(args);
					}
				}

				// Tell loading thread to stop waiting
				mLoadResetEvent.Set();
			}

			tracker.OnAssetLoaded = Callback;

			// Return tracker. Allows async caller to poll loaded status
			return tracker;
		}
        private IEnumerator LoadAsset(string assetPath, string assetBundle, AssetLoaded callback)
        {
            if (string.IsNullOrEmpty(assetBundle)) {
                // Then we wanna load a prefab from resources instead.
                yield return StartCoroutine(LoadAssetFromResources(assetPath, callback));
            }
            else {
                if (assetManager.HasAssetBundle(assetBundle)) {
                    yield return StartCoroutine(LoadAssetFromBundle(assetPath, assetBundle, callback));
                }

                WWW www = new WWW("file://" + Application.streamingAssetsPath + "/" + assetBundle + ".bundle");
                yield return www;

                assetManager.AddAssetBundle(assetBundle, www.assetBundle);
                yield return StartCoroutine(LoadAssetFromBundle(assetPath, assetBundle, callback));
            }
        }
Exemple #4
0
        public override void Load(AssetLoaded <T> callback = null)
        {
            try
            {
                if (!waiting)
                {
                    Logger.Log(String.Format("Updating WebXMLFileAsset {0} from {1}", typeof(T).Name, url));
                    waiting = true;
                    webclient.DownloadStringCompleted += (object sender, System.Net.DownloadStringCompletedEventArgs e) =>
                    {
                        TaskDispatcher.QueueOnMainThread(() =>
                        {
                            using (StringReader reader = new StringReader(e.Result))
                            {
                                try
                                {
                                    T result = (T)serializer.Deserialize(reader);

                                    instance = result;

                                    if (callback != null)
                                    {
                                        callback(this);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError("Error retrieving WebXMLFileAsset: " + ex.Message);
                                    callback(this);
                                }
                            }
                            waiting = false;
                        });
                    };
                    webclient.DownloadStringAsync(url);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Failed to deserialize WebXMLFileAsset: {0}", url), ex);
            }
        }
        public override void Load(AssetLoaded <T> callback = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(file) && File.Exists(file))
                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        instance = (T)serializer.Deserialize(reader);
                    }
                }

                Save();

                if (callback != null)
                {
                    callback(this);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Failed to deserialize XMLFileAsset: {0}", file), ex);
            }
        }
Exemple #6
0
 public virtual void Load(AssetLoaded <T> callback = null)
 {
     callback(this);
 }
Exemple #7
0
 public WebXMLFileAsset(Uri url = null, XmlRootAttribute attr = null, AssetLoaded <T> callback = null)
 {
     serializer = new XmlSerializer(typeof(T), attr);
     this.url   = url;
     Load(callback);
 }
		/// <summary>
		/// Asyncronously loads the specified asset List
		/// </summary>
		/// <param name="assetNames">Names of assets to laod</param>
		/// <param name="itemLoadedMethod">Method to call once load is completed</param>
		/// <returns>AssetTracker List of assets to be loaded. Allows to poll the asset status if desired</returns>
		public List<AssetTracker> LoadAsync(List<string> assetNames, AssetLoaded itemLoadedMethod, AssetLoadedHandler Callback) {
			return new List<AssetTracker>(LoadAsync(assetNames.ToArray(), itemLoadedMethod, Callback));
		}
		/// <summary>
		/// Asyncronously loads the specified asset array
		/// </summary>
		/// <param name="assetNames">Names of assets to laod</param>
		/// <param name="itemLoadedMethod">Method to call once load is completed</param>
		/// <returns>AssetTracker array of assets to be loaded. Allows to poll the asset status if desired</returns>
		public AssetTracker[] LoadAsync(string[] assetNames, AssetLoaded itemLoadedMethod, AssetLoadedHandler Callback) {
			AssetTracker[] assets = new AssetTracker[assetNames.Length];
			for (int i = 0; i < assets.Length; i++)
				assets[i] = LoadAsync(assetNames[i], itemLoadedMethod, Callback);
			return assets;
		}
Exemple #10
0
 /// <summary>
 /// Asyncronously loads the specified asset List
 /// </summary>
 /// <param name="assetNames">Names of assets to laod</param>
 /// <param name="itemLoadedMethod">Method to call once load is completed</param>
 /// <returns>AssetTracker List of assets to be loaded. Allows to poll the asset status if desired</returns>
 public List <AssetTracker> LoadAsync(List <string> assetNames, AssetLoaded itemLoadedMethod, AssetLoadedHandler Callback)
 {
     return(new List <AssetTracker>(LoadAsync(assetNames.ToArray(), itemLoadedMethod, Callback)));
 }
Exemple #11
0
        /// <summary>
        /// Asyncronously loads the specified asset
        /// </summary>
        /// <typeparam name="T">Generic type parameter</typeparam>
        /// <param name="assetName">Name of asset to laod</param>
        /// <param name="itemLoadedMethod">Method to call once load is completed</param>
        /// <returns>AssetTracker of asset to be loaded. Allows
        /// users to poll the asset status if desired</returns>
        public AssetTracker LoadAsync <T>(string assetName, AssetLoaded itemLoadedMethod)
        {
            AssetTracker tracker = null;

            // Check if asset is already loaded
            if (loadedAssets.ContainsKey(assetName))
            {
                tracker = loadedAssets[assetName];

                // Increment reference count
                tracker.RefCount++;

                // Call the specified item loaded method
                if (itemLoadedMethod != null)
                {
                    itemLoadedMethod(tracker.Asset);
                }
            }
            else
            {
                if (loadThread == null)
                {
                    // First time LoadAsync has been called so
                    // initialise thread, reset event and queue
                    loadThread     = new Thread(new ThreadStart(LoadingThreadWorker));
                    loadItemsQueue = new Queue <LoadAsyncParams>();
                    loadResetEvent = new AutoResetEvent(false);

                    // Start thread. It will wait once queue is empty
                    loadThread.Start();
                }

                // Create the async argument structure and enqueue it for async load.
                lock (loadItemsQueue)
                {
                    // first check if this item is already enqueued
                    Queue <LoadAsyncParams> .Enumerator enumer = loadItemsQueue.GetEnumerator();
                    while (enumer.MoveNext())
                    {
                        if (enumer.Current.Tracker.AssetName == assetName)
                        {
                            // Register the itemLoaded method
                            enumer.Current.ItemLoadedMethods.Add(itemLoadedMethod);
                            tracker = enumer.Current.Tracker;
                            tracker.RefCount++;
                            break;
                        }
                    }

                    // Item not already queued for loading
                    if (tracker == null)
                    {
                        LoadAsyncParams args = new LoadAsyncParams(typeof(T), assetName, itemLoadedMethod);
                        tracker = args.Tracker;
                        loadItemsQueue.Enqueue(args);
                    }
                }

                // Tell loading thread to stop waiting
                loadResetEvent.Set();
            }

            // Return tracker. Allows async caller to poll loaded status
            return(tracker);
        }
 public WebXMLFileAsset(Uri url = null, XmlRootAttribute attr = null, AssetLoaded <T> callback = null)
 {
     this.url  = url;
     this.attr = attr;
     Load(callback);
 }
 private IEnumerator LoadAssetFromResources(string assetPath, AssetLoaded callback)
 {
     var request = Resources.LoadAsync(assetPath);
     yield return request;
     callback(request.asset);
 }
 private IEnumerator LoadAssetFromBundle(string assetPath, string assetBundle, AssetLoaded callback)
 {
     var request = assetManager.GetAssetBundle(assetBundle).LoadAssetAsync(assetPath);
     yield return request;
     callback(request.asset);
 }
Exemple #15
0
 public virtual void Load(AssetLoaded <T> callback = null, bool update = false)
 {
     callback(this);
 }
 /// <summary>
 /// Asyncronously loads the specified asset List
 /// </summary>
 /// <param name="filenames"></param>
 /// <param name="itemLoadedMethod">Method to call once load is completed</param>
 /// <param name="callback"></param>
 /// <returns>AssetTracker List of assets to be loaded. Allows to poll the asset status if desired</returns>
 public List <AssetTracker> LoadAsync(List <string> filenames, AssetLoaded itemLoadedMethod, AssetLoadedHandler callback)
 {
     return(mExtendedContentManager.LoadAsync(filenames, itemLoadedMethod, callback));
 }
 /// <summary>
 /// Asyncronously loads the specified asset array
 /// </summary>
 /// <param name="filenames"></param>
 /// <param name="itemLoadedMethod">Method to call once load is completed</param>
 /// <param name="callback"></param>
 /// <returns>AssetTracker array of assets to be loaded. Allows to poll the asset status if desired</returns>
 public AssetTracker[] LoadAsync(string[] filenames, AssetLoaded itemLoadedMethod, AssetLoadedHandler callback)
 {
     return(mExtendedContentManager.LoadAsync(filenames, itemLoadedMethod, callback));
 }
        private void View_ScriptNotify(object sender, NotifyEventArgs e)
        {
            var key   = e.Value.Substring(0, 4);
            var value = e.Value.Substring(4);

            switch (key)
            {
            // Loading assets
            case "LOAD":
            {
                AssetLoading?.Invoke(this, EventArgs.Empty);
                break;
            }

            // Assets loaded
            case "LODD":
            {
                _view.Opacity = 1;
                AssetLoaded?.Invoke(this, EventArgs.Empty);
                break;
            }

            // Camera alpha angle in radians
            case "ALPH":
            {
                AlphaInDegrees = GetDegreeFromRadian(double.Parse(value));
                break;
            }

            // Camera beta angle in radians
            case "BETA":
            {
                BetaInDegrees = GetDegreeFromRadian(double.Parse(value));
                break;
            }

            // Camera radius
            case "RADI":
            {
                RadiusPercentage = double.Parse(value);
                break;
            }

            // Animations are supported
            case "ANIM":
            {
                if (_commandGrid == null)
                {
                    return;
                }

                _commandGrid.Visibility = Visibility.Visible;

                var animationNames = value.Split(',');

                if (_animationList != null)
                {
                    _animationList.Items.Clear();

                    foreach (var animName in animationNames)
                    {
                        _animationList.Items.Add(animName);
                    }

                    _animationList.SelectedIndex = 0;

                    _playSymbol.Visibility  = Visibility.Collapsed;
                    _pauseSymbol.Visibility = Visibility.Visible;
                }

                break;
            }

            // No animation
            case "NANM":
            {
                if (_commandGrid != null)
                {
                    _commandGrid.Visibility = Visibility.Collapsed;
                }

                break;
            }

            // Current animation frame
            case "FRAM":
            {
                if (_animationSlider == null || !_sliderLayoutUpdated)
                {
                    return;
                }

                var position = Math.Min(1, Math.Max(0, double.Parse(value)));

                // Making sure we are not overwhelming the system
                _sliderLayoutUpdated       = false;
                _lastValueSetFromAnimation = position * 99.0;
                _animationSlider.Value     = _lastValueSetFromAnimation;
                break;
            }
            }
        }