虚拟仿真项目之工具栏

Posted fangshiyuanzhucheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了虚拟仿真项目之工具栏相关的知识,希望对你有一定的参考价值。

首先看一下效果图:

技术图片

 

 然后是干货源代码:

using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 工具快捷栏面板
/// 大类:打开界面默认可以看到的一排按钮
/// 小类:点击打开界面默认可以看到的一排按钮 之后 弹出的第二级 新的一排工具按钮
/// </summary>
public class ToolShortCutKeyPanel : BasePanel

    bool isClick;//点击是否弹出面板
    public int toolParBtnCount;//生成大类格子数量
    public int toolBtnCount;//生成小类格子数量
    public GameObject toolParBtn;//大类格子工具按钮
    public GameObject toolBtn;//小类格子工具按钮
    public GameObject nullToolBtn;//空格子
    public GameObject toolBtnParTrans;//工具栏小类所有格子的父物体
    public GameObject lucencyImage;//透明遮罩
    public Sprite selectSprite;//选中图片
    public Sprite noSelectSprite;//未选中图片
    public Sprite up;//工具栏伸展按钮图片
    public Sprite down;//工具栏伸展按钮图片
    public Image updownImage;//工具栏伸展按钮图片组件
    public Button toolShortCutKeyBtn;//工具栏弹出(缩回)按钮
    public Transform second;//工具栏小类所有格子的父物体的父物体
    public Transform toolParBtnsParent;//工具栏大类所有格子的父物体
    public Transform activeInventoryTrans;//打开的小类的父物体
    public List<Transform> toolsObjectList;////工具栏小类所有格子的父物体集合
    public List<ToolButton> toolBtnList;//所有小类工具按钮集合
    public List<ToolParentButton> toolParBtnList;//所有大类工具按钮集合
    public int[] array1;//大类格子ID数组
    public int[] array2;//小类格子ID数组
    public char[] charCtrl;//操作指令按键数组(显示)
    protected override void Awake()
    
        base.Awake();
        for (int i = 0; i < toolsObjectList.Count; i++)
        
            for (int j = 0; j < toolsObjectList[i].childCount; j++)
            
                toolBtnList.Add(toolsObjectList[i].GetChild(j).GetComponent<ToolButton>());
            
        
    
    int ss = 0;
    protected override void Start()
    
        base.Start();
        toolShortCutKeyBtn.onClick.AddListener(OnClickToolShortCutKeyBtn);
        for (int i = 0; i < toolParBtnCount; i++)
        
            //生成大类格子
            GameObject tpbn = Instantiate(toolParBtn, toolParBtnsParent);
            tpbn.GetComponent<Item>().id = array1[i];
            tpbn.GetComponent<Item>().InitItemProperty();
            toolParBtnList.Add(tpbn.GetComponent<ToolParentButton>());
            int ii = i;
            tpbn.GetComponentInChildren<Text>().text = charCtrl[ii].ToString();
            toolParBtnList[ii].GetComponent<Button>().onClick.AddListener(delegate ()  OnClickToolParentButton(ii); );
            GameObject tbpt = Instantiate(toolBtnParTrans, second);
            toolsObjectList.Add(tbpt.transform);
        
        for (int i = 0; i < toolsObjectList.Count; i++)
        
            for (int j = 0; j < toolBtnCount; j++)
            
                if (ss >= array2.Length)
                
                    //生成空格子
                    Instantiate(nullToolBtn, toolsObjectList[i]);
                
                else
                
                    //生成小类格子
                    GameObject tbn = Instantiate(toolBtn, toolsObjectList[i]);
                    tbn.GetComponent<Item>().id = array2[ss];
                    tbn.GetComponent<Item>().InitItemProperty();
                    toolBtnList.Add(tbn.GetComponent<ToolButton>());
                    tbn.GetComponentInChildren<Text>().text = (j + 1).ToString();
                    int ii = ss;
                    toolBtnList[ii].GetComponent<Button>().onClick.AddListener(delegate ()  OnClickToolButton(ii); );
                    ss++;
                
            
        
        OnInitPanel();
    
    //点击小类按钮
    public void OnClickToolButton(int num)
    
        //高亮
        for (int ii = 0; ii < toolBtnList.Count; ii++)
        
            int i = ii;
            if (i == num)
            
                if (toolBtnList[i].isClick)
                
                    toolBtnList[i].isClick = false;
                    //发送玩家选择物品事件
                    PlayerSelectItemArgs arg = new PlayerSelectItemArgs
                    
                        item = toolBtnList[i].GetComponent<Item>()
                    ;
                    NotificationCenter.Instance().PostDispatch((uint)CommonEnumType.Player_Deselect_Item, new Notification(arg, this));
                    toolBtnList[i].GetComponent<Image>().sprite = noSelectSprite;
                
                else
                
                    toolBtnList[i].isClick = true;
                    //发送玩家选择物品事件
                    PlayerSelectItemArgs arg = new PlayerSelectItemArgs
                    
                        item = toolBtnList[i].GetComponent<Item>()
                    ;
                    NotificationCenter.Instance().PostDispatch((uint)CommonEnumType.Player_Select_Item, new Notification(arg, this));
                    NotificationCenter.Instance().PostDispatch((uint)CommonEnumType.Open_Panel, new Notification(PanelEnum.Item2DPropertyPanel.ToString(), toolBtnList[i].GetComponent<Item>()));
                    toolBtnList[i].GetComponent<Image>().sprite = selectSprite;
                
            
            else
            
                toolBtnList[i].GetComponent<Image>().sprite = noSelectSprite;
                toolBtnList[i].isClick = false;
            
        
    
    //点击大类按钮
    public void OnClickToolParentButton(int num)
    
        //高亮
        for (int i = 0; i < toolParBtnList.Count; i++)
        
            if (i == num)
            
                toolParBtnList[i].GetComponent<Image>().sprite = selectSprite;
            
            else
            
                toolParBtnList[i].GetComponent<Image>().sprite = noSelectSprite;
            
        
        //对应子工具栏出现
        for (int i = 0; i < toolsObjectList.Count; i++)
        
            if (i == num)
            
                toolsObjectList[i].gameObject.SetActive(true);
                activeInventoryTrans = toolsObjectList[i];
            
            else
            
                toolsObjectList[i].gameObject.SetActive(false);
            
        
    
    private void OnClickToolShortCutKeyBtn()
    
        if (!isClick)
        
            ShowParentParent();
        
        else
        
            HideParentParent();
        
    
    //展示界面
    public void ShowParentParent()
    
        if (isClick) return;
        isClick = true;
        transform.DOLocalMove(new Vector3(transform.localPosition.x, transform.localPosition.y + 120f, transform.localPosition.z), 0.7f);
        updownImage.sprite = down;
    
    //隐藏界面
    public void HideParentParent()
    
        if (!isClick) return;
        ClearSecond();
        isClick = false;
        transform.DOLocalMove(new Vector3(transform.localPosition.x, transform.localPosition.y - 120f, transform.localPosition.z), 0.7f);
        updownImage.sprite = up;
    
    //小类隐藏
    public void ClearSecond()
    
        foreach (Transform item in toolsObjectList)
        
            item.gameObject.SetActive(false);
        
        activeInventoryTrans = null;
    
    //清除小类高亮
    public void ClearSecondSelect()
    
        foreach (ToolButton item in toolBtnList)
        
            item.GetComponent<Image>().sprite = noSelectSprite;
        
    
    //清除大类高亮
    public void ClearFirstSelect()
    
        for (int i = 0; i < toolParBtnList.Count; i++)
        
            toolParBtnList[i].GetComponent<Image>().sprite = noSelectSprite;
        
        //activeInventoryTrans = null;
    
    public void OnInitPanel()
    
        ClearSecond();
        ClearFirstSelect();
        ClearSecondSelect();
    

    //所有工具未点击
    public void BtuNoClick()
    
        for (int i = 0; i < toolBtnList.Count; i++)
        
            toolBtnList[i].isClick = false;
        
    
