chunk 2: remaining non-audio non-NewImport assets
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
// Copyright 2018 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Google.Play.Common.LoadingScreen
|
||||
{
|
||||
/// <summary>
|
||||
/// A loading bar that can be used to display download progress to the user.
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
public class LoadingBar : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The outline width.
|
||||
/// </summary>
|
||||
public float OutlineWidth = 6f;
|
||||
|
||||
/// <summary>
|
||||
/// The inner border width.
|
||||
/// </summary>
|
||||
public float InnerBorderWidth = 6f;
|
||||
|
||||
/// <summary>
|
||||
/// If true, the <see cref="Outline"/> and <see cref="Background"/> RectTransforms will update to match the
|
||||
/// outline and border width.
|
||||
/// </summary>
|
||||
[Tooltip(
|
||||
"If true, the Outline and Background RectTransforms will update to match the outline and border width.")]
|
||||
public bool ResizeAutomatically = true;
|
||||
|
||||
/// <summary>
|
||||
/// AssetBundle download and install progress. The value set in the Editor is ignored at runtime.
|
||||
/// </summary>
|
||||
[Tooltip("AssetBundle download and install progress. The value set in the Editor is ignored at runtime.")]
|
||||
[Range(0f, 1f)]
|
||||
public float Progress = 0.25f;
|
||||
|
||||
/// <summary>
|
||||
/// The loading bar background.
|
||||
/// </summary>
|
||||
public RectTransform Background;
|
||||
|
||||
/// <summary>
|
||||
/// The loading bar outline.
|
||||
/// </summary>
|
||||
public RectTransform Outline;
|
||||
|
||||
/// <summary>
|
||||
/// The loading bar progress holder.
|
||||
/// </summary>
|
||||
public RectTransform ProgressHolder;
|
||||
|
||||
/// <summary>
|
||||
/// The loading bar progress fill.
|
||||
/// </summary>
|
||||
public RectTransform ProgressFill;
|
||||
|
||||
/// <summary>
|
||||
/// Proportion of the loading bar allocated to AssetBundle download progress.
|
||||
/// The remainder of the loading bar is allocated to install progress.
|
||||
/// </summary>
|
||||
[Tooltip("Proportion of the loading bar allocated to AssetBundle download progress. " +
|
||||
"The remainder of the loading bar is allocated to install progress.")]
|
||||
[Range(0f, 1f)]
|
||||
public float AssetBundleDownloadToInstallRatio = 0.8f;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (ResizeAutomatically)
|
||||
{
|
||||
ApplyBorderWidth();
|
||||
SetProgress(Progress);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the border width to the loading bar.
|
||||
/// </summary>
|
||||
public void ApplyBorderWidth()
|
||||
{
|
||||
Outline.anchorMin = Vector3.zero;
|
||||
Outline.anchorMax = Vector3.one;
|
||||
Outline.sizeDelta = Vector2.one * (OutlineWidth + InnerBorderWidth);
|
||||
|
||||
Background.anchorMin = Vector3.zero;
|
||||
Background.anchorMax = Vector3.one;
|
||||
Background.sizeDelta = Vector2.one * (InnerBorderWidth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the loading bar progress to the specified value in the range [0,1].
|
||||
/// </summary>
|
||||
/// <param name="proportionOfLoadingBar">The loading bar's current progress in the range [0,1].</param>
|
||||
public void SetProgress(float proportionOfLoadingBar)
|
||||
{
|
||||
Progress = proportionOfLoadingBar;
|
||||
if (ProgressFill != null)
|
||||
{
|
||||
ProgressFill.anchorMax = new Vector2(proportionOfLoadingBar, ProgressFill.anchorMax.y);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a loading bar by the progress made by an asynchronous operation. The bar will interpolate between
|
||||
/// <see cref="startingFillProportion"/> and <see cref="endingFillProportion"/> as the operation progresses.
|
||||
/// </summary>
|
||||
/// <param name="operation">The <see cref="AsyncOperation"/> indicating progress.</param>
|
||||
/// <param name="startingFillProportion">The starting position from which to fill.</param>
|
||||
/// <param name="endingFillProportion">The ending position to fill to.</param>
|
||||
/// <param name="skipFinalUpdate">
|
||||
/// If true, the bar will only fill before the operation has finished.
|
||||
/// This is useful in cases where an async operation will set its progress to 1, even when it has failed.
|
||||
/// </param>
|
||||
public IEnumerator FillUntilDone(AsyncOperation operation, float startingFillProportion,
|
||||
float endingFillProportion, bool skipFinalUpdate)
|
||||
{
|
||||
var previousFillProportion = startingFillProportion;
|
||||
var isDone = false;
|
||||
while (!isDone)
|
||||
{
|
||||
if (operation.isDone)
|
||||
{
|
||||
isDone = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var fillProportion = Mathf.Lerp(startingFillProportion, endingFillProportion, operation.progress);
|
||||
fillProportion = Mathf.Max(previousFillProportion, fillProportion); // Progress can only increase.
|
||||
SetProgress(fillProportion);
|
||||
previousFillProportion = fillProportion;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
if (skipFinalUpdate)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
var finalFillProportion = Mathf.Lerp(startingFillProportion, endingFillProportion, operation.progress);
|
||||
finalFillProportion = Mathf.Max(previousFillProportion, finalFillProportion);
|
||||
SetProgress(finalFillProportion);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fcbdd4679cf6e41568c661c1c762b33d
|
||||
timeCreated: 1539016701
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,169 @@
|
||||
// Copyright 2018 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Google.Play.Common.LoadingScreen
|
||||
{
|
||||
/// <summary>
|
||||
/// Downloads the AssetBundle available at AssetBundleUrl and updates LoadingBar with its progress.
|
||||
/// </summary>
|
||||
public class LoadingScreen : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The URL used to fetch the AssetBundle on <see cref="Start"/>.
|
||||
/// </summary>
|
||||
[Tooltip("The URL used to fetch the AssetBundle on Start.")]
|
||||
public string AssetBundleUrl;
|
||||
|
||||
/// <summary>
|
||||
/// The LoadingBar used to indicated download and install progress.
|
||||
/// </summary>
|
||||
[Tooltip("The LoadingBar used to indicated download and install progress.")]
|
||||
public LoadingBar LoadingBar;
|
||||
|
||||
/// <summary>
|
||||
/// The button displayed when a download error occurs. Should call ButtonEventRetryDownload in its onClick()
|
||||
/// event.
|
||||
/// </summary>
|
||||
[Tooltip("The button displayed when a download error occurs. " +
|
||||
"Should call ButtonEventRetryDownload in its onClick() event.")]
|
||||
public Button RetryButton;
|
||||
|
||||
// Number of attempts before we show the user a retry button.
|
||||
private const int InitialAttemptCount = 3;
|
||||
private AssetBundle _bundle;
|
||||
private int _assetBundleRetrievalAttemptCount;
|
||||
private float _maxLoadingBarProgress;
|
||||
private bool _downloading;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
AttemptAssetBundleDownload(InitialAttemptCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to retry the AssetBundle download one time.
|
||||
/// </summary>
|
||||
public void ButtonEventRetryDownload()
|
||||
{
|
||||
AttemptAssetBundleDownload(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to download the AssetBundle available at AssetBundleUrl.
|
||||
/// If it fails numberOfAttempts times, then it will display a retry button.
|
||||
/// </summary>
|
||||
private void AttemptAssetBundleDownload(int numberOfAttempts)
|
||||
{
|
||||
if (_downloading)
|
||||
{
|
||||
Debug.Log("Download attempt ignored because a download is already in progress.");
|
||||
return;
|
||||
}
|
||||
|
||||
HideRetryButton();
|
||||
_maxLoadingBarProgress = 0f;
|
||||
StartCoroutine(AttemptAssetBundleDownloadsCo(numberOfAttempts));
|
||||
}
|
||||
|
||||
private IEnumerator AttemptAssetBundleDownloadsCo(int numberOfAttempts)
|
||||
{
|
||||
_downloading = true;
|
||||
|
||||
for (var i = 0; i < numberOfAttempts; i++)
|
||||
{
|
||||
_assetBundleRetrievalAttemptCount++;
|
||||
Debug.LogFormat("Attempt #{0} at downloading AssetBundle...", _assetBundleRetrievalAttemptCount);
|
||||
|
||||
yield return GetAssetBundle(AssetBundleUrl);
|
||||
|
||||
if (_bundle != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
|
||||
if (_bundle == null)
|
||||
{
|
||||
ShowRetryButton();
|
||||
_downloading = false;
|
||||
yield break;
|
||||
}
|
||||
|
||||
var sceneLoadOperation = SceneManager.LoadSceneAsync(_bundle.GetAllScenePaths()[0]);
|
||||
var installStartFill = Mathf.Max(LoadingBar.AssetBundleDownloadToInstallRatio, _maxLoadingBarProgress);
|
||||
yield return LoadingBar.FillUntilDone(sceneLoadOperation, installStartFill, 1f, false);
|
||||
|
||||
_downloading = false;
|
||||
}
|
||||
|
||||
private IEnumerator GetAssetBundle(string assetBundleUrl)
|
||||
{
|
||||
UnityWebRequest webRequest;
|
||||
var downloadOperation = StartAssetBundleDownload(assetBundleUrl, out webRequest);
|
||||
|
||||
yield return LoadingBar.FillUntilDone(downloadOperation,
|
||||
_maxLoadingBarProgress, LoadingBar.AssetBundleDownloadToInstallRatio, true);
|
||||
|
||||
if (IsNetworkError(webRequest))
|
||||
{
|
||||
_maxLoadingBarProgress = LoadingBar.Progress;
|
||||
Debug.LogFormat("Failed to download AssetBundle: {0}", webRequest.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
_bundle = DownloadHandlerAssetBundle.GetContent(webRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowRetryButton()
|
||||
{
|
||||
LoadingBar.gameObject.SetActive(false);
|
||||
RetryButton.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void HideRetryButton()
|
||||
{
|
||||
LoadingBar.gameObject.SetActive(true);
|
||||
RetryButton.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private static bool IsNetworkError(UnityWebRequest request)
|
||||
{
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
return request.result == UnityWebRequest.Result.ConnectionError ||
|
||||
request.result == UnityWebRequest.Result.ProtocolError;
|
||||
#else
|
||||
return request.isHttpError || request.isNetworkError;
|
||||
#endif
|
||||
}
|
||||
|
||||
private static AsyncOperation StartAssetBundleDownload(string assetBundleUrl, out UnityWebRequest webRequest)
|
||||
{
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
webRequest = UnityWebRequestAssetBundle.GetAssetBundle(assetBundleUrl);
|
||||
#else
|
||||
webRequest = UnityWebRequest.GetAssetBundle(assetBundleUrl);
|
||||
#endif
|
||||
return webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 387db7af181f74e18ae5ac6796940d73
|
||||
timeCreated: 1539016701
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright 2018 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Google.Play.Common.LoadingScreen
|
||||
{
|
||||
/// <summary>
|
||||
/// A UI component that scrolls a tiled texture horizontally.
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(RawImage))]
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
public class ScrollingFillAnimator : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// How fast the fill texture will scroll in units per second.
|
||||
/// </summary>
|
||||
[Tooltip("How fast the fill texture will scroll in units per second.")]
|
||||
public float ScrollSpeed = 2.5f;
|
||||
|
||||
private RawImage _image;
|
||||
private RectTransform _rectTransform;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
LazyInit();
|
||||
|
||||
var uvRect = _image.uvRect;
|
||||
uvRect = ScrollUvs(uvRect);
|
||||
uvRect = ScaleUvs(uvRect);
|
||||
_image.uvRect = uvRect;
|
||||
}
|
||||
|
||||
// Move the uvs to the left so that the texture appears to scroll to the right.
|
||||
private Rect ScrollUvs(Rect uvRect)
|
||||
{
|
||||
// Don't scroll the uvs in edit mode, because Update is called too intermittently to be a useful preview.
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
uvRect.x -= ScrollSpeed * Time.deltaTime;
|
||||
}
|
||||
return uvRect;
|
||||
}
|
||||
|
||||
// Scale the uv rect so that the texture tiles across its rect instead of scaling with it.
|
||||
private Rect ScaleUvs(Rect uvRect)
|
||||
{
|
||||
if (_image.texture == null)
|
||||
{
|
||||
return uvRect;
|
||||
}
|
||||
|
||||
uvRect.size = new Vector2(_rectTransform.rect.width / _image.texture.width,
|
||||
_rectTransform.rect.height / _image.texture.height);
|
||||
return uvRect;
|
||||
}
|
||||
|
||||
private void LazyInit()
|
||||
{
|
||||
if (_rectTransform == null)
|
||||
{
|
||||
_rectTransform = GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
if (_image == null)
|
||||
{
|
||||
_image = GetComponent<RawImage>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 781257c89d9b2471490603c8f2d3c8bf
|
||||
timeCreated: 1540231614
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user