工具: 序列化工具 ; xml序列化和反序列化;检测射线是否碰到UI

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

using System.Xml.Serialization;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Tools
{

    /// <summary>
    /// 序列化任意一个对象,但是要注意,这个对象本身是可序列化的 
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static byte[] ObjectionToBytes(object obj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            return ms.GetBuffer();
        }
    }
    /// <summary>
    /// 反序列化与上一个函数对应
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static object BytesToObject(byte[] bytes)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            return formatter.Deserialize(ms);
        }
    }

    //-------------------------------------------------

    /// <summary>
    /// XML 序列化和反序列化
    /// </summary>
    static string fileName;
    public static void SavaData<T>(T savaData) where T : new()
    {
        //路径
        fileName = Application.dataPath + "/" + savaData.GetType().ToString();
        Stream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(stream, Encoding.UTF8);
        XmlSerializer xmlSerializer = new XmlSerializer(savaData.GetType());
        xmlSerializer.Serialize(sw, savaData);
        sw.Close();
        stream.Close();
    }

    public static T LoadData<T>() where T : new()
    {
        T saveData = new T();
        fileName = Application.dataPath + "/" + saveData.GetType().ToString();
        Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);

        //是否自动检测编码方式
        StreamReader sr = new StreamReader(stream, true);
        XmlSerializer xmlSerializer = new XmlSerializer(saveData.GetType());
        saveData = (T)xmlSerializer.Deserialize(sr);
        stream.Close();
        sr.Close();
        return saveData;

    }


    //----------------------------------------------

    //检测射线是否碰到UI

    public static bool UIRayCastCheck()
    {
        //事件系统
        EventSystem es = GameObject.FindObjectOfType<EventSystem>();
        //构造一个点击事件
        PointerEventData m_ped = new PointerEventData(es);
        //给事件 赋值位置信息
        m_ped.pressPosition = Input.mousePosition;
        m_ped.position = Input.mousePosition;
        //获取Canvas身上的发射射线组件
        GraphicRaycaster m_gr = GameObject.FindObjectOfType<GraphicRaycaster>();
        List<RaycastResult> list = new List<RaycastResult>();

        //发射向这个事件中发射射线,存放在list中
        m_gr.Raycast(m_ped, list);
        return list.Count > 0;
    }
    public static bool UIRayCastCheck(Canvas canvas)
    {
        //事件系统
        EventSystem es = GameObject.FindObjectOfType<EventSystem>();
        //构造一个点击事件
        PointerEventData m_ped = new PointerEventData(es);
        //给事件 赋值位置信息
        m_ped.pressPosition = Input.mousePosition;
        m_ped.position = Input.mousePosition;
        //获取Canvas身上的发射射线组件
        GraphicRaycaster m_gr = canvas.GetComponent<GraphicRaycaster>();
        List<RaycastResult> list = new List<RaycastResult>();

        //发射向这个事件中发射射线,存放在list中
        m_gr.Raycast(m_ped, list);
        return list.Count > 0;
    }

}


猜你喜欢

转载自blog.csdn.net/yuan_bowen/article/details/80572032