-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomShopLoader.cs
More file actions
89 lines (66 loc) · 2.21 KB
/
Copy pathCustomShopLoader.cs
File metadata and controls
89 lines (66 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using MiniJSON;
namespace CustomShops
{
public class CustomShopLoader : MonoBehaviour
{
public Main Main;
private List<Product> _products = new List<Product>();
private List<BuildableObject> _buildableObjects = new List<BuildableObject>();
public string Path;
public void LoadShop()
{
try
{
char dsc = System.IO.Path.DirectorySeparatorChar;
var dict = Json.Deserialize(File.ReadAllText(Path + @"/shop.json")) as Dictionary<string,object>;
UnityEngine.Debug.Log("invalid json:" + (dict == null));
using (WWW www = new WWW("file://" + Path + dsc + "assetbundle" + dsc + "shop"))
{
if(www.error != null)
throw new Exception("Failed to Load:" + www.error);
if(www.assetBundle == null)
{
throw new Exception("AssetBundle is null");
}
AssetBundle bundle = www.assetBundle;
foreach(KeyValuePair<string,object> pair in dict)
{
var option = pair.Value as Dictionary<string,object>;
GameObject asset = UnityEngine.Object.Instantiate(bundle.LoadAsset((string)option["model"])) as GameObject;
asset.SetActive(false);
new ShopSettingsDecorator().Decorate(asset,option,bundle);
new PriceDecorator((double)option["price"]).Decorate(asset,option,bundle);
new NameDecorator(pair.Key).Decorate(asset,option,bundle);
asset.GetComponent<BuildableObject>().dontSerialize = true;
asset.GetComponent<BuildableObject>().isPreview = true;
_buildableObjects.Add(asset.GetComponent<BuildableObject>());
foreach(GameObject productsObject in asset.GetComponent<ProductShop>().productGOs)
{
_products.Add(productsObject.GetComponent<Product>());
}
AssetManager.Instance.registerObject(asset.GetComponent<BuildableObject>());
}
bundle.Unload(false);
}
}
catch(Exception e) {
UnityEngine.Debug.LogException (e);
}
}
public void UnloadShops()
{
foreach (BuildableObject shops in _buildableObjects)
{
AssetManager.Instance.unregisterObject(shops);
}
foreach (Product product in _products)
{
AssetManager.Instance.unregisterObject(product);
}
}
}
}