简单的单例+委托事件的例子.....

using UnityEngine;
using System.Collections;

public class ZRay : MonoBehaviour
{


    private static ZRay instance;

    public static ZRay Instance
    {
        get { if (instance == null)instance = GameObject.FindObjectOfType<ZRay>() as ZRay; return instance; }
    }
    public delegate void RAY(GameObject obj);
    public  event RAY rayEvent;
    

    Ray ray;
    RaycastHit hit;
    void Update()
    {
       ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       if (Physics.Raycast(ray, out hit))
       {
           rayEvent(hit.collider.gameObject);
       }
    }
}
using UnityEngine;
using System.Collections;

public class Do : MonoBehaviour
{



    void Start()
    {
        ZRay.Instance.rayEvent += dosth;
    }


    void dosth(GameObject obj)
    {
        Debug.Log(obj.name);
    }
}

 

posted @ 2016-06-16 14:03  哎呦不能错喔  阅读(302)  评论(0编辑  收藏  举报