强化界面的属性Item的逻辑以及金币钻石的逻辑都还没有完成,接下来继续。

Button加点逻辑

修改PropertyItem,添加InitButtonAction方法

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
public class PropertyItem : MonoBehaviour, IViewUpdate, IViewShow
{
//...
public void Init(string key)
{
_key = key;
_itemId++;
UpdatePos(_itemId);
InitButtonAction();//+++
}
private void InitButtonAction()//+++
{
transform.ButtonAction("Add", AddAction);
}
private void AddAction()//+++
{
string valueKey = GetPropertyKey(ItemKey.value);
float value = GetValueToFloat(valueKey);
string grouthKey = GetPropertyKey(ItemKey.grouth);
float grouth = GetValueToFloat(grouthKey);
value += grouth;

DataMgr.Instance.SetObject(valueKey, value);//注意这里,如果之前value是Int会被修改为Float,这是PlayerPrefs的机制
}
private string GetPropertyKey(ItemKey itemKey)
{
int planeId = GameStateModel.Instance.SelectedPlaneId;
return KeysUtil.GetPropertyKeys(planeId, _key + itemKey);
}
private float GetValueToFloat(string key)
{
string value = DataMgr.Instance.GetObject(key).ToString();
try
{
return float.Parse(value);

}
catch (System.Exception)
{
Debug.LogErrorFormat("属性值类型转换错误,无法转换成float,Key:{0},Value: {1}",key,value);
return 0.0f;
}
}
//...
}

PropertyItem并不继承ViewBase,所以不能使用UIUtil来获取子对象,这里使用一个包装好的拓展方法

使用GetValueToFloat来将所有从存档取出来的值转化为float,我们先将其包装成字符串,然后Parse成float。如果我们使用object强转float,即使是int型的object数据强转float也会报错,所以使用先转string再Parse的方法。

上面的代码仅供参考,我们没有必要将value都转为float,所以修改一下,还是使用int形式:

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
public void Init(string key)
{
//...
InitButtonAction();
}
private void InitButtonAction()
{
transform.ButtonAction("Add", AddAction);
}
private void AddAction()
{
string valueKey = GetPropertyKey(ItemKey.value);
int value = GetValue(valueKey);//改成int
string grouthKey = GetPropertyKey(ItemKey.grouth);
int grouth = GetValue(grouthKey);//改成int
value += grouth;

DataMgr.Instance.SetObject(valueKey, value);//注意这里,如果之前value是Int会被修改为Float,这是PlayerPrefs的机制
}
private string GetPropertyKey(ItemKey itemKey)
{
int planeId = GameStateModel.Instance.SelectedPlaneId;
return KeysUtil.GetPropertyKeys(planeId, _key + itemKey);
}
private int GetValue(string key)//修改
{
return DataMgr.Instance.Get<int>(key);
}

ExtendUtil

在其中添加快速给子UI按钮添加Listener的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void ButtonAction(this Transform target, string path, Action action)
{
var go = target.Find(path);
if (go != null)
{
var button = go.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(()=> action());
}
else
{
Debug.LogError("获取Button组件失败:" + path);
}
}
else
{
Debug.LogError("查找transform失败:" + path);
}
}

Slider填充逻辑

修改PropertyItem,添加UpdateSlider方法

1
2
3
4
5
6
7
8
9
10
11
12
13
public void UpdatePlaneId(int planeId)//注意这个Id是选择的战机的id,不是Item位置的id
{
UpdateData(planeId);
UpdateSlider();//+++
}
//...
private void UpdateSlider()
{
Slider slider = transform.Find("Slider").GetComponent<Slider>();
slider.minValue = 0.0f;
slider.maxValue = DataMgr.Instance.Get<int>(GetPropertyKey(ItemKey.maxValue));
slider.value = DataMgr.Instance.Get<int>(GetPropertyKey(ItemKey.value));//???
}