初级
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CsharpProxy : MonoBehaviour {
// Use this for initialization
void Start () {
PayFine self = new NormalDriver("老王","1111232321311","123123123");
FriendDriver friendProxy = new FriendDriver("小王", "111166666311", "123166623");
//自己交罚款
self.Pay(200, 3);
//建立代理关系
friendProxy.realDriver = self;
friendProxy.Pay(200, 3);}
}public abstract class PayFine {
public string name;
/// <summary>
/// 违章者的身份证
/// </summary>
public string violatorID;
/// <summary>
/// 违章者驾驶证
/// </summary>
public string violatorDriverID;
protected PayFine(string name, string violatorID, string violatorDriverID)
{
this.name = name;
this.violatorID = violatorID;
this.violatorDriverID = violatorDriverID;
}/// <summary>
/// 支付罚款和扣除分数
/// </summary>
/// <param name="money"></param>
/// <param name="score"></param>
public abstract void Pay(float money,float score);
}
public class NormalDriver : PayFine
{
public NormalDriver(string name, string violatorID, string violatorDriverID) : base(name, violatorID, violatorDriverID)
{
}public override void Pay(float money, float score)
{
Debug.Log(string.Format("我是{0},身份证号{1},驾驶证号{2},今交罚款{3},扣除分数{4}",
name,violatorID,violatorDriverID,money,score));
}
}public class FriendDriver : PayFine
{
/// <summary>
/// 正真的驾驶员,如果没有,设置为null
/// </summary>
public PayFine realDriver;public FriendDriver(string name, string violatorID, string violatorDriverID) : base(name, violatorID, violatorDriverID)
{
}public override void Pay(float money, float score)
{
if (realDriver!=null)
{
Debug.Log("代理人"+name);
realDriver.Pay(money, score);
}
}
}
高级
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;public class UnityProxy : MonoBehaviour {
// Use this for initialization
void Start () {
gameObject.AddComponent<DelayProxy>();
//延时执行方法
//DelayProxy.instance.DelayCall(ShowTime,3);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) {
//获取方法
MethodInfo methodInfo = this.GetType().GetMethod("ShowCurrentTime");
//通过反射延时执行方法
DelayProxy.instance.DelayCallByReflection(methodInfo, this, new object[] { Time.time }, 5);
}
}
public void ShowTime() {
Debug.Log("Time:"+Time.time);
}
public void ShowCurrentTime(float crtTime) {
Debug.Log("启动延时时间"+crtTime);
Debug.Log("执行时间:"+Time.time);
}
}public class DelayProxy :MonoBehaviour{
public static DelayProxy instance;
private void Awake()
{
instance = this;
}
public void DelayCallByReflection(MethodInfo methodInfo,object obj,object[] parameters, float delayTime=3f ) {
StartCoroutine(IEDelayCallByReflection(methodInfo, obj, parameters, delayTime));
}
IEnumerator IEDelayCallByReflection(MethodInfo methodInfo, object obj, object[] parameters, float delayTime)
{
//等待几秒
yield return new WaitForSeconds(delayTime);
methodInfo.Invoke(obj,parameters);
}
public void DelayCall(System.Action action,float delayTime=3f) {
StartCoroutine(IEDelayCall(action,delayTime));
}
IEnumerator IEDelayCall(System.Action action, float delayTime) {
//等待几秒
yield return new WaitForSeconds(delayTime);
action();
}
}