You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.6 KiB
124 lines
4.6 KiB
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
using AX.NetworkSystem; |
|
using System.IO; |
|
|
|
public class MapItem : MonoBehaviour { |
|
|
|
public MapType mapType = new MapType(); |
|
|
|
private Toggle toggle; |
|
private GameObject disasterContent; |
|
private Texture2D texture; |
|
public GameObject disasterItem; |
|
// Use this for initialization |
|
void Start () { |
|
toggle = transform.Find("Dept").GetComponent<Toggle>(); |
|
toggle.onValueChanged.AddListener(RefreshDisasters); |
|
|
|
disasterContent = GameObject.Find("DisasterList/Body/Viewport/Content"); |
|
|
|
texture = (Texture2D)Resources.Load("Common/" + mapType.unit.ToString()); |
|
|
|
if (GameSettings.disasterSetting.mapType != null) |
|
{ |
|
toggle.isOn = true; |
|
} |
|
} |
|
|
|
private void OnDestroy() |
|
{ |
|
toggle.onValueChanged.RemoveListener(RefreshDisasters); |
|
} |
|
|
|
private void RefreshDisasters(bool flag) |
|
{ |
|
if (flag) |
|
{ |
|
if (GameSettings.othersSettings.mode == Mode.Practice) |
|
{//练习模式下才可选择自定义灾情 |
|
disasterContent.transform.Find("DisasterCustom").gameObject.SetActive(true); |
|
disasterContent.transform.Find("DisasterCustom").GetComponent<DisasterItem>().mapType = mapType; |
|
|
|
disasterContent.transform.Find("DisasterCustom/Toggle/Background").GetComponent<Image>().sprite |
|
= Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); |
|
} |
|
|
|
/* |
|
* 练习模式,演习模式,创建灾情库都需要 |
|
* TODO:向服务器请求所选中地图(单位)下的所有灾情的元数据 |
|
* 调用CreateDisaters()生成所有灾情的灾情项 |
|
*/ |
|
NetworkManager.Default.SendRequestAsync("GET_DISASTER_METADATA_REQUEST"); |
|
|
|
if (GameSettings.othersSettings.mode == Mode.DisasterManagement) |
|
{ |
|
GameObject.Find("CreateDisasterBtn").GetComponent<CreateDisasterBtn>().mapType = mapType; |
|
} |
|
} |
|
else |
|
{ |
|
disasterContent.transform.Find("DisasterCustom").gameObject.SetActive(false); |
|
|
|
/* |
|
* 练习模式,演习模式,创建灾情库都需要 |
|
* TODO:清空其他动态创建的灾情项,除自定义灾情项 |
|
*/ |
|
ClearDisaters(); |
|
|
|
if (GameSettings.othersSettings.mode == Mode.DisasterManagement) |
|
{ |
|
GameObject.Find("CreateDisasterBtn").GetComponent<CreateDisasterBtn>().mapType = null; |
|
} |
|
} |
|
} |
|
|
|
public void CreateDisaters(List<DisasterLibrary> disasterMetaData) |
|
{ |
|
for (int i = disasterMetaData.Count - 1; i >= 0; i--) |
|
{ |
|
GameObject obj = Instantiate(disasterItem, disasterContent.transform) as GameObject; |
|
|
|
obj.transform.Find("Toggle").GetComponent<Toggle>().group = disasterContent.GetComponent<ToggleGroup>(); |
|
|
|
if (File.Exists(Application.streamingAssetsPath + "/DisasterLibrary/" + disasterMetaData[i].Id + ".disaster")) |
|
{ |
|
obj.transform.Find("NotLoad").gameObject.SetActive(false); |
|
obj.transform.Find("Button").gameObject.SetActive(false); |
|
obj.GetComponent<DisasterItem>().myPath = Application.streamingAssetsPath + "/DisasterLibrary/" + disasterMetaData[i].Id + ".disaster"; |
|
obj.GetComponent<DisasterItem>().mapType = mapType; |
|
obj.GetComponent<DisasterItem>().disasterMetaDataId = disasterMetaData[i].Id; |
|
} |
|
else |
|
{ |
|
obj.GetComponent<DisasterItem>().mapType = mapType; |
|
obj.GetComponent<DisasterItem>().fullName = "DisasterLibrary/" + disasterMetaData[i].Id + ".disaster"; |
|
obj.GetComponent<DisasterItem>().disasterMetaDataId = disasterMetaData[i].Id; |
|
} |
|
|
|
obj.transform.Find("Toggle/Background").GetComponent<Image>().sprite |
|
= Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); |
|
|
|
obj.transform.Find("Text").GetComponent<Text>().text = "灾情名称:" + disasterMetaData[i].DisasterName; |
|
} |
|
} |
|
|
|
private void ClearDisaters() |
|
{ |
|
foreach (Transform item in disasterContent.transform) |
|
{ |
|
if (!item.name.Contains("Custom")) |
|
{ |
|
Destroy(item.gameObject); |
|
} |
|
} |
|
} |
|
|
|
// Update is called once per frame |
|
void Update () { |
|
|
|
} |
|
}
|
|
|