http://blog.csdn.net/liulala16/article/details/8835012
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;
public class Game : MonoBehaviour
{
private void Start()
{
_Data = new Data();
_Data.OnDateChange += ChangeView();
}
private void ChangeView()
{
}
private Data _Data;
给c#添加SetTimeout和SetInterval函数(.NET Framework 3.5 System.Action命名空间)
http://blog.csdn.net/showrock/article/details/4648437
http://www.cnblogs.com/supers/articles/1120894.html
1.
Javascript中的SetTimeout和SetInterval函数很方便,把他们移植到c#中来。
调用示例:
执行效果:
2.
实现代码:
2 /// 在指定时间过后执行指定的表达式
3 /// </summary>
4 /// <param name="interval"> 事件之间经过的时间(以毫秒为单位) </param>
5 /// <param name="action"> 要执行的表达式 </param>
6 public static void SetTimeout( double interval, Action action)
7 {
8 System.Timers.Timer timer = new System.Timers.Timer(interval);
9 timer.Elapsed += delegate ( object sender, System.Timers.ElapsedEventArgs e)
10 {
11 timer.Enabled = false ;
12 action();
13 };
14 timer.Enabled = true ;
15 }
16 /// <summary>
17 /// 在指定时间周期重复执行指定的表达式
18 /// </summary>
19 /// <param name="interval"> 事件之间经过的时间(以毫秒为单位) </param>
20 /// <param name="action"> 要执行的表达式 </param>
21 public static void SetInterval( double interval, Action < ElapsedEventArgs > action)
22 {
23 System.Timers.Timer timer = new System.Timers.Timer(interval);
24 timer.Elapsed += delegate ( object sender, System.Timers.ElapsedEventArgs e)
25 {
26 action(e);
27 };
28 timer.Enabled = true ;
29 }
30
3.
由于System.Timers.Timer 是“基于服务器的 Timer 是为在多线程环境中用于辅助线程而设计的”,所以在winform中使用时如果要修改UI对象就要注意了,给个在winform中使用的例子:
运行效果:
http://bbs.csdn.net/topics/390039314
下例展示了 Action 泛型委托中的逆变支持带来的益处。 AddToContacts 方法获取 Person 类型的参数。 但是,您可以将此方法指派给 Action<Employee> 委托(在 Visual Basic 中为 (Action(Of Employee)),因为 Employee 继承 Person。
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
|
public
class
Person { }
public
class
Employee : Person { }
class
Program
{
static
void
AddToContacts(Person person)
{
// This method adds a Person object
// to a contact list.
}
static
void
Test()
{
// Create an instance of the delegate without using variance.
Action<Person> addPersonToContacts = AddToContacts;
// The Action delegate expects
// a method that has an Employee parameter,
// but you can assign it a method that has a Person parameter
// because Employee derives from Person.
Action<Employee> addEmployeeToContacts = AddToContacts;
// You can also assign a delegate
// that accepts a less derived parameter to a delegate
// that accepts a more derived parameter.
addEmployeeToContacts = addPersonToContacts;
}
}
|