2018-09-08 16:19:17 U3DCoder 阅读数 1398

此方法是将世界坐标转换为屏幕坐标,并实现UI的缩放移动效果,具体情况具体分析。

应用于3D场景中有很多3DUI界面,功能如下所示。【效果>>>>从哪里来到哪里去<<<<】

  1. 点击3DUI界面转换为2DUI界面,并放大移动到屏幕中心显示
  2. 点击已点击过3DUI界面或其它3DUI界面,显示的2DUI界面缩小回原处并放大新点击的3DUI界面
  3. 给2DUI界面添加Button按钮,点击2DUI界面缩小返回到3DUI界面位置点

 

详细代码如下

    //加载数据UI界面
    private GameObject dataPanel;
    private Text dataName, value;
    GameObject dataUIParent;
    public void WorldToScrennPoint(GameObject _parent)
    {
        mask =  1 << 15;//实例化mask自定义的层级之上;

        dataUIParent = _parent;

        var _dataUI = Resources.Load<GameObject>("factory/DataUI");
        dataPanel = GameObject.Instantiate(_dataUI) as GameObject;
        dataPanel.transform.SetParent(dataUIParent.transform, false);

        dataName = dataPanel.transform.Find("lineBg/name").GetComponent<Text>();
        value = dataPanel.transform.Find("lineBg/Value").GetComponent<Text>();

        dataPanel.transform.FindChild("lineBg").GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() =>
        {
            DataUIShrink();//缩小UI界面
        });
    }


    //用于检测数据UI界面信息
    public LayerMask mask = 1 << 15;//实例化mask自定义的层级之上;
    GameObject gameObj;
    Vector2 screenPos;
    Tweener scaleTweener;
    void Update()
    {
         if (Input.GetMouseButtonDown(0))
        {
            //创建射线;从摄像机发射一条经过鼠标当前位置的射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            if (Physics.Raycast(ray, out hitInfo, 500, mask.value))
            {
                if (dataPanel.transform.localScale == Vector3.zero)
                {
                    gameObj = hitInfo.collider.gameObject;
                    //赋值
                    dataName.text = hitInfo.transform.parent.parent.parent.name;
                    value.text = gameObj.GetComponent<Text>().text;

                    DataUIMagnify();//放大UI              
                }
                else
                {
                    DataUIShrink(); //缩小UI界面

                    gameObj = hitInfo.collider.gameObject;
                    //赋值
                    dataName.text = hitInfo.transform.parent.parent.parent.name;
                    value.text = gameObj.GetComponent<Text>().text;

                    scaleTweener.OnComplete(DataUIMagnify);//缩小后放大点击的另一个UI界面

                }
            }
        
            Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
        }
    }

    //将点击的物体坐标转换为屏幕坐标-----------UI界面放大移动
    private void DataUIMagnify()
    {
        screenPos = Camera.main.WorldToScreenPoint(gameObj.transform.position);//世界坐标转屏幕坐标
        dataPanel.transform.position = screenPos;

        dataPanel.transform.DOScale(Vector3.one, 0.3f);
        dataPanel.transform.DOLocalMove(Vector3.zero, 0.3f);

    }
    //将点击的物体坐标转换为屏幕坐标-----------UI界面缩小移动
    private void DataUIShrink()
    {
        if (gameObj == null)
            return;
        screenPos = Camera.main.WorldToScreenPoint(gameObj.transform.position);//世界坐标转换为屏幕坐标

        dataPanel.transform.DOScale(Vector3.zero, 0.3f);
        scaleTweener = dataPanel.transform.DOMove(screenPos, 0.3f);
        Debug.LogError("缩小");
    }

 

2018-08-31 16:51:17 U3DCoder 阅读数 917

