您的位置:首页 > 移动开发 > Unity3D

Unity3D通过委托 一个类访问到另一个类中的方法

2017-03-14 14:18 441 查看
本文固定链接:http://blog.csdn.net/u013108312/article/details/62042387

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class class1 : MonoBehaviour {

public static class1 Instance
{
private set;
get;
}

void Awake()
{
Instance = this;
}

void OnDestroy()
{
if (Instance != null)
{
Instance = null;
}
}

public delegate void FunVoid();
public delegate void FunString(string str);

public FunVoid delFun;
public FunString delFunString;

// Use this for initialization
void Start () {

delFun();
delFunString("11111");
}

// Update is called once per frame
void Update () {

}
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class class2 : MonoBehaviour {

public static class2 Instance
{
private set;
get;
}

void Awake()
{
Instance = this;
}

void Start()
{
class1.Instance.delFun += class2Fun;
class1.Instance.delFunString += class2FunString;
}

void OnDestroy()
{
if (Instance != null)
{
Instance = null;
}
class1.Instance.delFun -= class2Fun;
class1.Instance.delFunString -= class2FunString;
}
// Use this for initialization

// Update is called once per frame
void Update () {

}

void class2Fun()
{
Debug.Log("class2Fun()");
}

void class2FunString(string str)
{
Debug.Log("class2FunString()");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