using UnityEngine.UI;
using UnityEngine;

/// <summary>
/// 工具快捷栏面板控制器
/// </summary>
public class ToolShortCutKeyCtrl : ControllerBase

    public ToolShortCutKeyPanel toolShortCutKeyPanel;
    public ItemLayoutController itemLayoutController;
    public ItemLayoutExamController itemLayoutExamController;
    public bool isClose;
    protected override void Awake()
    
        base.Awake();
        noticeCenter.AttachObsever((uint)CommonEnumType.Closed_UITool_Light, ClosedUIToolLight);
        noticeCenter.AttachObsever((uint)CommonEnumType.Esc_Clear_Select_Item, EscClearSelectItem);
    

    private void OnDestroy()
    
        noticeCenter.DetachObsever((uint)CommonEnumType.Closed_UITool_Light, ClosedUIToolLight);
        noticeCenter.DetachObsever((uint)CommonEnumType.Esc_Clear_Select_Item, EscClearSelectItem);
    

    private void EscClearSelectItem(Notification notification)
    
        EscClearSelectItem();
    

    private void ClosedUIToolLight(Notification notification)
    
        ClearSelect();
        toolShortCutKeyPanel.BtuNoClick();
    

    private void Update()
    
        if (isClose) return;
        if (Input.GetKeyDown(KeyCode.Escape))
        
            EscClearSelectItem();
        
        if (Input.GetKeyDown(KeyCode.Z))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[0].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.X))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[1].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.C))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[2].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.V))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[3].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.F))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[4].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.G))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[5].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.R))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[6].GetComponent<Button>().onClick.Invoke();
        
        if (Input.GetKeyDown(KeyCode.T))
        
            toolShortCutKeyPanel.ShowParentParent();
            toolShortCutKeyPanel.toolParBtnList[7].GetComponent<Button>().onClick.Invoke();
        
        if (toolShortCutKeyPanel.activeInventoryTrans)
        
            if (Input.GetKeyDown(KeyCode.Alpha1))
            
                toolShortCutKeyPanel.activeInventoryTrans.GetChild(0).GetComponent<Button>().onClick.Invoke();
            
            if (Input.GetKeyDown(KeyCode.Alpha2))
            
                toolShortCutKeyPanel.activeInventoryTrans.GetChild(1).GetComponent<Button>().onClick.Invoke();
            
            if (Input.GetKeyDown(KeyCode.Alpha3))
            
                toolShortCutKeyPanel.activeInventoryTrans.GetChild(2).GetComponent<Button>().onClick.Invoke();
            
            if (Input.GetKeyDown(KeyCode.Alpha4))
            
                toolShortCutKeyPanel.activeInventoryTrans.GetChild(3).GetComponent<Button>().onClick.Invoke();
            
            if (Input.GetKeyDown(KeyCode.Alpha5))
            
                toolShortCutKeyPanel.activeInventoryTrans.GetChild(4).GetComponent<Button>().onClick.Invoke();
            
            //if (Input.GetKeyDown(KeyCode.Alpha6))
            //
            //    toolShortCutKeyPanel.activeInventoryTrans.GetChild(5).GetComponent<Button>().onClick.Invoke();
            //
            //if (Input.GetKeyDown(KeyCode.Alpha7))
            //
            //    toolShortCutKeyPanel.activeInventoryTrans.GetChild(6).GetComponent<Button>().onClick.Invoke();
            //
            //if (Input.GetKeyDown(KeyCode.Alpha8))
            //
            //    toolShortCutKeyPanel.activeInventoryTrans.GetChild(7).GetComponent<Button>().onClick.Invoke();
            //
        
    

    private void EscClearSelectItem()
    
        CancelTool();
        toolShortCutKeyPanel.BtuNoClick();
    

    public void HidePanel()
    
        toolShortCutKeyPanel.HideParentParent();
    
    public void CancelTool()
    
        //NotificationCenter.Instance().PostDispatch((uint)CommonEnumType.Close_Panel, new Notification(PanelEnum.Item2DPropertyPanel.ToString(), this));
        toolShortCutKeyPanel.ClearFirstSelect();
        toolShortCutKeyPanel.ClearSecondSelect();
        toolShortCutKeyPanel.ClearSecond();
        if (itemLayoutController.gameObject.activeSelf)
            itemLayoutController.OnDeselectItem();
        else
            itemLayoutExamController.OnDeselectItem();
    
    void ClearSelect()
    
        //toolShortCutKeyPanel.ClearFirstSelect();
        toolShortCutKeyPanel.ClearSecondSelect();
    
    //关闭快捷栏功能
    public void CloseFunction()
    
        isClose = true;
        toolShortCutKeyPanel.lucencyImage.SetActive(true);
    
    //开启快捷栏功能
    public void OpenFunction()
    
        isClose = false;
        toolShortCutKeyPanel.lucencyImage.SetActive(false);
    

接着是物体组件截图:

技术图片


在开始之前我们只需要把这些字段设置好

技术图片技术图片


Array1为第一层工具栏的大类工具ID(ID是使用时用来判断是否正确,也是用来显示属性的重要依据),也就是这些

技术图片

对应的ID分别为101~109


 

Array2为第二层,如图,从1到5 的ID分别为 1011~1015,以此类推第八个大类第五个小类的格子ID为 1085(ID可不固定,我这样更便于记)

技术图片


 

CharCtrl就是我们的快捷键字符数组,要跟快捷键控制器的按键一一对应


 

最后就是一些UI的摆放和做预制

 

以上是关于虚拟仿真项目之工具栏的主要内容,如果未能解决你的问题,请参考以下文章

unity虚拟仿真怎么样

几种专业的虚拟仿真开发工具

西门子PLC S7-200虚拟仿真模拟软件实训调试工具组态软件联调

虚拟电能表 仿真dlt645规约通信协议虚拟智能电表模拟器软件工具

基于virtual Box linux虚拟机外连接secure CRT终端仿真工具(很详细)

华为云计算HCIE之oceanstor仿真器的安装教程