unity中将3D场景中的3DUI界面转换为2DUI界面(方法一)

此方法是改变3D场景中3DUI界面的父类,实现点击3DUI界面呈现在屏幕上。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

public class MachineInfoUICon : MonoBehaviour
{
    private EventTrigger m_eventTrigger;
    private GameObject m_machineInfoCanvas;
    private GameObject m_machineInfoParent;
    private GameObject m_machineInfoPanel;

    private GameObject m_UIObj;

    void Start()
    {

        m_machineInfoCanvas = GameObject.Find("MachineInfoCanvas");
        m_machineInfoParent = m_machineInfoCanvas.transform.FindChild("machineInfoParent").gameObject;
        m_machineInfoPanel = m_machineInfoCanvas.transform.FindChild("machineInfoPanel").gameObject;
        m_machineInfoPanel.SetActive(false);

        //效果1时下面一行注释,效果2下面一行显示,不写下面一行也可以
        //m_machineInfoCanvas.GetComponent().scaleFactor = 0.01f;

        if (GameObject.Find("jiali_QF_Aircontrol(Clone)") != null)
        {
            m_UIObj = GameObject.Find("jiali_QF_Aircontrol(Clone)/Sensor/" + this.gameObject.transform.parent.name);
            //Debug.LogError("空压机" + this.gameObject.transform.parent.name);
        }

        //AddTriggersListener(this.gameObject, EventTriggerType.PointerEnter, Enter);
        AddTriggersListener(this.gameObject, EventTriggerType.PointerClick, Click);
        //屏幕分辨率
        CommonFunc.Instance().ScreenUISelfAdptationFunc(m_machineInfoParent, 5);
        CommonFunc.Instance().ScreenUISelfAdptationFunc(m_machineInfoPanel, 5);

    }
  
    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            if (m_machineInfoState == MachineInfoState.click)
            {
                //Debug.LogError("鼠标点击");
                m_machineInfoState = MachineInfoState.exit;
            }

        }


        if (m_machineInfoState == MachineInfoState.enter)
        {
            //效果1------改变物体的大小Scale属性
            m_machineInfoPanel.SetActive(true);
            this.gameObject.transform.parent = m_machineInfoParent.transform;
            this.gameObject.transform.localPosition = Vector3.zero;
            this.gameObject.transform.localRotation = Quaternion.identity;
            this.gameObject.transform.localScale = new Vector3(0.65f, 0.65f, 1);
            m_machineInfoState = MachineInfoState.click;

            ////效果2------改变Canvas的大小scaleFactor属性
            //m_machineInfoCanvas.GetComponent().scaleFactor += Time.deltaTime * 10f;
            //if (m_machineInfoCanvas.GetComponent().scaleFactor >= 1)
            //{
            //    m_machineInfoCanvas.GetComponent().scaleFactor = 1;

            //    m_machineInfoPanel.SetActive(true);
            //    this.gameObject.transform.parent = m_machineInfoParent.transform;
            //    this.gameObject.transform.localPosition = Vector3.zero;
            //    this.gameObject.transform.localRotation = Quaternion.identity;
            //    this.gameObject.transform.localScale = new Vector3(0.65f, 0.65f, 1);

            //    m_machineInfoState = MachineInfoState.click;

            //}

        }
        else if (m_machineInfoState == MachineInfoState.click)
        {

            //if (Input.GetMouseButtonDown(0))
            //{
            //    Debug.LogError("鼠标点击");

            //    m_machineInfoState = MachineInfoState.exit;

            //}

        }
        else if (m_machineInfoState == MachineInfoState.exit)
        {
            //效果1------改变物体的大小Scale属性
            m_machineInfoPanel.SetActive(false);
            this.gameObject.transform.parent = m_UIObj.transform;
            this.gameObject.transform.localPosition = Vector3.zero;
            this.gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
            this.gameObject.transform.localScale = Vector3.one;
            m_machineInfoState = MachineInfoState.end;

            ////效果2------改变Canvas的大小scaleFactor属性
            //m_machineInfoCanvas.GetComponent().scaleFactor -= Time.deltaTime * 10f;
            //if (m_machineInfoCanvas.GetComponent().scaleFactor <= 0.01f)
            //{
            //    m_machineInfoCanvas.GetComponent().scaleFactor = 0.01f;

            //    m_machineInfoPanel.SetActive(false);
            //    this.gameObject.transform.parent = m_UIObj.transform;
            //    this.gameObject.transform.localPosition = Vector3.zero;
            //    this.gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
            //    this.gameObject.transform.localScale = Vector3.one;

            //    m_machineInfoState = MachineInfoState.end;
            //}
        }
        else if (m_machineInfoState == MachineInfoState.end)
        {

        }
    }

    public void Enter(BaseEventData arg0)
    {

        //Debug.LogError("Enter");
        m_machineInfoState = MachineInfoState.enter;

    }
    public void Click(BaseEventData arg0)
    {

        //Debug.Log("Click");
        m_machineInfoState = MachineInfoState.enter;

    }
    /// 
    /// 为obj添加Eventrigger监听事件  
    /// 
    /// 添加监听的对象
    /// 添加的监听类型
    /// 触发的函数
    private void AddTriggersListener(GameObject obj, EventTriggerType eventType, UnityAction action)
    {
        m_eventTrigger = obj.GetComponent();
        if (m_eventTrigger == null)
        {
            m_eventTrigger = obj.AddComponent();
        }
        if (m_eventTrigger.triggers.Count == 0)
        {
            m_eventTrigger.triggers = new List();
        }
        //定义所要绑定的事件类型   
        EventTrigger.Entry entry = new EventTrigger.Entry();
        //设置事件类型    
        entry.eventID = eventType;
        //定义回调函数    
        UnityAction callback = new UnityAction(action);
        //设置回调函数    
        entry.callback.AddListener(callback);
        //添加事件触发记录到GameObject的事件触发组件    
        m_eventTrigger.triggers.Add(entry);
    }
    public static Vector3 WorldToUIPoint(Canvas canvas, Transform worldGo)
    {
        Vector3 v_v3 = Camera.main.WorldToScreenPoint(worldGo.position);
        Vector3 v_ui = canvas.worldCamera.ScreenToWorldPoint(v_v3);
        Vector3 v_new = new Vector3(v_ui.x, v_ui.y, canvas.GetComponent().anchoredPosition3D.z);
        return v_new;
    }

    public MachineInfoState m_machineInfoState = MachineInfoState.start;
    public enum MachineInfoState
    {
        start,
        enter,
        click,
        exit,
        end
    }

}

 

你可能感兴趣的:(Unity开发,Unity,UGUI)