首次提交
This commit is contained in:
36
Assets/Plugins/FairyGUI/Examples/Bag/BagMain.cs
Normal file
36
Assets/Plugins/FairyGUI/Examples/Bag/BagMain.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
|
||||
/// <summary>
|
||||
/// A game bag demo, demonstrated how to customize loader to load icons not in the UI package.
|
||||
/// </summary>
|
||||
public class BagMain : MonoBehaviour
|
||||
{
|
||||
GComponent _mainView;
|
||||
BagWindow _bagWindow;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
//Register custom loader class
|
||||
UIObjectFactory.SetLoaderExtension(typeof(MyGLoader));
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Application.targetFrameRate = 60;
|
||||
Stage.inst.onKeyDown.Add(OnKeyDown);
|
||||
GRoot.inst.SetContentScaleFactor(1136, 640);
|
||||
_mainView = this.GetComponent<UIPanel>().ui;
|
||||
|
||||
_bagWindow = new BagWindow();
|
||||
_mainView.GetChild("bagBtn").onClick.Add(() => { _bagWindow.Show(); });
|
||||
}
|
||||
|
||||
void OnKeyDown(EventContext context)
|
||||
{
|
||||
if (context.inputEvent.keyCode == KeyCode.Escape)
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Plugins/FairyGUI/Examples/Bag/BagMain.cs.meta
Normal file
12
Assets/Plugins/FairyGUI/Examples/Bag/BagMain.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72adad54c37cc25499988e9b785d5b90
|
||||
timeCreated: 1446194671
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
49
Assets/Plugins/FairyGUI/Examples/Bag/BagWindow.cs
Normal file
49
Assets/Plugins/FairyGUI/Examples/Bag/BagWindow.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using FairyGUI;
|
||||
using UnityEngine;
|
||||
|
||||
public class BagWindow : Window
|
||||
{
|
||||
GList _list;
|
||||
|
||||
public BagWindow()
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnInit()
|
||||
{
|
||||
this.contentPane = UIPackage.CreateObject("Bag", "BagWin").asCom;
|
||||
this.Center();
|
||||
this.modal = true;
|
||||
|
||||
_list = this.contentPane.GetChild("list").asList;
|
||||
_list.onClickItem.Add(__clickItem);
|
||||
_list.itemRenderer = RenderListItem;
|
||||
_list.numItems = 45;
|
||||
}
|
||||
|
||||
void RenderListItem(int index, GObject obj)
|
||||
{
|
||||
GButton button = (GButton)obj;
|
||||
button.icon = "i" + UnityEngine.Random.Range(0, 10);
|
||||
button.title = "" + UnityEngine.Random.Range(0, 100);
|
||||
}
|
||||
|
||||
override protected void DoShowAnimation()
|
||||
{
|
||||
this.SetScale(0.1f, 0.1f);
|
||||
this.SetPivot(0.5f, 0.5f);
|
||||
this.TweenScale(new Vector2(1, 1), 0.3f).OnComplete(this.OnShown);
|
||||
}
|
||||
|
||||
override protected void DoHideAnimation()
|
||||
{
|
||||
this.TweenScale(new Vector2(0.1f, 0.1f), 0.3f).OnComplete(this.HideImmediately);
|
||||
}
|
||||
|
||||
void __clickItem(EventContext context)
|
||||
{
|
||||
GButton item = (GButton)context.data;
|
||||
this.contentPane.GetChild("n11").asLoader.url = item.icon;
|
||||
this.contentPane.GetChild("n13").text = item.icon;
|
||||
}
|
||||
}
|
||||
12
Assets/Plugins/FairyGUI/Examples/Bag/BagWindow.cs.meta
Normal file
12
Assets/Plugins/FairyGUI/Examples/Bag/BagWindow.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 418d0818f0209ee42938610bc6cb82be
|
||||
timeCreated: 1446193148
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
188
Assets/Plugins/FairyGUI/Examples/Bag/IconManager.cs
Normal file
188
Assets/Plugins/FairyGUI/Examples/Bag/IconManager.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using FairyGUI;
|
||||
#if UNITY_5_4_OR_NEWER
|
||||
using UnityEngine.Networking;
|
||||
#endif
|
||||
|
||||
public delegate void LoadCompleteCallback(NTexture texture);
|
||||
public delegate void LoadErrorCallback(string error);
|
||||
|
||||
/// <summary>
|
||||
/// Use to load icons from asset bundle, and pool them
|
||||
/// </summary>
|
||||
public class IconManager : MonoBehaviour
|
||||
{
|
||||
static IconManager _instance;
|
||||
public static IconManager inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject go = new GameObject("IconManager");
|
||||
DontDestroyOnLoad(go);
|
||||
_instance = go.AddComponent<IconManager>();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public const int POOL_CHECK_TIME = 30;
|
||||
public const int MAX_POOL_SIZE = 10;
|
||||
|
||||
List<LoadItem> _items;
|
||||
bool _started;
|
||||
Hashtable _pool;
|
||||
string _basePath;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_items = new List<LoadItem>();
|
||||
_pool = new Hashtable();
|
||||
_basePath = Application.streamingAssetsPath.Replace("\\", "/") + "/fairygui-examples/";
|
||||
if (Application.platform != RuntimePlatform.Android)
|
||||
_basePath = "file:///" + _basePath;
|
||||
|
||||
StartCoroutine(FreeIdleIcons());
|
||||
}
|
||||
|
||||
public void LoadIcon(string url,
|
||||
LoadCompleteCallback onSuccess,
|
||||
LoadErrorCallback onFail)
|
||||
{
|
||||
LoadItem item = new LoadItem();
|
||||
item.url = url;
|
||||
item.onSuccess = onSuccess;
|
||||
item.onFail = onFail;
|
||||
_items.Add(item);
|
||||
if (!_started)
|
||||
StartCoroutine(Run());
|
||||
}
|
||||
|
||||
IEnumerator Run()
|
||||
{
|
||||
_started = true;
|
||||
|
||||
LoadItem item = null;
|
||||
while (true)
|
||||
{
|
||||
if (_items.Count > 0)
|
||||
{
|
||||
item = _items[0];
|
||||
_items.RemoveAt(0);
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
||||
if (_pool.ContainsKey(item.url))
|
||||
{
|
||||
//Debug.Log("hit " + item.url);
|
||||
|
||||
NTexture texture = (NTexture)_pool[item.url];
|
||||
texture.refCount++;
|
||||
|
||||
if (item.onSuccess != null)
|
||||
item.onSuccess(texture);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
string url = _basePath + item.url + ".ab";
|
||||
#if UNITY_2017_2_OR_NEWER
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(url);
|
||||
#else
|
||||
UnityWebRequest www = UnityWebRequest.GetAssetBundle(url);
|
||||
#endif
|
||||
yield return www.SendWebRequest();
|
||||
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
if (www.result == UnityWebRequest.Result.Success)
|
||||
#else
|
||||
if (!www.isNetworkError && !www.isHttpError)
|
||||
#endif
|
||||
{
|
||||
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
|
||||
#else
|
||||
WWW www = new WWW(url);
|
||||
yield return www;
|
||||
|
||||
if (string.IsNullOrEmpty(www.error))
|
||||
{
|
||||
AssetBundle bundle = www.assetBundle;
|
||||
#endif
|
||||
|
||||
if (bundle == null)
|
||||
{
|
||||
Debug.LogWarning("Run Window->Build FairyGUI example Bundles first.");
|
||||
if (item.onFail != null)
|
||||
item.onFail(www.error);
|
||||
continue;
|
||||
}
|
||||
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
|
||||
NTexture texture = new NTexture(bundle.LoadAllAssets<Texture2D>()[0]);
|
||||
#else
|
||||
NTexture texture = new NTexture((Texture2D)bundle.mainAsset);
|
||||
#endif
|
||||
texture.refCount++;
|
||||
bundle.Unload(false);
|
||||
|
||||
_pool[item.url] = texture;
|
||||
|
||||
if (item.onSuccess != null)
|
||||
item.onSuccess(texture);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.onFail != null)
|
||||
item.onFail(www.error);
|
||||
}
|
||||
}
|
||||
|
||||
_started = false;
|
||||
}
|
||||
|
||||
IEnumerator FreeIdleIcons()
|
||||
{
|
||||
yield return new WaitForSeconds(POOL_CHECK_TIME); //check the pool every 30 seconds
|
||||
|
||||
int cnt = _pool.Count;
|
||||
if (cnt > MAX_POOL_SIZE)
|
||||
{
|
||||
ArrayList toRemove = null;
|
||||
foreach (DictionaryEntry de in _pool)
|
||||
{
|
||||
string key = (string)de.Key;
|
||||
NTexture texture = (NTexture)de.Value;
|
||||
if (texture.refCount == 0)
|
||||
{
|
||||
if (toRemove == null)
|
||||
toRemove = new ArrayList();
|
||||
toRemove.Add(key);
|
||||
texture.Dispose();
|
||||
|
||||
//Debug.Log("free icon " + de.Key);
|
||||
|
||||
cnt--;
|
||||
if (cnt <= 8)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (toRemove != null)
|
||||
{
|
||||
foreach (string key in toRemove)
|
||||
_pool.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class LoadItem
|
||||
{
|
||||
public string url;
|
||||
public LoadCompleteCallback onSuccess;
|
||||
public LoadErrorCallback onFail;
|
||||
}
|
||||
12
Assets/Plugins/FairyGUI/Examples/Bag/IconManager.cs.meta
Normal file
12
Assets/Plugins/FairyGUI/Examples/Bag/IconManager.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5725e4855896724d9bdeb29c68c6c92
|
||||
timeCreated: 1446193148
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
33
Assets/Plugins/FairyGUI/Examples/Bag/MyGLoader.cs
Normal file
33
Assets/Plugins/FairyGUI/Examples/Bag/MyGLoader.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
using System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Extend the ability of GLoader
|
||||
/// </summary>
|
||||
public class MyGLoader : GLoader
|
||||
{
|
||||
protected override void LoadExternal()
|
||||
{
|
||||
IconManager.inst.LoadIcon(this.url, OnLoadSuccess, OnLoadFail);
|
||||
}
|
||||
|
||||
protected override void FreeExternal(NTexture texture)
|
||||
{
|
||||
texture.refCount--;
|
||||
}
|
||||
|
||||
void OnLoadSuccess(NTexture texture)
|
||||
{
|
||||
if (string.IsNullOrEmpty(this.url))
|
||||
return;
|
||||
|
||||
this.onExternalLoadSuccess(texture);
|
||||
}
|
||||
|
||||
void OnLoadFail(string error)
|
||||
{
|
||||
Debug.Log("load " + this.url + " failed: " + error);
|
||||
this.onExternalLoadFailed();
|
||||
}
|
||||
}
|
||||
12
Assets/Plugins/FairyGUI/Examples/Bag/MyGLoader.cs.meta
Normal file
12
Assets/Plugins/FairyGUI/Examples/Bag/MyGLoader.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca2c5718cae50694a8291cc372ad458d
|
||||
timeCreated: 1446193148
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user