此方法是改变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<CanvasScaler>().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<CanvasScaler>().scaleFactor += Time.deltaTime * 10f;
            //if (m_machineInfoCanvas.GetComponent<CanvasScaler>().scaleFactor >= 1)
            //{
            //    m_machineInfoCanvas.GetComponent<CanvasScaler>().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<CanvasScaler>().scaleFactor -= Time.deltaTime * 10f;
            //if (m_machineInfoCanvas.GetComponent<CanvasScaler>().scaleFactor <= 0.01f)
            //{
            //    m_machineInfoCanvas.GetComponent<CanvasScaler>().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;

    }
    /// <summary>
    /// 为obj添加Eventrigger监听事件  
    /// </summary>
    /// <param name="obj">添加监听的对象</param>
    /// <param name="eventType">添加的监听类型</param>
    /// <param name="action">触发的函数</param>
    private void AddTriggersListener(GameObject obj, EventTriggerType eventType, UnityAction<BaseEventData> action)
    {
        m_eventTrigger = obj.GetComponent<EventTrigger>();
        if (m_eventTrigger == null)
        {
            m_eventTrigger = obj.AddComponent<EventTrigger>();
        }
        if (m_eventTrigger.triggers.Count == 0)
        {
            m_eventTrigger.triggers = new List<EventTrigger.Entry>();
        }
        //定义所要绑定的事件类型   
        EventTrigger.Entry entry = new EventTrigger.Entry();
        //设置事件类型    
        entry.eventID = eventType;
        //定义回调函数    
        UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(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<RectTransform>().anchoredPosition3D.z);
        return v_new;
    }

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

}

 

2014-04-04 21:06:50 inspironx 阅读数 4515

NGUI是一款主打2dui界面或者动画的插件,如果直接在UIRoot里加入3d的物体或者粒子是不会显示出来的

主要原因是2d UI是用正交相机来投射加入NGUI定制相机使得投射范围在一个方形可视范围内

在z轴的视觉表现上被忽略


a.在Unity3d组件菜单上创建一个灯光,创建一个Camera

b.设置Camera的参数为,添加UICamera到Camera上


c.创建一个NGUI 2d UI,删除UI Root内的组件物体,设置UI Root


d.在UI Root下创建一个3d UI组,在Widget Tool里添加的组件添加到这里

e.在UI Root下创建一个GameScene级,游戏3d组件存放在这里

f.在3d UI下添加一些UI Widget,在GameScene里添加一些组件,这样3d组件与ui可以在同一个相机投影到

视图窗口

g.3d Game组件有z轴表现,当前Camera在-1.5的位置,需要根据游戏的设定来进行调整





2011-12-03 16:22:20 cynixway 阅读数 2272

在做一个好玩的UI的界面,想了2天了,先留坑

代码肯定是不会贴,但是会把思路讲清楚

生平最讨厌的就是照搬照抄,思路,关键是思路

我的思维跳跃比较诡异,考虑上代码,先给思路

2011.12.05增加界面一

苦x的程序员生活,写到凌晨2点,只完善了一个界面,只有简单的2个按钮,3个位置

实现了如下界面

两个按钮,3个位置,初始一边一个,中间位置为空(以下表述为按钮1,2,位置1,2,3)

点击按钮1,其移动到位置2,如按钮2在位置2处则按钮2自动退回到位置3,并开始自动播放图片,点击滚动中的图片触发某事件

点击按钮2,其移动到位置2,如按钮1在位置2处则按钮2自动退回到位置1,并开始自动播放图片,点击滚动中的图片触发某事件

思路:

在window中实现button的移动,其实移动到位置2的是带有button的window

移动过程使用了Vector4.larp,但是不要用==来比较Rect,Rect里面的成员是float类型,伤不起,可以用Vector3.Distance()<0.1f来看是否

 public static Rect Vec4ToRect(Vector4 v4)
    {
        return new Rect(v4.x, v4.y, v4.z, v4.w);
    }
   public static Vector4 RectToVec4(Rect rect)
    {
        return new Vector4(rect.x,rect.y,rect.width,rect.height);
    }

记录位置和状态,代码很少,除去注释什么的大概也就180行左右,有空了考虑优化下

 

Unity3D简单的UI系统

阅读数 5032

没有更多推荐了,返回首页