unity System.Action 回调是怎么做到的?因为做项目的时候需要用到,所以就找到了一下,找到了相关代码,仅供参考交流。
- using UnityEngine;
- namespace Liulala.Project
- {
- public class Data
- {
- public System.Action<int, int> OnDateChange;
- public int Date1 { get; private set; }
- public int Date2 { get; private set; }
- public void SetData(int date)
- {
- Date1 = date;
- Date2 = _dateStart + date;
- PlayerPrefs.SetInt(NewDate, Date2);
- OnDateChange(Date1, Date2);
- }
- public Data()
- {
- Date2 = PlayerPrefs.GetInt(NewDate);
- _dateStart = Date2;
- Date1 = 0;
- OnDateChange += (date1, date2) => { };
- }
- private int _dateStart;
- private static readonly string NewDate = "Date2";
- }
- }
- using Liulala.Project;
- using UnityEngine;
- publicclass Game : MonoBehaviour
- {
- privatevoid Start()
- {
- _Data = new Data();
- _Data.OnDateChange += ChangeView();
- }
- private void ChangeView()
- {
- }
- private Data _Data;
- }